improve readability of code in some complex branching points

avoid usage of if...elif...elif...else in some points, substituted
with while true; do ... done loops and break statements on success.
This commit is contained in:
Jaromil 2024-08-31 05:38:45 +02:00
parent c1b5e1b310
commit 117bd9bd6e

176
tomb
View File

@ -500,56 +500,54 @@ ask_password() {
pass_asked=0 pass_asked=0
if [[ ! -z $WAYLAND_DISPLAY ]]; then while true; do
_verbose "wayland display detected" [[ ! -z $WAYLAND_DISPLAY ]] && {
_is_found "pinentry-gnome3" && { _verbose "wayland display detected"
_verbose "using pinentry-gnome3 on wayland" _is_found "pinentry-gnome3" && {
output=$(pinentry_assuan_getpass | pinentry-gnome3) _verbose "using pinentry-gnome3 on wayland"
pass_asked=1 output=$(pinentry_assuan_getpass | pinentry-gnome3)
break; }
# TODO: pinentry on KDE running in wayland?
}
[[ ! -z $DISPLAY ]] && [[ $pass_asked == 0 ]] && {
_verbose "X11 display detected"
_is_found "pinentry-gtk-2" && {
_verbose "using pinentry-gtk2"
output=$(pinentry_assuan_getpass | pinentry-gtk-2)
break; }
_is_found "pinentry-x11" && {
_verbose "using pinentry-x11"
output=$(pinentry_assuan_getpass | pinentry-x11)
break; }
_is_found "pinentry-gnome3" && {
_verbose "using pinentry-gnome3 on X11"
output=$(pinentry_assuan_getpass | pinentry-gnome3)
break; }
_is_found "pinentry-qt5" && {
_verbose "using pinentry-qt5"
output=$(pinentry_assuan_getpass | pinentry-qt5)
break; }
_is_found "pinentry-qt4" && {
_verbose "using pinentry-qt4"
output=$(pinentry_assuan_getpass | pinentry-qt4)
break; }
} }
fi
if [[ ! -z $DISPLAY ]] && [[ $pass_asked == 0 ]]; then
_verbose "X11 display detected"
if _is_found "pinentry-gtk-2"; then
_verbose "using pinentry-gtk2"
output=$(pinentry_assuan_getpass | pinentry-gtk-2)
pass_asked=1
elif _is_found "pinentry-x11"; then
_verbose "using pinentry-x11"
output=$(pinentry_assuan_getpass | pinentry-x11)
pass_asked=1
elif _is_found "pinentry-gnome3"; then
_verbose "using pinentry-gnome3 on X11"
output=$(pinentry_assuan_getpass | pinentry-gnome3)
pass_asked=1
elif _is_found "pinentry-qt5"; then
_verbose "using pinentry-qt5"
output=$(pinentry_assuan_getpass | pinentry-qt5)
pass_asked=1
elif _is_found "pinentry-qt4"; then
_verbose "using pinentry-qt4"
output=$(pinentry_assuan_getpass | pinentry-qt4)
pass_asked=1
fi
fi
if [[ $pass_asked == 0 ]]; then
_verbose "no display detected" _verbose "no display detected"
if _is_found "pinentry-curses"; then _is_found "pinentry-curses" && {
_verbose "using pinentry-curses with no display" _verbose "using pinentry-curses with no display"
output=$(pinentry_assuan_getpass | pinentry-curses) output=$(pinentry_assuan_getpass | pinentry-curses)
pass_asked=1 break; }
elif _is_found "pinentry-tty"; then _is_found "pinentry-tty" && {
_verbose "using pinentry-tty with no display" _verbose "using pinentry-tty with no display"
output=$(pinentry_assuan_getpass | pinentry-tty) output=$(pinentry_assuan_getpass | pinentry-tty)
pass_asked=1 break; }
else # TODO: fallback using read -s - and beware
# TODO: fallback to asking password using read # using read with or without -r may break
_failure "Cannot find any pinentry and no DISPLAY detected." # passwords, so this must be covered by a test
fi # for compatibility
fi
[[ $pass_asked == 0 ]] &&
_failure "Cannot find any pinentry and no DISPLAY detected." _failure "Cannot find any pinentry and no DISPLAY detected."
exit 1
done
# parse the pinentry output # parse the pinentry output
local pinentry_error local pinentry_error
@ -589,26 +587,23 @@ is_valid_tomb() {
local _fail=0 local _fail=0
# Tomb file must be a readable, writable, non-empty regular file. # Tomb file must be a readable, writable, non-empty regular file.
# If passed the "ro" mount option, the writable check is skipped. # If passed the "ro" mount option, the writable check is skipped.
option_value_contains -o ro || { while true; do
[[ ! -w "$1" ]] && { option_value_contains -o ro || {
_warning "Tomb file is not writable: ::1 tomb file::" $1 [[ ! -w "$1" ]] && {
_fail=1 _warning "Tomb file is not writable: ::1 tomb file::" $1
_fail=1; break; }
} }
} _verbose "tomb file is readable"
_verbose "tomb file is readable" [[ ! -f "$1" ]] && {
_warning "Tomb file is not a regular file: ::1 tomb file::" $1
[[ ! -f "$1" ]] && { _fail=1; break; }
_warning "Tomb file is not a regular file: ::1 tomb file::" $1 _verbose "tomb file is a regular file"
_fail=1 [[ ! -s "$1" ]] && {
} _warning "Tomb file is empty (zero length): ::1 tomb file::" $1
_verbose "tomb file is a regular file" _fail=1; break; }
_verbose "tomb file is not empty"
[[ ! -s "$1" ]] && { break;
_warning "Tomb file is empty (zero length): ::1 tomb file::" $1 done
_fail=1
}
_verbose "tomb file is not empty"
[[ $_fail == 1 ]] && { [[ $_fail == 1 ]] && {
_failure "Tomb command failed: ::1 command name::" $subcommand _failure "Tomb command failed: ::1 command name::" $subcommand
} }
@ -922,12 +917,12 @@ _ensure_dependencies() {
command -v $req 1>/dev/null 2>/dev/null || { command -v $req 1>/dev/null 2>/dev/null || {
_failure "Missing required dependency ::1 command::. Please install it." $req; } _failure "Missing required dependency ::1 command::. Please install it." $req; }
done done
# Check for pinentry or at least pinentry-tty (which has no alias) # Check for pinentry or at least pinentry-tty (which has no alias)
if ! command -v pinentry 1>/dev/null 2>/dev/null; then if ! command -v pinentry 1>/dev/null 2>/dev/null; then
if ! command -v pinentry-tty 1>/dev/null 2>/dev/null; then if ! command -v pinentry-tty 1>/dev/null 2>/dev/null; then
_failure "Missing required dependency ::1 command::. Please install it." pinentry _failure "Missing required dependency ::1 command::. Please install it." pinentry
fi fi
fi fi
# Ensure system binaries are available in the PATH # Ensure system binaries are available in the PATH
path+=(/sbin /usr/sbin) # zsh magic path+=(/sbin /usr/sbin) # zsh magic
@ -1101,24 +1096,27 @@ _load_key() {
[[ -z $keyfile ]] && { [[ -z $keyfile ]] && {
_failure "This operation requires a key file to be specified using the -k option." } _failure "This operation requires a key file to be specified using the -k option." }
if [[ $keyfile == "-" ]]; then while true; do
_verbose "load_key reading from stdin." [[ $keyfile == "-" ]] && {
_message "Waiting for the key to be piped from stdin... " _verbose "load_key reading from stdin."
TOMBKEYFILE=stdin _message "Waiting for the key to be piped from stdin... "
TOMBKEY=$(cat) TOMBKEYFILE=stdin
elif [[ $keyfile == "cleartext" ]]; then TOMBKEY=$(cat)
_verbose "load_key reading SECRET from stdin" break; }
_message "Waiting for the key to be piped from stdin... " [[ $keyfile == "cleartext" ]] && {
TOMBKEYFILE=cleartext _verbose "load_key reading SECRET from stdin"
TOMBKEY=cleartext _message "Waiting for the key to be piped from stdin... "
TOMBSECRET=$(cat) TOMBKEYFILE=cleartext
else TOMBKEY=cleartext
TOMBSECRET=$(cat)
break; }
_verbose "load_key argument: ::1 key file::" $keyfile _verbose "load_key argument: ::1 key file::" $keyfile
[[ -r $keyfile ]] || _failure "Key not found, specify one using -k." [[ -r $keyfile ]] || _failure "Key not found, specify one using -k."
_track_stat "$keyfile" _track_stat "$keyfile"
TOMBKEYFILE=$keyfile TOMBKEYFILE=$keyfile
TOMBKEY="${mapfile[$TOMBKEYFILE]}" TOMBKEY="${mapfile[$TOMBKEYFILE]}"
fi break
done
_verbose "load_key: ::1 key::" $TOMBKEYFILE _verbose "load_key: ::1 key::" $TOMBKEYFILE
@ -2520,14 +2518,18 @@ exec_safe_bind_hooks() {
for dir in ${mounted}; do _sudo umount $dir; done for dir in ${mounted}; do _sudo umount $dir; done
return 0 } return 0 }
if [[ ! -r "$HOME/${maps[$dir]}" ]]; then while true; do
_warning "bind-hook target not existent, skipping ::1 home::/::2 subdir::" $HOME ${maps[$dir]} [[ ! -r "$mnt/$dir" ]] && {
elif [[ ! -r "$mnt/$dir" ]]; then _warning "bind-hook source not found in tomb, skipping ::1 mount point::/::2 subdir::" $mnt $dir
_warning "bind-hook source not found in tomb, skipping ::1 mount point::/::2 subdir::" $mnt $dir break; }
else [[ ! -r "$HOME/${maps[$dir]}" ]] && {
_sudo mount -o bind,$MOUNTOPTS $mnt/$dir $HOME/${maps[$dir]} \ _warning "bind-hook target not existent, skipping ::1 home::/::2 subdir::" $HOME ${maps[$dir]}
break; }
_sudo mount -o bind,$MOUNTOPTS \
$mnt/$dir $HOME/${maps[$dir]} \
&& mounted+=("$HOME/${maps[$dir]}") && mounted+=("$HOME/${maps[$dir]}")
fi break
done
done done
} }