#!/bin/bash set -eu # shellcheck disable=2034 description="service manager" export FUZL_PROMPT=svcm export FUZL_ICON=󰲽 . libfuzl.sh SVCM_SCOPE=usr main() { fuzl_action_add "usr" "󰀄" "User" fuzl_action_add "sys" "󰟀" "System" fuzl_action_menu || return select_service } # # helpers # select_service() { local rl rl=$(runlevel_current) local srv srv=$(get_services | FUZL_PROMPT="$FUZL_PROMPT [$rl]" fuzl_menu $FUZL_MENU_FILTER || true) test -n "$srv" || main if [[ "$srv" = runlevel:* ]]; then runlevel_start "$(cut -c10- <<<"$srv")" return fi fuzl_action_add "restart" "󰜉" "Restart" fuzl_action_add "start" "󰈸" "Start" fuzl_action_add "stop" "" "Stop" fuzl_action_menu "$srv" || select_service } get_services() { case "${SVCM_SCOPE:?missing scope}" in usr) fuzl_try -- rc-service -U -l | sort -u find ~/.config/rc/runlevels/ -mindepth 1 -type d -printf 'runlevel:%P\n' ;; sys) fuzl_try -- rc-service -l | sort -u find /etc/runlevels/ -mindepth 1 -type d -printf 'runlevel:%P\n' ;; esac } runlevel_current() { case "${SVCM_SCOPE:?missing scope}" in usr) fuzl_try -- rc-status -U -r ;; sys) fuzl_try -- rc-status -r ;; esac } runlevel_start() { local rl=${1:?missing runlevel} case "${SVCM_SCOPE:?missing scope}" in usr) fuzl_try -- openrc -U "$rl" ;; sys) fuzl_try -- doas -n openrc "$rl" ;; esac } # # actions # svcm_usr() { SVCM_SCOPE=usr } svcm_sys() { SVCM_SCOPE=sys } svcm_start() { local srv=${1:?missing service} case "${SVCM_SCOPE:?missing scope}" in usr) fuzl_try -- rc-service -U "$srv" start ;; sys) fuzl_try -- doas -n rc-service "$srv" start ;; esac } svcm_stop() { local srv=${1:?missing service} case "${SVCM_SCOPE:?missing scope}" in usr) fuzl_try -- rc-service -U "$srv" stop ;; sys) fuzl_try -- doas -n rc-service "$srv" stop ;; esac } svcm_restart() { local srv=${1:?missing service} case "${SVCM_SCOPE:?missing scope}" in usr) fuzl_try -- rc-service -U "$srv" restart ;; sys) fuzl_try -- doas -n rc-service "$srv" stop ;; esac } main