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 #!/bin/bash
validate_arguments ()
die ()
{ {
if [ "$1" != "0" ]; then echo "$MY_NAME: $1"
exit 1 exit 1
fi
} }
parse_show () MY_NAME="${0##*/}"
{
PARSED_ARGUMENTS=$(getopt -a -n systemctl -o p: --long properties:,values -- "$@")
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" eval set -- "$PARSED_ARGUMENTS"
while : while :
do do
case "$1" in case "$1" in
-p | --properties) -t | --type)
shift 2
;;
-p | --property)
IFS="," read -a PROPERTIES <<< $2 IFS="," read -a PROPERTIES <<< $2
shift 2 shift 2
;; ;;
--values) -a | --all)
shift
;;
--value)
shift
;;
--no-legend)
shift
;;
--plain)
shift shift
;; ;;
--) --)
shift; shift;
UNITS=($@)
break break
;; ;;
*) *)
echo "systemctl: unexpected option: $1 - this should not happen." die "unexpected option: $1 - this should not happen."
exit 2
;; ;;
esac esac
done done
COMMAND="$1"
shift
UNITS=($@)
case "$COMMAND" in
show)
for UNIT in "${UNITS[@]}" for UNIT in "${UNITS[@]}"
do do
for PROPERTY in "${PROPERTIES[@]}" for PROPERTY in "${PROPERTIES[@]}"
@ -56,71 +71,25 @@ parse_show ()
done done
echo "" echo ""
done 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) 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 if [ "$UNIT" = "klipper" ]; then
sudo /usr/bin/supervisorctl "$COMMAND" simulavr klipper sudo /usr/bin/supervisorctl "$COMMAND" simulavr klipper
else else
sudo /usr/bin/supervisorctl "$COMMAND" "$UNIT" sudo /usr/bin/supervisorctl "$COMMAND" "$UNIT"
fi fi
done
;; ;;
*) *)
echo "systemctl: unknown command '$COMMAND'" die "unknown command '$COMMAND'"
exit 1
;; ;;
esac esac