-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create and use library of common functions
- Loading branch information
Showing
7 changed files
with
158 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/bin/sh | ||
|
||
|
||
# Find the executable passed as an argument in the PATH variable and print it. | ||
find_exec() { | ||
while IFS= read -r dir; do | ||
if [ -n "${dir}" ] && [ -d "${dir}" ]; then | ||
if [ -x "${dir%/}/$1" ] && [ "${dir%/}/$1" != "$(abspath "$0")" ]; then | ||
printf %s\\n "${dir%/}/$1" | ||
break | ||
fi | ||
fi | ||
done <<EOF | ||
$(printf %s\\n "$PATH"|tr ':' '\n') | ||
EOF | ||
} | ||
|
||
# shellcheck disable=SC2120 # Take none or one argument | ||
to_lower() { | ||
if [ -z "${1:-}" ]; then | ||
tr '[:upper:]' '[:lower:]' | ||
else | ||
printf %s\\n "$1" | to_lower | ||
fi | ||
} | ||
|
||
is_true() { | ||
case "$(to_lower "${1:-}")" in | ||
1 | true | yes | y | on | t) return 0;; | ||
*) return 1;; | ||
esac | ||
} | ||
|
||
# shellcheck disable=SC2120 # Function has good default. | ||
random_string() { | ||
LC_ALL=C tr -dc 'a-zA-Z0-9' < /dev/urandom | head -c "${1:-12}" | ||
} | ||
|
||
usage() { | ||
# This uses the comments behind the options to show the help. Not extremly | ||
# correct, but effective and simple. | ||
echo "$0 -- ${KRUNVM_RUNNER_MAIN:-"Part of the gh-krunvm-runner project"}" && \ | ||
grep "[[:space:]].) #" "$0" | | ||
sed 's/#//' | | ||
sed -r 's/([a-z-])\)/-\1/' | ||
exit "${1:-0}" | ||
} | ||
|
||
# PML: Poor Man's Logging | ||
_log() { | ||
printf '[%s] [%s] [%s] %s\n' \ | ||
"$(basename "$0")" \ | ||
"${2:-LOG}" \ | ||
"$(date +'%Y%m%d-%H%M%S')" \ | ||
"${1:-}" \ | ||
>&"$KRUNVM_RUNNER_LOG" | ||
} | ||
trace() { if [ "${KRUNVM_RUNNER_VERBOSE:-0}" -ge "3" ]; then _log "$1" TRC; fi; } | ||
debug() { if [ "${KRUNVM_RUNNER_VERBOSE:-0}" -ge "2" ]; then _log "$1" DBG; fi; } | ||
verbose() { if [ "${KRUNVM_RUNNER_VERBOSE:-0}" -ge "1" ]; then _log "$1" NFO; fi; } | ||
info() { if [ "${KRUNVM_RUNNER_VERBOSE:-0}" -ge "1" ]; then _log "$1" NFO; fi; } | ||
warn() { _log "$1" WRN; } | ||
error() { _log "$1" ERR && exit 1; } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.