feat: Add platform helpers
This commit is contained in:
@@ -6,3 +6,6 @@ source "prompts.sh"
|
||||
source "user_feedback.sh"
|
||||
# shellcheck disable=SC1091
|
||||
source "logging.sh"
|
||||
# shellcheck disable=SC1091
|
||||
source "platform_helpers.sh"
|
||||
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
#!/bin/bash
|
||||
# @name Platform-Helpers
|
||||
# @brief Platform specific helpers
|
||||
|
||||
# @description Detect the OS the script is running on
|
||||
# @stdout solaris | macos | linux | bsd | windows | unknown
|
||||
detect_os() {
|
||||
case "$OSTYPE" in
|
||||
solaris*) echo "solaris"; ;;
|
||||
darwin*) echo "macos"; ;;
|
||||
linux*) echo "linux"; ;;
|
||||
bsd*) echo "bsd"; ;;
|
||||
msys*) echo "windows"; ;;
|
||||
cygwin*) echo "windows"; ;;
|
||||
*) echo "unknown"; ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# @description Get opener command for platform
|
||||
# @stdout Command that can be used, if it is not supported returns an empty string
|
||||
get_opener() {
|
||||
local cmd
|
||||
case "$(detect_os)" in
|
||||
darwin) cmd="open"; ;;
|
||||
linux) cmd="xdg-open"; ;;
|
||||
windows) cmd="start"; ;;
|
||||
*) cmd=""; ;;
|
||||
esac
|
||||
echo "$cmd"
|
||||
}
|
||||
|
||||
# @description Open a link using the default opener, if it is not possible/supported or an error occurs simply prints the url with instructions
|
||||
# @arg $1 Link to open
|
||||
# @exitcode 1 Failed to open link
|
||||
# @exitcode 0 Opened link using util
|
||||
# @stdout Instructions in case link can not be opened
|
||||
open_link() {
|
||||
cmd="$(get_opener)"
|
||||
if [ "$cmd" == "" ]; then
|
||||
echo "Your platform is not supported for opening links."
|
||||
echo "Please open the following URL in your preferred browser:"
|
||||
echo " ${1}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
$cmd "$1"
|
||||
|
||||
if [[ $? -eq 1 ]]; then
|
||||
echo "Failed to open your browser."
|
||||
echo "Please open the following URL in your browser:"
|
||||
echo "${1}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user