Last active
March 19, 2021 04:15
-
-
Save ahawkins/694a02768641d7850d0b4b21e2c93b91 to your computer and use it in GitHub Desktop.
stdlib.sh
This file contains hidden or 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
assert::var() { | |
if [ -z "${1:-}" ]; then | |
log::error "Argument required" | |
return 1 | |
fi | |
set +u | |
if [ -z "${!1}" ]; then | |
log::error "${1} required, nothing given" | |
return 1 | |
fi | |
set -u | |
} | |
assert::path() { | |
if [ -z "${1:-}" ]; then | |
log::error "File name required" | |
return 1 | |
fi | |
if [ ! -f "${1}" ] && [ ! -d "${1}" ]; then | |
log::error "No such path ${1}" | |
return 1 | |
fi | |
} |
This file contains hidden or 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
log::header() { | |
echo '=>' "$@" 1>&2 | |
} | |
log::indicator() { | |
echo '->' "$@" 1>&2 | |
} | |
log::debug() { | |
if tput colors &>/dev/null; then | |
# XXX: Light gray color | |
echo -e "\033[1;30m-> $*\033[0m" 1>&2 | |
else | |
echo '->' "$@" 1>&2 | |
fi | |
} | |
log::info() { | |
if tput colors &>/dev/null; then | |
# XXX: Cyan | |
echo -e "\033[0;36m-> $*\033[0m" 1>&2 | |
else | |
echo '->' "$@" 1>&2 | |
fi | |
} | |
log::warn() { | |
if tput colors &>/dev/null; then | |
# XXX: bright yellow | |
echo -e "\033[1;33m-> $*\033[0m" 1>&2 | |
else | |
echo "->" "$@" 1>&2 | |
fi | |
} | |
log::error() { | |
if tput colors &>/dev/null; then | |
# XXX: Red | |
echo -e "\033[31mERROR: $*\033[0m" 1>&2 | |
else | |
echo "ERROR:" "$@" 1>&2 | |
fi | |
} | |
log::success() { | |
if tput colors &>/dev/null; then | |
# XXX: green | |
echo -e "\033[32m-> $*\033[0m" 1>&2 | |
else | |
echo "->" "$@" 1>&2 | |
fi | |
} | |
log::exec_command() { | |
log::debug '$' "$@" | |
"$@" | |
} | |
log::error_banner() { | |
echo 1>&2 | |
echo 1>&2 | |
if tput colors &>/dev/null; then | |
# XXX: red | |
echo -e "\033[1;31m$(cat "${1}")\033[0m" 1>&2 | |
else | |
cat "${1}" | |
fi | |
echo 1>&2 | |
echo 1>&2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment