2017-09-19 10:10:53 +00:00
|
|
|
COLOR_RED="\033[0;31m"
|
|
|
|
COLOR_GREEN="\033[0;32m"
|
|
|
|
COLOR_CYAN="\033[0;36m"
|
2018-02-02 09:54:00 +00:00
|
|
|
COLOR_YELLOW="\033[0;33m"
|
2017-09-19 10:10:53 +00:00
|
|
|
COLOR_PLAIN="\033[0m"
|
2020-09-06 11:35:43 +00:00
|
|
|
COLOR_PURPLE="\033[35m"
|
2017-09-19 10:10:53 +00:00
|
|
|
|
2018-12-13 15:49:43 +00:00
|
|
|
function check_util() {
|
2022-10-29 21:14:14 +00:00
|
|
|
command -v ${1} >/dev/null 2>&1 || fail "Missing ${1} util"
|
|
|
|
}
|
|
|
|
|
|
|
|
function check_utils() {
|
|
|
|
for util in "$@"; do
|
|
|
|
check_util "${util}"
|
|
|
|
done
|
2018-12-13 15:49:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 11:35:43 +00:00
|
|
|
function debug() {
|
2022-10-29 21:14:14 +00:00
|
|
|
log_level_matches 0 || return 0
|
|
|
|
echo -e "${COLOR_PURPLE}$@${COLOR_PLAIN}"
|
2020-09-06 11:35:43 +00:00
|
|
|
}
|
|
|
|
|
2018-03-10 11:47:55 +00:00
|
|
|
function error() {
|
2022-10-29 21:14:14 +00:00
|
|
|
log_level_matches 3 || return 0
|
|
|
|
echo -e "${COLOR_RED}$@${COLOR_PLAIN}" >&2
|
2017-09-19 10:10:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-10 11:47:55 +00:00
|
|
|
function fail() {
|
2022-10-29 21:14:14 +00:00
|
|
|
error "$@"
|
|
|
|
exit 1
|
2018-10-07 12:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function fatal() {
|
2022-10-29 21:14:14 +00:00
|
|
|
fail "$@"
|
2017-09-19 10:10:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-10 11:47:55 +00:00
|
|
|
function info() {
|
2022-10-29 21:14:14 +00:00
|
|
|
log_level_matches 1 || return 0
|
|
|
|
echo -e "${COLOR_CYAN}$@${COLOR_PLAIN}" >&2
|
2017-09-19 10:10:53 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 14:47:45 +00:00
|
|
|
function join_by() {
|
|
|
|
local d=$1
|
|
|
|
shift
|
|
|
|
echo -n "$1"
|
|
|
|
shift
|
|
|
|
printf "%s" "${@/#/$d}"
|
|
|
|
}
|
|
|
|
|
2021-12-13 17:57:02 +00:00
|
|
|
function log_level_matches() {
|
2022-10-29 21:14:14 +00:00
|
|
|
declare -A log_levels=(
|
|
|
|
[debug]=0
|
|
|
|
[info]=1
|
|
|
|
[warn]=2
|
|
|
|
[warning]=2
|
|
|
|
[error]=3
|
|
|
|
)
|
|
|
|
local fb_ll=${log_levels[${DEFAULT_LOG_LEVEL:-info}]}
|
|
|
|
[ ${log_levels[${LOG_LEVEL:-UNDEF}]:-${fb_ll}} -le ${1} ] && return 0 || return 1
|
2021-12-13 17:57:02 +00:00
|
|
|
}
|
|
|
|
|
2018-03-10 11:47:55 +00:00
|
|
|
function step() {
|
2022-10-29 21:14:14 +00:00
|
|
|
info "[$(date +%H:%M:%S)] $(printf "%${script_level:-0}s" '' | tr ' ' '+')$@"
|
2017-09-19 10:10:53 +00:00
|
|
|
}
|
2017-09-19 10:55:43 +00:00
|
|
|
|
2018-03-10 11:47:55 +00:00
|
|
|
function success() {
|
2022-10-29 21:14:14 +00:00
|
|
|
log_level_matches 1 || return 0
|
|
|
|
echo -e "${COLOR_GREEN}$@${COLOR_PLAIN}" >&2
|
2017-09-19 10:55:43 +00:00
|
|
|
}
|
2018-02-02 09:54:00 +00:00
|
|
|
|
2018-03-10 11:47:55 +00:00
|
|
|
function warn() {
|
2022-10-29 21:14:14 +00:00
|
|
|
log_level_matches 2 || return 0
|
|
|
|
echo -e "${COLOR_YELLOW}$@${COLOR_PLAIN}" >&2
|
2018-02-02 09:54:00 +00:00
|
|
|
}
|