summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2018-06-01 15:41:52 (EDT)
committer P. J. McDermott <pj@pehjota.net>2018-06-03 23:40:52 (EDT)
commit9520b945203d830bc32885a8187296825ab38747 (patch)
tree40e702bd9e1b2f9791daf217260055325a842b4a
parent573a0a5ff1489d0ae3c582b844a80b9f693b4c71 (diff)
downloadhomerc-9520b945203d830bc32885a8187296825ab38747.zip
homerc-9520b945203d830bc32885a8187296825ab38747.tar.gz
homerc-9520b945203d830bc32885a8187296825ab38747.tar.bz2
bin/svc: Add action msgs and fix default restart func
And wrap function calls in conditionals.
-rwxr-xr-xbin/svc83
1 files changed, 65 insertions, 18 deletions
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