feat: Add platform helpers

This commit is contained in:
Timo Reymann
2023-02-17 10:32:12 +01:00
parent c086017998
commit f6a6e91d2f
6 changed files with 121 additions and 0 deletions
+5
View File
@@ -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"
}
]
}
+2
View File
@@ -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
+47
View File
@@ -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
+3
View File
@@ -6,3 +6,6 @@ source "prompts.sh"
source "user_feedback.sh"
# shellcheck disable=SC1091
source "logging.sh"
# shellcheck disable=SC1091
source "platform_helpers.sh"
+57
View File
@@ -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
}
+7
View File
@@ -6,6 +6,13 @@ cd src || exit 2
source main.sh
cd - || exit 2
#
# Platform
#
echo "You are using the OS '$(detect_os)'"
echo "Opener for tools/links: '$(get_opener)'"
open_link "https://github.com"
#
# UTILS
#