Update 01-config_yaml.sh

This commit is contained in:
Alexandre
2025-07-06 16:25:01 +02:00
committed by GitHub
parent 9468b4f956
commit 9250af0f0b

View File

@@ -134,23 +134,27 @@ fi
# Export all yaml entries as env variables # Export all yaml entries as env variables
# Helper function # Helper function
function parse_yaml { function parse_yaml_to_vars {
local prefix=$2 || local prefix="" local input_file=$1
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @ | tr @ '\034') local output_file=$2
sed -ne "s|^\($s\):|\1|" \
-e "s| #.*$||g" \ # Clear the output file if it already exists
-e "s|#.*$||g" \ > "$output_file"
-e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 | # Process each line to escape special characters and format as needed
awk -F$fs '{ while IFS= read -r line; do
indent = length($1)/2; # Skip lines that are empty or only contain whitespace
vname[indent] = $2; [[ -z "$line" ]] && continue
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) { # Replace the first occurrence of ": " with "="
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")} line=${line/: /=}
printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
} # Escape special characters not within single quotes
}' line=$(sed -E "s/([^'])([][\$\`\"\\!&;|<>])/\1\\\\\\2/g" <<< "$line")
# Write to output file
echo "$line" >> "$output_file"
done < "$input_file"
} }
# Get list of parameters in a file # Get list of parameters in a file
@@ -163,8 +167,6 @@ SECRETSFILE="/config/secrets.yaml"
if [ -f "$SECRETSFILE" ]; then SECRETSFILE="/homeassistant/secrets.yaml"; fi if [ -f "$SECRETSFILE" ]; then SECRETSFILE="/homeassistant/secrets.yaml"; fi
while IFS= read -r line; do while IFS= read -r line; do
# Clean output
line="${line//[\"\']/}"
# Check if secret # Check if secret
if [[ "${line}" == *'!secret '* ]]; then if [[ "${line}" == *'!secret '* ]]; then
echo "secret detected" echo "secret detected"