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

81
ssm
View File

@ -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"
result "$res" \
0 "$service_name is enabled" \
1 "$service_name is not enabled"
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
result "$res" \
0 "$service_name is running" \
1 "$service_name is not running"
fi
;;
logs) logs;;
*) "$2"; res=$?;;
*)
"$2"; res=$?;;
esac
(( res )) && return "$res"