From 1c5959cafbd5572662f148fc2d749259b6cd1f2f Mon Sep 17 00:00:00 2001 From: aki Date: Fri, 25 Apr 2025 23:52:33 +0800 Subject: [PATCH] fix(env): Enhance environment variable tracking by adding current key presence check --- update-env.sh | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/update-env.sh b/update-env.sh index 06aba1e..042ee06 100755 --- a/update-env.sh +++ b/update-env.sh @@ -55,6 +55,7 @@ cp "$ENV_FILE" "$ENV_BACKUP" # Store current env values echo -e "${BLUE}Reading current environment values...${NC}" declare -A current_values +declare -A current_keys_present while IFS='=' read -r key value; do # Skip comments and empty lines if [[ ! "$key" =~ ^#.*$ ]] && [[ ! -z "$key" ]]; then @@ -66,6 +67,8 @@ while IFS='=' read -r key value; do # Store in associative array if key is not empty if [[ ! -z "$key" ]]; then current_values["$key"]="$value" + # Track that this key existed in original file, regardless of value + current_keys_present["$key"]=1 fi fi done < "$ENV_FILE" @@ -87,19 +90,20 @@ while IFS= read -r line; do key="${BASH_REMATCH[1]}" default_value="${BASH_REMATCH[2]}" - # Check if this key exists in current values - if [[ -n "${current_values[$key]}" ]]; then - # Replace the line with the current value - sed -i "s|^$key=.*$|$key=${current_values[$key]}|" "$ENV_FILE.new" + # Mark the key as used if it exists in the original file + if [[ -n "${current_keys_present[$key]}" ]]; then used_keys["$key"]=1 - else - # Mark new keys that need attention (those without default values) - if [[ -z "$default_value" ]] || [[ "$default_value" == '""' ]] || [[ "$default_value" == "''" ]]; then - new_keys+=("$key") + + # Replace the line with the current value if one exists + if [[ -n "${current_values[$key]}" ]]; then + sed -i "s|^$key=.*$|$key=${current_values[$key]}|" "$ENV_FILE.new" fi + # If key doesn't exist in original file and has empty/placeholder value + elif [[ -z "$default_value" ]] || [[ "$default_value" == '""' ]] || [[ "$default_value" == "''" ]]; then + new_keys+=("$key") # Special attention for Authelia keys - if [[ "$key" == AUTHELIA_* ]] && [[ -z "$default_value" ]]; then + if [[ "$key" == AUTHELIA_*_SECRET* ]] || [[ "$key" == AUTHELIA_*_KEY* ]]; then special_keys+=("$key") fi fi