Improve service state utility in femto-utils.sh

This commit is contained in:
nagumii 2025-02-08 19:05:19 +02:00
parent 3b520405ff
commit b543a11898
3 changed files with 34 additions and 65 deletions

View file

@ -549,13 +549,13 @@ meshtastic_settings() {
local service_state="$(femto-utils.sh -R "$(femto-utils.sh -C "meshtasticd")")"
choice=$(dialog --no-collapse --colors --cancel-label "Back" --title "Meshtasticd Service" --default-item "$choice" --menu "Meshtasticd service is $service_state$(femto-meshtasticd-config.sh -k | grep -q 'simulated' && echo '\n\Z1Using simulated radio\Zn')" 17 52 5 \
"View detailed service status" "x" \
" " " " \
$(if echo "$service_state" | grep -q 'enabled'; then echo "Start/restart service x"; fi) \
$(if echo "$service_state" | grep -q 'enabled and running'; then echo "Stop service x"; fi) \
$(if echo "$service_state" | grep -q 'enabled'; then echo "   "; fi) \
" " "" \
"Start/restart service" "x" \
$(if ! echo "$service_state" | grep -q 'not running'; then echo "Stop service x"; fi) \
" " "" \
$(if echo "$service_state" | grep -q 'disabled'; then echo "Enable service x"; fi) \
$(if echo "$service_state" | grep -q 'enabled'; then echo "Disable service x"; fi) \
" " " " \
" " "" \
"Back to previous menu" "x" 3>&1 1>&2 2>&3)
[ $? -eq 1 ] && break # Exit the loop if the user selects "Cancel" or closes the dialog
case $choice in
@ -567,14 +567,14 @@ meshtastic_settings() {
dialog --no-collapse --title "$title" --yesno "Start/restart Meshtasticd service?" 6 45
if [ $? -eq 0 ]; then #unless cancel/no
femto-meshtasticd-config.sh -s
dialog --no-collapse --msgbox "Meshtasticd service started/restarted." 6 45
dialog --no-collapse --colors --msgbox "Sent \`\Zusystemctl restart meshtasticd\Zn\` command to system.\n\nMeshtasticd should be running." 9 50
fi
;;
"Stop service")
dialog --no-collapse --colors --title "$title" --yesno "Stop Meshtasticd service?\n\nThis will take the node \Zu\Z1OFFLINE\Zn.\n\nMeshtasticd will automatically restart on boot if not disabled." 11 50
dialog --no-collapse --colors --title "$title" --yesno "Stop Meshtasticd service?\n\nThis will take the node \Zu\Z1OFFLINE\Zn.\n\nMeshtasticd will automatically restart on boot service if enabled." 11 50
if [ $? -eq 0 ]; then #unless cancel/no
femto-meshtasticd-config.sh -t
dialog --no-collapse --msgbox "Meshtasticd service stopped." 6 45
dialog --no-collapse --colors --msgbox "Sent \`\Zusystemctl stop meshtasticd\Zn\` command to system.\n\nMeshtasticd should be stopped." 9 50
fi
;;
"Enable service")
@ -588,7 +588,7 @@ meshtastic_settings() {
dialog --no-collapse --colors --title "$title" --yesno "Disable Meshtasticd service?\n\nThis will take the node \Zu\Z1OFFLINE\Zn and will not restart!" 9 50
if [ $? -eq 0 ]; then #unless cancel/no
femto-meshtasticd-config.sh -M "disable"
dialog --no-collapse --colors --msgbox "Sent \`\Zusystemctl disable meshtasticd\Zn\` and \`\Zusystemctl stop meshtasticd\Zn\` commands to system.\n\nMeshtasticd is disabled and stopped." 10 50
dialog --no-collapse --colors --msgbox "Sent \`\Zusystemctl disable meshtasticd\Zn\` and \`\Zusystemctl stop meshtasticd\Zn\` commands to system.\n\nMeshtasticd should be disabled and stopped." 10 50
fi
;;
"Back to previous menu") break ;;

View file

@ -98,8 +98,8 @@ package_menu() {
$(if $package_dir/$1.sh -O | grep -q 'g' && $package_dir/$1.sh -I; then echo "Upgrade x"; fi) \
$(if $package_dir/$1.sh -O | grep -q 'e' && $package_dir/$1.sh -I && [[ ! $service_state =~ "enabled" ]]; then echo "Enable service x"; fi) \
$(if $package_dir/$1.sh -O | grep -q 'e' && $package_dir/$1.sh -I && [[ ! $service_state =~ "disabled" ]]; then echo "Disable service x"; fi) \
$(if $package_dir/$1.sh -O | grep -q 'e' && $package_dir/$1.sh -I && [[ ! $service_state =~ "not running" ]] && [[ ! $service_state =~ "disabled" ]]; then echo "Stop service x"; fi) \
$(if $package_dir/$1.sh -O | grep -q 'e' && $package_dir/$1.sh -I && [[ $service_state =~ "enabled" ]]; then echo "Start/restart service x"; fi) \
$(if $package_dir/$1.sh -O | grep -q 'e' && $package_dir/$1.sh -I && [[ ! $service_state =~ "not running" ]]; then echo "Stop service x"; fi) \
$(if $package_dir/$1.sh -O | grep -q 'e' && $package_dir/$1.sh -I; then echo "Start/restart service x"; fi) \
$(if $package_dir/$1.sh -O | grep -q 'S' && $package_dir/$1.sh -I; then echo "Detailed service status x"; fi)"
menu_count=$(( $(echo "$menu_list" | grep -o " x" | wc -l) $(if $package_dir/$1.sh -O | grep -q 'e' && $package_dir/$1.sh -I; then echo "+1"; fi) )) # count the number of menu items by counting how many times " x" shows up, +1 if there's a service. This is a stupid way to do this, but because each menu item only contains one space (the rest being space-sized invisible chars) it works. It's late at night, OKAY?!
choice=$(dialog --no-collapse --colors --title "$($package_dir/$1.sh -N)" --cancel-label "Back" --default-item "$choice" $license_button --menu "$(if $package_dir/$1.sh -O | grep -q 'S' && $package_dir/$1.sh -I; then echo "Service status: $(femto-utils.sh -R "$service_state")"; fi)" $(( menu_count + 9 )) 45 $(( menu_count + 3 )) \

View file

@ -214,45 +214,11 @@ replace_colors() {
input="${input//$(echo -e '\033[0;37m')/\\Z7}" # white
input="${input//$(echo -e '\033[7m')/\\Zr}" # invert
input="${input//$(echo -e '\033[4m')/\\Zu}" # underline
input="${input//$(echo -e '\033[0m')/\\Zn}" # reset
input="${input//$(echo -e '\e[39m')/\\Z0}" # reset colors not underline
input="${input//$(echo -e '\033[0m')/\\Zn}" # reset all
echo "$input"
}
# this has been moved to /usr/local/bin/packages/ttyd.sh under the aegis of femto_software.sh
# ttyd() {
# if [ "$1" = "disable" ]; then
# systemctl disable ttyd
# systemctl stop ttyd
# echo "Disabled ttyd service."
# exit 0
# elif [ "$1" = "enable" ]; then
# systemctl enable ttyd
# echo "Enabled ttyd service."
# systemctl start ttyd
# exit 0
# elif [ "$1" = "check" ]; then
# if systemctl is-enabled ttyd &>/dev/null; then
# if echo "$(systemctl status ttyd)" | grep -q "active (running)"; then
# echo -e "\033[4m\033[0;34menabled and running\033[0m"
# elif echo "$(systemctl status ttyd)" | grep -q "inactive (dead)"; then
# echo -e "\033[4m\033[0;31menabled but not running\033[0m"
# exit 1
# else
# echo "unknown"
# fi
# else
# echo -e "\033[4m\033[0;31mdisabled\033[0m"
# exit 1
# fi
# elif [ "$1" = "start" ]; then
# echo "Starting/restarting ttyd service..."
# systemctl restart ttyd
# elif [ "$1" = "stop" ]; then
# echo "Stopping ttyd service..."
# systemctl stop ttyd
# fi
# }
while getopts ":harsl:ipcnoSEC:R:" opt; do
case ${opt} in
h) # Option -h (help)
@ -289,26 +255,29 @@ while getopts ":harsl:ipcnoSEC:R:" opt; do
;;
C) # Option -C (Check service)
if systemctl is-enabled $OPTARG &>/dev/null; then
if echo "$(systemctl status $OPTARG)" | grep -q "active (running)"; then
echo -e "\033[4m\033[0;34menabled and running\033[0m"
exit 0
elif echo "$(systemctl status $OPTARG)" | grep -q "inactive (dead)"; then
echo -e "\033[4m\033[0;31menabled but not running\033[0m"
exit 1
elif echo "$(systemctl status $OPTARG)" | grep -q "failed"; then
echo -e "\033[4m\033[0;31menabled but failed\033[0m"
exit 1
elif echo "$(systemctl status $OPTARG)" | grep -q "activating"; then
echo -e "\033[4m\033[0;31menabled, activating\033[0m"
exit 2
else
echo "unknown"
exit 2
fi
state="\033[0;34m\033[4menabled\e[39m, "
else
echo -e "\033[4m\033[0;31mdisabled\033[0m"
exit 1
state="\033[0;31m\033[4mdisabled\e[39m, "
exit_state=1
fi
if echo "$(systemctl status $OPTARG)" | grep -q "active (running)"; then
state+="\033[0;34mrunning\e[0m"
exit_state=0
elif echo "$(systemctl status $OPTARG)" | grep -q "inactive (dead)"; then
state+="\033[0;31mnot running\e[0m"
exit_state=1
elif echo "$(systemctl status $OPTARG)" | grep -q "failed"; then
state+="\033[0;31mfailed\e[0m"
exit_state=1
elif echo "$(systemctl status $OPTARG)" | grep -q "activating"; then
state+="activating\e[0m"
exit_state=2
else
state+="unknown\e[0m"
exit_state=2
fi
echo -e "$state"
exit $exit_state
;;
R)
replace_colors "$OPTARG"