refactor: streamline environment variable loading with dedicated function

This commit is contained in:
S
2026-03-01 08:40:25 -05:00
parent 6100d482d2
commit a34cc69681

View File

@@ -38,22 +38,22 @@ fi
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Load current values # Load current values
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
declare -A CURRENT_VALUES # Lookup a variable's current value from .env (bash 3.2 compatible — no assoc arrays)
while IFS= read -r line; do get_env_val() {
# Skip comments and blank lines local key="$1" default="${2:-}"
[[ "$line" =~ ^[[:space:]]*# ]] && continue local line val
[[ -z "$line" ]] && continue line=$(grep "^${key}=" "$ENV_FILE" 2>/dev/null | head -1) || true
# Extract KEY=VALUE (strip inline comments) if [[ -z "$line" ]]; then
if [[ "$line" =~ ^([A-Za-z_][A-Za-z0-9_]*)=(.*) ]]; then printf '%s' "$default"
key="${BASH_REMATCH[1]}" return 0
val="${BASH_REMATCH[2]}" fi
# Strip inline comment (everything after # preceded by spaces) val="${line#*=}"
# Strip inline comment
val="${val%%#*}" val="${val%%#*}"
# Trim trailing whitespace # Trim trailing whitespace
val="${val%"${val##*[![:space:]]}"}" val="${val%"${val##*[![:space:]]}"}"
CURRENT_VALUES["$key"]="$val" printf '%s' "$val"
fi }
done < "$ENV_FILE"
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Validation functions — sourced from lib/common.sh (validate_ip, validate_port, # Validation functions — sourced from lib/common.sh (validate_ip, validate_port,
@@ -87,7 +87,8 @@ prompt_var() {
fi fi
# Determine current value (from .env or default) # Determine current value (from .env or default)
local current="${CURRENT_VALUES[$var_name]:-$default}" local current
current=$(get_env_val "$var_name" "$default")
# Build prompt # Build prompt
local progress local progress
@@ -317,11 +318,11 @@ printf '%b║ Configuration Complete ║%b\n'
printf '%b╚══════════════════════════════════════════════════════════╝%b\n\n' "$C_GREEN" "$C_RESET" printf '%b╚══════════════════════════════════════════════════════════╝%b\n\n' "$C_GREEN" "$C_RESET"
printf '%bSummary:%b\n' "$C_BOLD" "$C_RESET" printf '%bSummary:%b\n' "$C_BOLD" "$C_RESET"
printf ' Unraid: %s@%s:%s\n' "${CURRENT_VALUES[UNRAID_SSH_USER]:-}" "${CURRENT_VALUES[UNRAID_IP]:-}" "${CURRENT_VALUES[UNRAID_SSH_PORT]:-22}" printf ' Unraid: %s@%s:%s\n' "$(get_env_val UNRAID_SSH_USER)" "$(get_env_val UNRAID_IP)" "$(get_env_val UNRAID_SSH_PORT 22)"
printf ' Fedora: %s@%s:%s\n' "${CURRENT_VALUES[FEDORA_SSH_USER]:-}" "${CURRENT_VALUES[FEDORA_IP]:-}" "${CURRENT_VALUES[FEDORA_SSH_PORT]:-22}" printf ' Fedora: %s@%s:%s\n' "$(get_env_val FEDORA_SSH_USER)" "$(get_env_val FEDORA_IP)" "$(get_env_val FEDORA_SSH_PORT 22)"
printf ' Gitea: %s (admin: %s, password: ****)\n' "${CURRENT_VALUES[GITEA_DOMAIN]:-}" "${CURRENT_VALUES[GITEA_ADMIN_USER]:-}" printf ' Gitea: %s (admin: %s, password: ****)\n' "$(get_env_val GITEA_DOMAIN)" "$(get_env_val GITEA_ADMIN_USER)"
printf ' Org: %s\n' "${CURRENT_VALUES[GITEA_ORG_NAME]:-}" printf ' Org: %s\n' "$(get_env_val GITEA_ORG_NAME)"
printf ' Repos: %s, %s, %s\n' "${CURRENT_VALUES[REPO_1_NAME]:-}" "${CURRENT_VALUES[REPO_2_NAME]:-}" "${CURRENT_VALUES[REPO_3_NAME]:-}" printf ' Repos: %s, %s, %s\n' "$(get_env_val REPO_1_NAME)" "$(get_env_val REPO_2_NAME)" "$(get_env_val REPO_3_NAME)"
printf ' SSL: %s\n' "${COLLECTED_SSL_MODE}" printf ' SSL: %s\n' "${COLLECTED_SSL_MODE}"
printf ' .env saved: %s\n\n' "$ENV_FILE" printf ' .env saved: %s\n\n' "$ENV_FILE"