feat: Add platform helpers
This commit is contained in:
@@ -53,6 +53,11 @@
|
||||
"path": "dist/user_feedback.sh",
|
||||
"name": "user-feedback.bash",
|
||||
"label": "Component > User-Feedback - Provides useful colored outputs for user feedback on actions"
|
||||
},
|
||||
{
|
||||
"path": "dist/platform_helpers.sh",
|
||||
"name": "platform-helpers.bash",
|
||||
"label": "Component > Platform-Helpers - Platform specific helpers"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ generate-docs: ## Build documentation using docker container
|
||||
@docker run --rm bash-tui-toolkit/shdoc 'shdoc < logging.sh ' 2>&1 > docs/modules/Logging.md
|
||||
@docker run --rm bash-tui-toolkit/shdoc 'shdoc < prompts.sh ' 2>&1 > docs/modules/Prompts.md
|
||||
@docker run --rm bash-tui-toolkit/shdoc 'shdoc < user_feedback.sh ' 2>&1 > docs/modules/User-Feedback.md
|
||||
@docker run --rm bash-tui-toolkit/shdoc 'shdoc < platform_helpers.sh ' 2>&1 > docs/modules/Platform-Helpers.md
|
||||
|
||||
_remove_comments_from_file:
|
||||
@cat $(file) | sed '/^$$/d' | sed '/^#/d' | sed '/^\s*#/d' | tee $(file) > /dev/null
|
||||
@@ -33,3 +34,4 @@ build: ## Bundle script to dist folder and remove all top level comments
|
||||
@$(MAKE) _push_module module=logging
|
||||
@$(MAKE) _push_module module=prompts
|
||||
@$(MAKE) _push_module module=user_feedback
|
||||
$(MAKE) _push_module module=platform_helpers
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
# Platform-Helpers
|
||||
|
||||
Platform specific helpers
|
||||
|
||||
## Overview
|
||||
|
||||
Detect the OS the script is running on
|
||||
|
||||
## Index
|
||||
|
||||
* [detect_os](#detect_os)
|
||||
* [get_opener](#get_opener)
|
||||
* [open_link](#open_link)
|
||||
|
||||
### detect_os
|
||||
|
||||
Detect the OS the script is running on
|
||||
|
||||
#### Output on stdout
|
||||
|
||||
* solaris | macos | linux | bsd | windows | unknown
|
||||
|
||||
### get_opener
|
||||
|
||||
Get opener command for platform
|
||||
|
||||
#### Output on stdout
|
||||
|
||||
* Command that can be used, if it is not supported returns an empty string
|
||||
|
||||
### open_link
|
||||
|
||||
Open a link using the default opener, if it is not possible/supported or an error occurs simply prints the url with instructions
|
||||
|
||||
#### Arguments
|
||||
|
||||
* **$1** (Link): to open
|
||||
|
||||
#### Exit codes
|
||||
|
||||
* **1**: Failed to open link
|
||||
* **0**: Opened link using util
|
||||
|
||||
#### Output on stdout
|
||||
|
||||
* Instructions in case link can not be opened
|
||||
|
||||
@@ -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