From 9520b945203d830bc32885a8187296825ab38747 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Fri, 01 Jun 2018 15:41:52 -0400 Subject: bin/svc: Add action msgs and fix default restart func And wrap function calls in conditionals. --- (limited to 'bin/svc') diff --git a/bin/svc b/bin/svc index 5587c6d..df12e4a 100755 --- a/bin/svc +++ b/bin/svc @@ -2,6 +2,16 @@ set -eu +msg_start='[....] Starting %s... ' +msg_stop='[....] Stopping %s... ' +msg_restart='[....] Restarting %s... ' +msg_start_ok='[ ok ] Starting %s... done' +msg_stop_ok='[ ok ] Stopping %s... done' +msg_restart_ok='[ ok ] Restarting %s... done' +msg_start_fail='[fail] Starting %s... failed' +msg_stop_fail='[fail] Stopping %s... failed' +msg_restart_fail='[fail] Restarting %s... failed' + XDG_DATA_HOME= XDG_CONFIG_HOME= XDG_DATA_DIRS= @@ -10,8 +20,6 @@ XDG_CACHE_HOME= PIDDIR= SERVICES= -# TODO: "[....] service" output if attached to a tty - load_service() { local service="${1}" @@ -26,20 +34,52 @@ load_service() # Default functions stop() { :; } - restart() { stop; start; } + restart() { stop && start; } status() { :; } # TODO: Errors - . "${XDG_CONFIG_HOME}/homerc/${service}" + . "${XDG_CONFIG_HOME}/homerc/${service}" || return 1 + return 0 +} + +log() +{ + local service="${1}" + local action="${2}" + local var="${3}" + shift 3 + + if ! tty 1>/dev/null 2>&1; then + return 0 + fi + + case "${action}" in 'start' | 'stop' | 'restart') + eval "printf \"\\r\${msg_${action}${var}}\" \"\${service}\"" + esac + case "${var}" in ?*) + printf '\n' + esac + + return 0 } act() { - local action="${1}" - shift 1 + local service="${1}" + local action="${2}" + shift 2 + local es= + + log "${service}" "${action}" '' - "${action}" - return ${?} + if "${action}"; then + log "${service}" "${action}" '_ok' + return 0 + else + es=${?} + log "${service}" "${action}" '_fail' + return ${es} + fi } match_session() @@ -64,9 +104,10 @@ match_session() chg_sessions() { - local old_sessions="${1}" - local new_sessions="${2}" - shift 2 + local service="${1}" + local old_sessions="${2}" + local new_sessions="${3}" + shift 3 local is_running= local should_run= @@ -82,15 +123,16 @@ chg_sessions() done if ${is_running} && ! ${should_run}; then - act 'stop' - return ${?} + if ! act "${service}" 'stop'; then + return ${?} + fi fi if ! ${is_running} && ${should_run}; then - act 'start' - return ${?} + if ! act "${service}" 'start'; then + return ${?} + fi fi - # No change. return 0 } @@ -116,11 +158,16 @@ main() case "${action}" in 'start' | 'stop' | 'restart' | 'status') load_service "${service}" || return 1 - act "${action}" + if ! act "${service}" "${action}"; then + return ${?} + fi ;; *';'*) load_service "${service}" || return 1 - chg_sessions "${action%%;*}" "${action#*;}" + if ! chg_sessions "${service}" \ + "${action%%;*}" "${action#*;}"; then + return ${?} + fi ;; *) usage 1>&2 -- cgit v0.9.1