From 67231312ba2d80592e305b08bbf03f7aece2e4a3 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 14 Jun 2026 15:17:32 +0300 Subject: lm0 de-duplicate imagemagick detection and config-array parsing Introduce a shared resolve_config_array helper in bootstrap.source.sh that parses a config variable declared as either a Bash array or a whitespace-separated scalar into a named output array. resolve_tar_opts and resolve_sync_destinations now both delegate to it, with resolve_tar_opts keeping its "-c" default for the empty/unset case. validate_imagemagick now reuses resolve_imagemagick_command instead of duplicating the magick/convert probing, reporting failures through config_error so the validation output is unchanged. Add a print-config test covering the empty TAR_OPTS (scalar and array) fallback to the default. Co-Authored-By: Claude Opus 4.8 --- src/lib/bootstrap.source.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'src/lib/bootstrap.source.sh') diff --git a/src/lib/bootstrap.source.sh b/src/lib/bootstrap.source.sh index 244cd55..67ba090 100644 --- a/src/lib/bootstrap.source.sh +++ b/src/lib/bootstrap.source.sh @@ -60,6 +60,48 @@ log_warning() { printf 'WARNING: %s\n' "$*" >&2 } +# Read a configuration value into the named array, accepting both Bash array +# and whitespace-separated scalar declarations of the same variable. +# This is shared by resolve_tar_opts and resolve_sync_destinations so the +# "array or scalar config" parsing lives in exactly one place. +# +# Arguments: +# $1 name of the source config variable (e.g. TAR_OPTS) +# $2 name of the destination array variable (nameref) +# Returns: +# 0 if the variable was declared (the destination may still be empty), +# 1 if the variable was not declared at all (lets callers apply defaults). +resolve_config_array() { + local -r config_var="$1"; shift + local -n config_array_ref="$1"; shift + local config_decl + + config_array_ref=() + + # declare -p fails when the variable was never set; callers use the + # non-zero return to distinguish "unset" from "set but empty". + if ! config_decl=$(declare -p "$config_var" 2>/dev/null); then + return 1 + fi + + case "$config_decl" in + declare\ -a*\ "$config_var"=*) + # Already a real array: copy it element by element. + local -n config_source_ref="$config_var" + # shellcheck disable=SC2034 + config_array_ref=("${config_source_ref[@]}") + ;; + *) + # Scalar string: word-split it into the destination array. + local -n config_scalar_ref="$config_var" + if [ -n "${config_scalar_ref:-}" ]; then + # shellcheck disable=SC2034 + read -r -a config_array_ref <<< "$config_scalar_ref" + fi + ;; + esac +} + resolve_default_rc_file() { local source_root -- cgit v1.2.3