Replace sed-based variable replacement with envsubst for universal handling of special characters
This commit is contained in:
@@ -119,35 +119,19 @@ localize_yml_file() {
|
|||||||
debug_log "Backed up $file_path"
|
debug_log "Backed up $file_path"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Find all ${VAR} patterns in the file
|
# Use envsubst to replace all ${VAR} with environment values
|
||||||
local vars=$(grep -o '\${[^}]*}' "$file_path" | sed 's/\${//' | sed 's/}//' | sort | uniq)
|
if command -v envsubst >/dev/null 2>&1; then
|
||||||
debug_log "Found variables to replace: $vars"
|
envsubst < "$file_path" > "$file_path.tmp" && mv "$file_path.tmp" "$file_path"
|
||||||
|
debug_log "Replaced variables in $file_path using envsubst"
|
||||||
for var in $vars; do
|
replaced_count=$(grep -o '\${[^}]*}' "$file_path" | wc -l)
|
||||||
# Trim whitespace from variable name
|
replaced_count=$((replaced_count / 2)) # Approximate, since envsubst replaces all
|
||||||
var=$(echo "$var" | xargs)
|
else
|
||||||
# Skip derived variables that should not be replaced
|
log_warning "envsubst not available, cannot localize $file_path"
|
||||||
case "$var" in
|
if [ "$fail_on_missing" = true ]; then
|
||||||
"ACME_EMAIL"|"AUTHELIA_ADMIN_EMAIL"|"SMTP_USERNAME"|"SMTP_PASSWORD")
|
exit 1
|
||||||
debug_log "Skipping derived variable: $var"
|
|
||||||
continue
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
if [ -z "${!var+x}" ]; then
|
|
||||||
log_warning "Environment variable $var not found in .env file"
|
|
||||||
debug_log "Missing variable: $var"
|
|
||||||
missing_vars="$missing_vars $var"
|
|
||||||
else
|
|
||||||
# Replace ${VAR} with the value, handling special characters
|
|
||||||
local escaped_value=$(printf '%s\n' "${!var}" | sed 's/[[\.*^$()+?{|]/\\&/g')
|
|
||||||
debug_log "Replacing \${$var} with value: [HIDDEN]" # Don't log actual secrets
|
|
||||||
sed -i "s|\${[ \t]*${var}[ \t]*}|${escaped_value}|g" "$file_path"
|
|
||||||
replaced_count=$((replaced_count + 1))
|
|
||||||
fi
|
fi
|
||||||
done
|
return
|
||||||
|
fi
|
||||||
debug_log "Replaced $replaced_count variables in $file_path"
|
|
||||||
|
|
||||||
# Post-replacement validation: check for remaining ${VAR} (except skipped)
|
# Post-replacement validation: check for remaining ${VAR} (except skipped)
|
||||||
local remaining_vars=$(grep -v '^[ \t]*#' "$file_path" | grep -o '\${[^}]*}' | sed 's/\${//' | sed 's/}//' | sort | uniq)
|
local remaining_vars=$(grep -v '^[ \t]*#' "$file_path" | grep -o '\${[^}]*}' | sed 's/\${//' | sed 's/}//' | sort | uniq)
|
||||||
|
|||||||
Reference in New Issue
Block a user