shell-base/script
Simon Vieille 02603c84b4
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
update doc
2023-05-21 15:27:36 +02:00

87 lines
1.5 KiB
Bash
Executable file

#!/bin/sh
set -eu
usage() {
printf "Usage: %s [-l DEBUG_LEVEL] [-h]\n" "$0"
}
help() {
cat << EOH
SYNOPSIS
$0 [-l DEBUG_LEVEL] [-h]
DESCRIPTION
$0 does things!
OPTIONS
-h Show this help
-l debug|info|notice|warning|error
Debug level
EOH
}
on_interrupt() {
print "Process aborted!\n"
exit 130
}
main() {
while getopts "l:h" option; do
case "${option}" in
h) help; exit 0;;
l) LOG_VERBOSE="$OPTARG";;
?) log -l error "$(usage)"; exit 1;;
esac
done
# log [-t] [-l debug|info|notice|warning|error] message
exit 0
}
log() {
LEVEL=info
TIME=
while getopts "tl:" option; do
case "${option}" in
l) LEVEL="$OPTARG"; shift $((OPTIND-1));;
t) TIME="$(printf "[%s] " "$(date +'%Y-%m-%dT%H:%m:%S.%s')")"; shift $((OPTIND-1));;
*) exit 1;;
esac
done
if [ -t 2 ] && [ -z "${NO_COLOR-}" ]; then
case "${LEVEL}" in
debug) COLOR="$(tput setaf 3)"; LEVEL=100;;
notice) COLOR="$(tput setaf 4)"; LEVEL=250;;
warning) COLOR="$(tput setaf 5)"; LEVEL=300;;
error) COLOR="$(tput setaf 1)"; LEVEL=400;;
*) COLOR="$(tput sgr0)"; LEVEL=200;;
esac
fi
LOG_VERBOSE="${LOG_VERBOSE:-info}"
case "${LOG_VERBOSE}" in
debug) LOG_VERBOSE_VALUE=100;;
notice) LOG_VERBOSE_VALUE=250;;
warning) LOG_VERBOSE_VALUE=300;;
error) LOG_VERBOSE_VALUE=400;;
*) LOG_VERBOSE_VALUE=200;;
esac
if [ $LEVEL -ge $LOG_VERBOSE_VALUE ]; then
printf "%s%s%s\n" "${COLOR:-}" "${TIME:-}" "$*" >&2
fi
}
trap on_interrupt INT
main "$@"