Add check_utils helper

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2022-10-29 23:14:14 +02:00
parent 00cd75cf61
commit b729c38fcb
Signed by: luzifer
GPG key ID: D91C3E91E4CAD6F5

View file

@ -6,55 +6,61 @@ COLOR_PLAIN="\033[0m"
COLOR_PURPLE="\033[35m"
function check_util() {
command -v ${1} >/dev/null 2>&1 || fail "Missing ${1} util"
command -v ${1} >/dev/null 2>&1 || fail "Missing ${1} util"
}
function check_utils() {
for util in "$@"; do
check_util "${util}"
done
}
function debug() {
log_level_matches 0 || return 0
echo -e "${COLOR_PURPLE}$@${COLOR_PLAIN}"
log_level_matches 0 || return 0
echo -e "${COLOR_PURPLE}$@${COLOR_PLAIN}"
}
function error() {
log_level_matches 3 || return 0
echo -e "${COLOR_RED}$@${COLOR_PLAIN}" >&2
log_level_matches 3 || return 0
echo -e "${COLOR_RED}$@${COLOR_PLAIN}" >&2
}
function fail() {
error "$@"
exit 1
error "$@"
exit 1
}
function fatal() {
fail "$@"
fail "$@"
}
function info() {
log_level_matches 1 || return 0
echo -e "${COLOR_CYAN}$@${COLOR_PLAIN}" >&2
log_level_matches 1 || return 0
echo -e "${COLOR_CYAN}$@${COLOR_PLAIN}" >&2
}
function log_level_matches() {
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
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
}
function step() {
info "[$(date +%H:%M:%S)] $(printf "%${script_level:-0}s" '' | tr ' ' '+')$@"
info "[$(date +%H:%M:%S)] $(printf "%${script_level:-0}s" '' | tr ' ' '+')$@"
}
function success() {
log_level_matches 1 || return 0
echo -e "${COLOR_GREEN}$@${COLOR_PLAIN}" >&2
log_level_matches 1 || return 0
echo -e "${COLOR_GREEN}$@${COLOR_PLAIN}" >&2
}
function warn() {
log_level_matches 2 || return 0
echo -e "${COLOR_YELLOW}$@${COLOR_PLAIN}" >&2
log_level_matches 2 || return 0
echo -e "${COLOR_YELLOW}$@${COLOR_PLAIN}" >&2
}