chore: improve wanted program check (remove array indirection)

This commit is contained in:
oddlama 2022-05-14 00:48:42 +02:00
parent 64218c9a5c
commit 38bf1de80c
No known key found for this signature in database
GPG Key ID: 14EFE510775FE39A
2 changed files with 24 additions and 22 deletions

View File

@ -73,7 +73,7 @@ function prepare_installation_environment() {
ntpd ntpd
partprobe partprobe
python3 python3
?rhash "?rhash"
sha512sum sha512sum
sgdisk sgdisk
uuidgen uuidgen

View File

@ -351,11 +351,22 @@ function parse_arguments() {
fi fi
} }
# $1: array # $1: program
# $@: elements # $2: checkfile
function append_to_array() { function has_program() {
declare -n _target_array=$1; shift local program="$1"
_target_array+=("$@") local checkfile="$2"
if [[ -z "$checkfile" ]]; then
type "$program" &>/dev/null \
|| return 1
elif [[ "${checkfile:0:1}" == "/" ]]; then
[[ -e "$checkfile" ]] \
|| return 1
else
type "$checkfile" &>/dev/null \
|| return 1
fi
return 0
} }
function check_wanted_programs() { function check_wanted_programs() {
@ -367,25 +378,16 @@ function check_wanted_programs() {
for tuple in "$@"; do for tuple in "$@"; do
program="${tuple%%=*}" program="${tuple%%=*}"
checkfile="${tuple##*=}" checkfile="${tuple##*=}"
if [[ "$program" == "?"* ]]; then if ! has_program "${program#"?"}" "$checkfile"; then
program="${program#"?"}" if [[ "$program" == "?"* ]]; then
arr=missing_wanted missing_wanted+=("${program#"?"}")
else else
arr=missing_required missing_required+=("$program")
fi fi
if [[ -z "$checkfile" ]]; then
type "$program" &>/dev/null \
|| append_to_array "$arr" "$program"
elif [[ "${checkfile:0:1}" == "/" ]]; then
[[ -e "$checkfile" ]] \
|| append_to_array "$arr" "$program"
else
type "$checkfile" &>/dev/null \
|| append_to_array "$arr" "$program"
fi fi
done done
[[ "${#missing_required[@]}" -eq 0 && "${#missing_required[@]}" -eq 0 ]] \ [[ "${#missing_required[@]}" -eq 0 && "${#missing_wanted[@]}" -eq 0 ]] \
&& return && return
if [[ "${#missing_required[@]}" -gt 0 ]]; then if [[ "${#missing_required[@]}" -gt 0 ]]; then