From 9fc3d74501689df08d36aa06db74be56f89002de Mon Sep 17 00:00:00 2001 From: fbt Date: Thu, 17 Nov 2016 13:11:11 +0300 Subject: [PATCH] nicer exit code handling Signed-off-by: fbt --- ssm | 85 +++++++++++++++++++++++++++++-------------------------------- 1 file changed, 40 insertions(+), 45 deletions(-) diff --git a/ssm b/ssm index 147be28..87f0fbb 100755 --- a/ssm +++ b/ssm @@ -251,6 +251,22 @@ super_stop() { fi } +result() { + declare rc=$1; shift + declare -A msgs + + while (( $# )); do + [[ "$2" ]] || return 1 + + msgs["$1"]="$2" + shift 2 + done + + [[ "${msgs[$rc]}" ]] || msgs["$rc"]="Failed!" + + printf '%s\n' "${msgs[$rc]}" +} + # Overloadable functions start() { super_start; } stop() { super_stop; } @@ -386,70 +402,49 @@ main() { # Run the function case "$2" in stop) - printf 'Stopping %s... ' "$service_name" - stop; res=$? - case "$res" in - 0) printf 'ok.\n';; - 3) printf 'not running.\n' "$service_name";; - 5) printf 'timed out.\n';; - *) printf 'fail.\n';; - esac + result "$res" \ + 0 "Stopped $service_name" \ + 3 "$service_name is not running" \ + 5 "Operation timed out" ;; start) - printf 'Starting %s... ' "$service_name" - start; res=$? - case "$res" in - 0) printf 'ok.\n';; - 3) printf 'already running.\n';; - 5) printf 'readyness check timed out.\n';; - 7) printf 'dependencies failed: %s.\n' "${failed_deps[@]}";; - 9) printf 'service_command does not exist: %s.\n' "${service_command[0]}";; - 13) printf '%s: failed to create temporary files.\n';; - *) printf 'fail.\n';; - esac + result "$res" \ + 0 "Started $service_name" \ + 3 "$service_name is already running" \ + 5 "Readyness check for $service_name timed out" \ + 7 "Failed to start dependencies for $service_name: ${failed_deps[@]}" \ + 9 "service_command does not exist: ${service_command[0]}" \ + 13 "Failed to create temporary files for $service_name" ;; reload) - printf 'Reloading %s... ' "$service_name" - reload; res=$? - case "$res" in - 0) printf 'ok.\n';; - *) printf 'fail.\n';; - esac + result "$res" \ + 0 "Reloaded $service_name" ;; status) status; res=$? - case "$res" in - 0) - if (( service_oneshot )); then - printf '%s is enabled.\n' "$service_name" - else - printf '%s is running.\n' "$service_name" - fi;; - - 1) - if (( service_oneshot )); then - printf '%s is not enabled.\n' "$service_name" - else - printf '%s is not running.\n' "$service_name" - fi;; - - *) printf '%s: status unknown.\n' "$service_name";; - esac + if (( service_oneshot )); then + result "$res" \ + 0 "$service_name is enabled" \ + 1 "$service_name is not enabled" + else + result "$res" \ + 0 "$service_name is running" \ + 1 "$service_name is not running" + fi ;; - logs) logs;; - - *) "$2"; res=$?;; + *) + "$2"; res=$?;; esac (( res )) && return "$res"