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
partprobe
python3
?rhash
"?rhash"
sha512sum
sgdisk
uuidgen

View File

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