diff --git a/script b/script index 935c8ae..9fb1d52 100755 --- a/script +++ b/script @@ -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 "$@"