From 1d79b6782a521e1712f29362bc8d7793c0a73402 Mon Sep 17 00:00:00 2001 From: Wynn Wolf Arbor Date: Wed, 13 May 2020 15:59:02 +0200 Subject: Improve error reporting This commit introduces several improvements to the way bosun reacts to and reports errors. Instead of doing nothing and exiting silently, alert the user and print a short usage synopsis if bosun does not understand a command it was passed. Also, alert the user and print available list types if the `list` command was invoked with a type it does not understand. Lastly, check for the existence of a role before trying to `add`, `remove`, or `rebuild` it. --- bosun | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/bosun b/bosun index c195176..cbc9718 100755 --- a/bosun +++ b/bosun @@ -2,20 +2,44 @@ export STOW_DIR=/etc/portage/stow +err() { + printf "bosun: %s\n" "$@" >&2 +} + +errx() { + err "$@" + exit 1 +} + +usage() { + printf "usage: bosun add role ...\n" >&2 + printf " bosun flush\n" >&2 + printf " bosun [list [type ...]]\n" >&2 + printf " bosun rebuild [role ...]\n" >&2 + printf " bosun remove [role ...]\n" >&2 + exit 1 +} + +need_role() { + test $# -gt 0 || errx "this command requires at least one role" + for dir in "$@"; do + test -d "$STOW_DIR/$dir" || errx "no such role '$dir' in $STOW_DIR" + done +} + need_root() { - if test "$(id -u)" -ne 0; then - printf 'This action requires superuser access.\n' - exit 1 - fi + test "$(id -u)" -eq 0 || errx "this action requires superuser access" } add() { need_root + need_role "$@" stow -S "$@" } remove() { need_root + need_role "$@" stow -D "$@" } @@ -27,6 +51,7 @@ flush() { rebuild() { need_root if test $# -gt 0; then + need_role "$@" stow -R "$@" else list | xargs -- stow -R @@ -39,6 +64,7 @@ list() { active) list_active;; all) list_all;; available) list_available;; + *) errx "no such list type '$subcmd'. Try: active, all, available" esac } @@ -71,4 +97,5 @@ case $cmd in list) list "$@";; rebuild) rebuild "$@";; remove) remove "$@";; + *) err "no such command '$cmd'"; usage;; esac -- cgit v1.2.3-2-gb3c3