Commit 5b0c59dd authored by Seblu's avatar Seblu
Browse files

checkservice: imrpove confirm function

- more generic (not only for restart)
- use local vars
- infinite loop protection
parent f05c6f03
Loading
Loading
Loading
Loading
+21 −19
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ if [[ -t 1 ]]; then
fi

# default options
confirm=1			# confirm before restart
autoconfirm=0		# autoconfirmation
dbus=1				# relauch when dbus
debug=0				# debug mode
failed=1			# display failed service at the end
@@ -55,8 +55,8 @@ usage() {
	echo 'options:'
	echo '  -h: this help' >&2
	echo "  -d: debug mode" >&2
	echo "  -c: auto confirmation" >&2
	echo "  -b/-B: restart (or not) ${0##*/} if dbus was updated (default: $dbus)" >&2
	echo "  -c/-C: ask (or not) confirmation before restart" >&2
	echo "  -l/-L: call (or not) systemd daemon-reload (default: $reload)" >&2
	echo "  -f/-F: display (or not) failed services before quit (default: $failed)" >&2
	echo "  -p/-P: call (or not) pacdiff before act (default: $pacdiff)" >&2
@@ -89,26 +89,28 @@ in_array() {
	return 1 # Not Found
}

# ask for confirmation before restarting services
confirm_restart() {
	if (( $confirm == 1 )); then
		while true; do
			printf 'Confirm services restart? [y|N] '
# ask for confirmation
# return 0 when confirmed, otherwise 1
confirm() {
	(( $autoconfirm == 1 )) && return 0
	local -i try
	local ans
	for try in 5 4 3 2 1; do
		printf '%s [y|N] ' "$1"
		read -r ans || return 1
		case $ans in
			y|Y|yes|Yes) return 0;;
			n|N|no|No) return 1;;
		esac
	done
	else
		return 0
	fi
	error "Too much invalid answer. Not confirmed."
	return 1
}

while getopts 'hBbCcdFfLlPpRrSsUuZz' opt; do
while getopts 'ahBbdFfLlPpRrSsUuZz' opt; do
	case $opt in
		a) autoconfirm=0;;
		B) dbus=0;;				b) dbus=1;;
		C) confirm=0;;			c) confirm=1;;
		d) debug=1;;
		F) failed=0;;			f) failed=1;;
		L) reload=0;;			l) reload=1;;
@@ -200,7 +202,7 @@ done
(( "${#needy[*]}" )) && echo '-------8<-------------------------------8<---------'

# start the dangerous action below
if (( $restart == 1 && ${#needy[*]} > 0 )) && confirm_restart; then
if (( $restart == 1 && ${#needy[*]} > 0 )) && confirm 'Confirm service restart?'; then

	declare -A registered_pids=()
	declare -a running_pids=()