diff options
| author | Robert Günzler <r@gnzler.io> | 2022-11-30 15:51:11 +0100 |
|---|---|---|
| committer | Robert Günzler <r@gnzler.io> | 2022-11-30 15:51:11 +0100 |
| commit | c36a274f664062d562eab3dfa934bb527df3a132 (patch) | |
| tree | 2f846c2ead1708eb8c015b9f1de5a887b27b10ac /bin | |
| parent | 5c97a9979ee2eda55da44dff8f55a57997cb66d1 (diff) | |
bin/rcmenu: port to s6
Diffstat (limited to 'bin')
| -rwxr-xr-x | bin/rcmenu | 61 |
1 files changed, 45 insertions, 16 deletions
diff --git a/bin/rcmenu b/bin/rcmenu index 0fce6f4..06e940c 100755 --- a/bin/rcmenu +++ b/bin/rcmenu @@ -1,28 +1,57 @@ -#!/bin/sh +#!/bin/bash -set -e [ -n "$DEBUG" ] && set -x +set -u export MENU_PROMPT=" rc" +svdir=/run/s6-rc/servicedirs ico=/usr/share/icons/mdi/scalable/run-fast.svg +menu_exitcode=-1 -handle() { +__list() { + for sv in "$svdir"/*; do + printf "%s " "$(basename "$sv")" + doas -n s6-svstat "$sv" + done | column -t +} + +__handle() { while read -r line; do - svc=$(printf "%s" "${line}" | awk '{print $1}') - mode=$(printf "%s" "${line}" | awk '{print $2}') - act=restart - [ "${mode}" = "started" ] && act=stop - _outp=$(doas rc-service "${svc}" "${act}") - if [ $? = 0 ]; then - notify-send -i "${ico}" -a rc "${MENU_PROMPT}: ${svc}" "${_outp}" + sv=$(printf "%s" "${line}" | awk '{print $1}') + state=$(printf "%s" "${line}" | awk '{print $2}') + + case "$state" in + up) + msg=$(doas -n s6-rc -v2 -d change "$sv") + if [ $menu_exitcode -eq 18 ]; then + action=stopp + else + action=restart + sleep 0.5 + msg=$(doas -n s6-rc -v2 -u change "$sv") + fi + ;; + + down) + action=start + msg=$(doas -n s6-rc -v2 -u change "$sv") + ;; + + *) + msg=$(printf "unknown service state: %s\n" "$state") + false + ;; + esac + + if [ "$?" = 0 ]; then + notify-send -i "${ico}" -a rc "${sv}" "${action}ed" else - notify-send -i "${ico}" -a rc -u critical -t 30000 "${MENU_PROMPT}: ${svc}" "${act}ing ${svc} failed:\n<i>${_outp}</i>" + notify-send -i "${ico}" -a rc -u critical -t 30000 "${sv}" "${action}ing ${sv} failed:\n<i>${msg}</i>" fi - printf "%s\n" "${_outp}" + printf "%s\n" "${msg}" done } -rc-status --servicelist | - menu --list=10 | - tr -d '[' | tr -d ']' | - handle +svs=$(__list | menu --list=10) +menu_exitcode=$? +__handle <<<"$svs" |