add logger
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Simon Vieille 2023-05-21 14:56:42 +02:00
parent 374ac3aeb9
commit 9241f0abb5
Signed by: deblan
GPG key ID: 579388D585F70417

29
script
View file

@ -1,5 +1,7 @@
#!/bin/sh
set -eu
usage() {
printf "Usage: %s [-h]\n" "$0"
}
@ -30,15 +32,40 @@ main() {
case "${option}" in
h) help; exit 0;;
f) FOO="$OPTARG";;
*) usage; exit 1;;
?) log -l error "$(usage)"; exit 1;;
esac
done
FOO="${FOO:-defaultValue}"
# log [-t] [-l debug|error|info] 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)";;
error) COLOR="$(tput setaf 1)";;
*) COLOR="$(tput sgr0)"
esac
fi
printf "%s%s%s\n" "${COLOR:-}" "${TIME:-}" "$*" >&2
}
trap on_interrupt INT
main "$@"