Improve code using yq

https://github.com/alexbelgium/hassio-addons/issues/1933
This commit is contained in:
Alexandre
2025-07-05 17:21:38 +02:00
committed by GitHub
parent 5a237c9637
commit 193e9e9f91

View File

@@ -2,8 +2,7 @@
# shellcheck shell=bash # shellcheck shell=bash
set -euo pipefail set -euo pipefail
slug="${HOSTNAME/-/_}" slug="${HOSTNAME/-/_}" ; slug="${slug#*_}"
slug="${slug#*_}"
# CONFIG FILE # CONFIG FILE
if [[ ! -f /config/configuration.yaml && ! -f /config/configuration.json ]]; then if [[ ! -f /config/configuration.yaml && ! -f /config/configuration.json ]]; then
@@ -20,8 +19,8 @@ if bashio::config.has_value 'CONFIG_LOCATION'; then
[[ "$CONFIGSOURCE" == *.* ]] && CONFIGSOURCE="$(dirname "$CONFIGSOURCE")" [[ "$CONFIGSOURCE" == *.* ]] && CONFIGSOURCE="$(dirname "$CONFIGSOURCE")"
[[ "$CONFIGSOURCE" != *.yaml ]] && CONFIGSOURCE="${CONFIGSOURCE%/}/config.yaml" [[ "$CONFIGSOURCE" != *.yaml ]] && CONFIGSOURCE="${CONFIGSOURCE%/}/config.yaml"
case "$CONFIGSOURCE" in case "$CONFIGSOURCE" in
/share/* | /config/* | /data/*) : ;; /share/*|/config/*|/data/*) :;;
*) bashio::log.red "CONFIG_LOCATION must be in /share, /config or /data reverting." && CONFIGSOURCE="$CONFIGLOCATION/config.yaml" ;; *) bashio::log.red "CONFIG_LOCATION must be in /share, /config or /data reverting." && CONFIGSOURCE="$CONFIGLOCATION/config.yaml";;
esac esac
fi fi
@@ -32,13 +31,11 @@ fi
chmod -R 755 "$(dirname "$CONFIGSOURCE")" chmod -R 755 "$(dirname "$CONFIGSOURCE")"
HAS_PYTHON=false HAS_PYTHON=false; command -v python3 &>/dev/null && HAS_PYTHON=true
command -v python3 &>/dev/null && HAS_PYTHON=true HAS_YQ=false; command -v yq &>/dev/null && HAS_YQ=true
HAS_YQ=false
command -v yq &>/dev/null && HAS_YQ=true
$HAS_PYTHON || bashio::log.yellow "python3 not found /env.py export disabled." $HAS_PYTHON || bashio::log.yellow "python3 not found /env.py export disabled."
$HAS_YQ || bashio::log.yellow "yq not found YAML parsing will use fallback parser." $HAS_YQ || bashio::exit.nok "yq not found script not executed."
if [[ ! -f "$CONFIGSOURCE" ]]; then if [[ ! -f "$CONFIGSOURCE" ]]; then
echo "… no config file, creating one from template." echo "… no config file, creating one from template."
@@ -56,32 +53,14 @@ shell_escape() { printf '%q' "$1"; }
# Prints key=value from YAML, ignoring comments/underscored keys # Prints key=value from YAML, ignoring comments/underscored keys
read_config() { read_config() {
local file="$1" local file="$1"
if $HAS_YQ; then
yq eval 'to_entries | .[] | select(.key|test("^[#_]")|not) | "\(.key)=\(.value)"' "$file" 2>/dev/null yq eval 'to_entries | .[] | select(.key|test("^[#_]")|not) | "\(.key)=\(.value)"' "$file" 2>/dev/null
return
fi
awk '
BEGIN{FS=":"}
/^[ \t]*#/ {next} # skip comment lines
/^[ \t]*$/ {next} # skip blank lines
/^[ \t]*[_#]/ {next} # skip keys starting with _ or #
match($0, /^[[:space:]]*([A-Za-z0-9][A-Za-z0-9_]*)[[:space:]]*:[[:space:]]*(.*)$/, m) {
key=m[1]; val=m[2]
gsub(/^['\"] | ['\"]$/,"",val) # remove surrounding quotes
print key "=" val
}
' "$file"
} }
SECRETSFILE="/config/secrets.yaml" SECRETSFILE="/config/secrets.yaml"
[[ -f "$SECRETSFILE" ]] || SECRETSFILE="/homeassistant/secrets.yaml" [[ -f "$SECRETSFILE" ]] || SECRETSFILE="/homeassistant/secrets.yaml"
get_secret() { get_secret() {
local name="$1" local name="$1"
if $HAS_YQ; then
yq eval ".${name}" "$SECRETSFILE" 2>/dev/null || true yq eval ".${name}" "$SECRETSFILE" 2>/dev/null || true
else
grep -m1 "^${name}:" "$SECRETSFILE" 2>/dev/null | sed 's/.*:[[:space:]]*//'
fi
} }
# Safe double-quote for .env and /etc/environment (bash and python compatible) # Safe double-quote for .env and /etc/environment (bash and python compatible)
@@ -100,8 +79,7 @@ while IFS= read -r LINE; do
VALUE="$(get_secret "$NAME")" VALUE="$(get_secret "$NAME")"
[[ -z "$VALUE" ]] && bashio::exit.nok "Secret '$NAME' not found in $SECRETSFILE" [[ -z "$VALUE" ]] && bashio::exit.nok "Secret '$NAME' not found in $SECRETSFILE"
fi fi
VALUE="${VALUE##[[:space:]]}" VALUE="${VALUE##[[:space:]]}" ; VALUE="${VALUE%%[[:space:]]}"
VALUE="${VALUE%%[[:space:]]}"
SAFE_VALUE=$(shell_escape "$VALUE") SAFE_VALUE=$(shell_escape "$VALUE")
export "$KEY=$VALUE" export "$KEY=$VALUE"
if $HAS_PYTHON; then if $HAS_PYTHON; then
@@ -117,14 +95,14 @@ os.environ[k] = v
PY PY
fi fi
env_val=$(dq_escape "$VALUE") env_val=$(dq_escape "$VALUE")
printf '%s="%s"\n' "$KEY" "$env_val" >>/.env printf '%s="%s"\n' "$KEY" "$env_val" >> /.env
printf '%s="%s"\n' "$KEY" "$env_val" >>/etc/environment printf '%s="%s"\n' "$KEY" "$env_val" >> /etc/environment
[[ -d /var/run/s6/container_environment ]] && printf '%s' "$VALUE" >"/var/run/s6/container_environment/$KEY" [[ -d /var/run/s6/container_environment ]] && printf '%s' "$VALUE" > "/var/run/s6/container_environment/$KEY"
for script in /etc/services.d/*/*run* /etc/cont-init.d/*run*; do for script in /etc/services.d/*/*run* /etc/cont-init.d/*run*; do
[[ -f $script ]] || continue [[ -f $script ]] || continue
grep -q "^export $KEY=" "$script" || sed -i "1i export $KEY=$SAFE_VALUE" "$script" grep -q "^export $KEY=" "$script" || sed -i "1i export $KEY=$SAFE_VALUE" "$script"
done done
grep -q "^export $KEY=" ~/.bashrc || echo "export $KEY=$SAFE_VALUE" >>~/.bashrc grep -q "^export $KEY=" ~/.bashrc || echo "export $KEY=$SAFE_VALUE" >> ~/.bashrc
bashio::log.blue "$KEY='${VALUE:0:60}'${VALUE:60:+…}" bashio::log.blue "$KEY='${VALUE:0:60}'${VALUE:60:+…}"
done < <(read_config "$CONFIGSOURCE") done < <(read_config "$CONFIGSOURCE")