nicer exit code handling

Signed-off-by: fbt <fbt@fleshless.org>
This commit is contained in:
Jack L. Frost 2016-11-17 13:11:11 +03:00
parent 0bfb6df3e1
commit 9fc3d74501

85
ssm
View File

@ -251,6 +251,22 @@ super_stop() {
fi 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 # Overloadable functions
start() { super_start; } start() { super_start; }
stop() { super_stop; } stop() { super_stop; }
@ -386,70 +402,49 @@ main() {
# Run the function # Run the function
case "$2" in case "$2" in
stop) stop)
printf 'Stopping %s... ' "$service_name"
stop; res=$? stop; res=$?
case "$res" in result "$res" \
0) printf 'ok.\n';; 0 "Stopped $service_name" \
3) printf 'not running.\n' "$service_name";; 3 "$service_name is not running" \
5) printf 'timed out.\n';; 5 "Operation timed out"
*) printf 'fail.\n';;
esac
;; ;;
start) start)
printf 'Starting %s... ' "$service_name"
start; res=$? start; res=$?
case "$res" in result "$res" \
0) printf 'ok.\n';; 0 "Started $service_name" \
3) printf 'already running.\n';; 3 "$service_name is already running" \
5) printf 'readyness check timed out.\n';; 5 "Readyness check for $service_name timed out" \
7) printf 'dependencies failed: %s.\n' "${failed_deps[@]}";; 7 "Failed to start dependencies for $service_name: ${failed_deps[@]}" \
9) printf 'service_command does not exist: %s.\n' "${service_command[0]}";; 9 "service_command does not exist: ${service_command[0]}" \
13) printf '%s: failed to create temporary files.\n';; 13 "Failed to create temporary files for $service_name"
*) printf 'fail.\n';;
esac
;; ;;
reload) reload)
printf 'Reloading %s... ' "$service_name"
reload; res=$? reload; res=$?
case "$res" in result "$res" \
0) printf 'ok.\n';; 0 "Reloaded $service_name"
*) printf 'fail.\n';;
esac
;; ;;
status) status)
status; res=$? status; res=$?
case "$res" in if (( service_oneshot )); then
0) result "$res" \
if (( service_oneshot )); then 0 "$service_name is enabled" \
printf '%s is enabled.\n' "$service_name" 1 "$service_name is not enabled"
else else
printf '%s is running.\n' "$service_name" result "$res" \
fi;; 0 "$service_name is running" \
1 "$service_name is not running"
1) fi
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
;; ;;
logs) logs;; *)
"$2"; res=$?;;
*) "$2"; res=$?;;
esac esac
(( res )) && return "$res" (( res )) && return "$res"