From 9405b1fc9984e43b0297d2bb89dea1666e1f4853 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Tue, 19 May 2026 12:30:07 -0500 Subject: refactor: extract validate_encryption_passphrase from gather_input MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit gather_input's unattended branch had two parallel if-blocks, one for ZFS and one for Btrfs, each doing the same encryption-passphrase empty check against a filesystem-specific variable (ZFS_PASSPHRASE or LUKS_PASSPHRASE). The two blocks shared the condition surface and error template. Only the variable name differed. I lifted the check into validate_encryption_passphrase in lib/config.sh next to validate_filesystem. The helper takes the variable name and uses indirect expansion (${!var_name}) so one function covers both filesystems. gather_input now dispatches via if/elif on FILESYSTEM and calls the helper with the right variable, collapsing 14 lines to 6. The original tests in test_archangel.bats (gather_input errors when ZFS without ZFS_PASSPHRASE / when Btrfs without LUKS_PASSPHRASE / accepts ZFS with NO_ENCRYPT=yes) still pass, exercising the helper through the dispatch. Added 4 direct unit tests in test_config.bats covering the four cases: NO_ENCRYPT=yes passes regardless, NO_ENCRYPT=no with empty fails, NO_ENCRYPT=no with value passes, and the error message names the offending variable. Bats: 177 → 181. No behavior change. The helper preserves the original error message format and exit conditions. --- installer/lib/config.sh | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'installer/lib') diff --git a/installer/lib/config.sh b/installer/lib/config.sh index a241e84..3ba2bb3 100644 --- a/installer/lib/config.sh +++ b/installer/lib/config.sh @@ -146,3 +146,16 @@ validate_filesystem() { error "Invalid FILESYSTEM: $FILESYSTEM (must be 'zfs' or 'btrfs')" fi } + +# Ensure an encryption passphrase variable is set when encryption is +# on. Takes the variable name (ZFS_PASSPHRASE or LUKS_PASSPHRASE) and +# errors out if NO_ENCRYPT is not "yes" and the named variable is +# empty. Indirect expansion (${!var_name}) lets one helper handle both +# ZFS and Btrfs passphrase fields without duplicating the conditional +# in gather_input's filesystem dispatch. +validate_encryption_passphrase() { + local var_name="$1" + if [[ "$NO_ENCRYPT" != "yes" && -z "${!var_name}" ]]; then + error "Config missing required: ${var_name} (or set NO_ENCRYPT=yes)" + fi +} -- cgit v1.2.3