diff --git a/.releaserc.json b/.releaserc.json index bac320b..9a4c2c7 100644 --- a/.releaserc.json +++ b/.releaserc.json @@ -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" } ] } diff --git a/Makefile b/Makefile index d58b5c9..7bda0c1 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/docs/modules/Platform-Helpers.md b/docs/modules/Platform-Helpers.md new file mode 100644 index 0000000..eed35fd --- /dev/null +++ b/docs/modules/Platform-Helpers.md @@ -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 + diff --git a/src/main.sh b/src/main.sh index 49e98d0..a543ba6 100644 --- a/src/main.sh +++ b/src/main.sh @@ -6,3 +6,6 @@ source "prompts.sh" source "user_feedback.sh" # shellcheck disable=SC1091 source "logging.sh" +# shellcheck disable=SC1091 +source "platform_helpers.sh" + diff --git a/src/platform_helpers.sh b/src/platform_helpers.sh new file mode 100644 index 0000000..7afa38a --- /dev/null +++ b/src/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 +} + diff --git a/test.sh b/test.sh index b9dfb08..dd4bdf0 100755 --- a/test.sh +++ b/test.sh @@ -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 #