aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWynn Wolf Arbor2020-05-13 15:59:02 +0200
committerWynn Wolf Arbor2020-05-13 15:59:02 +0200
commit1d79b6782a521e1712f29362bc8d7793c0a73402 (patch)
treed91a762594604d746d41275647f542b3a3c19324
parentebec2da1e00f228437f069a7c22045ffd915d37b (diff)
downloadbosun-1d79b6782a521e1712f29362bc8d7793c0a73402.tar.gz
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.
Diffstat (limited to '')
-rwxr-xr-xbosun35
1 files 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