refactor: simplify service_control script

This commit is contained in:
th33xitus 2022-02-17 21:54:38 +01:00
parent f98cc12e81
commit 69cec6bf2c
1 changed files with 76 additions and 107 deletions

View File

@ -1,40 +1,55 @@
#!/bin/bash
validate_arguments ()
die ()
{
if [ "$1" != "0" ]; then
echo "$MY_NAME: $1"
exit 1
fi
}
parse_show ()
{
PARSED_ARGUMENTS=$(getopt -a -n systemctl -o p: --long properties:,values -- "$@")
MY_NAME="${0##*/}"
validate_arguments $?
PARSED_ARGUMENTS="$(getopt -n "$MY_NAME" -o t:p:a --long type:,property:,all,value,no-legend,plain -- "$@")" || exit $?
eval set -- "$PARSED_ARGUMENTS"
while :
do
eval set -- "$PARSED_ARGUMENTS"
while :
do
case "$1" in
-p | --properties)
-t | --type)
shift 2
;;
-p | --property)
IFS="," read -a PROPERTIES <<< $2
shift 2
;;
--values)
-a | --all)
shift
;;
--value)
shift
;;
--no-legend)
shift
;;
--plain)
shift
;;
--)
shift;
UNITS=($@)
break
;;
*)
echo "systemctl: unexpected option: $1 - this should not happen."
exit 2
die "unexpected option: $1 - this should not happen."
;;
esac
done
done
COMMAND="$1"
shift
UNITS=($@)
case "$COMMAND" in
show)
for UNIT in "${UNITS[@]}"
do
for PROPERTY in "${PROPERTIES[@]}"
@ -56,71 +71,25 @@ parse_show ()
done
echo ""
done
exit 0
}
parse_list_units ()
{
PARSED_ARGUMENTS=$(getopt -a -n systemctl -o at --long all,type:,plain,no-legend -- "$@")
validate_arguments $?
eval set -- "$PARSED_ARGUMENTS"
while :
do
case "$1" in
-a | --all)
shift
;;
-t | --type)
shift 2
;;
--plain)
shift
;;
--no-legend)
shift
;;
--)
shift;
break
;;
*)
echo "systemctl: unexpected option: $1 - this should not happen."
exit 2
;;
esac
done
sudo /usr/bin/supervisorctl status | awk '{print $1".service\tloaded\tactive\t"tolower($2)"\t"$1" service"}'
exit 0
}
COMMAND="$1"
shift
case "$COMMAND" in
show)
parse_show "$@"
;;
list-units)
parse_list_units "$@"
sudo /usr/bin/supervisorctl status | awk '{print $1".service\tloaded\tactive\t"tolower($2)"\t"$1" service"}'
;;
start | stop | restart)
UNIT="$1"
start | stop | restart)
for UNIT in "${UNITS[@]}"
do
if [ "$UNIT" = "klipper" ]; then
sudo /usr/bin/supervisorctl "$COMMAND" simulavr klipper
else
sudo /usr/bin/supervisorctl "$COMMAND" "$UNIT"
fi
done
;;
*)
echo "systemctl: unknown command '$COMMAND'"
exit 1
die "unknown command '$COMMAND'"
;;
esac