mirror of
https://github.com/alexbelgium/hassio-addons.git
synced 2026-01-11 02:11:01 +01:00
GitHub bot: fix linting issues (nobuild)
This commit is contained in:
@@ -2,88 +2,92 @@
|
|||||||
# shellcheck shell=bash
|
# shellcheck shell=bash
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
slug="${HOSTNAME/-/_}" ; slug="${slug#*_}"
|
slug="${HOSTNAME/-/_}"
|
||||||
|
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
|
||||||
CONFIGLOCATION="/config"
|
CONFIGLOCATION="/config"
|
||||||
else
|
else
|
||||||
CONFIGLOCATION="/config/addons_config/${slug}"
|
CONFIGLOCATION="/config/addons_config/${slug}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
mkdir -p "$CONFIGLOCATION"
|
mkdir -p "$CONFIGLOCATION"
|
||||||
CONFIGSOURCE="$CONFIGLOCATION/config.yaml"
|
CONFIGSOURCE="$CONFIGLOCATION/config.yaml"
|
||||||
|
|
||||||
if bashio::config.has_value 'CONFIG_LOCATION'; then
|
if bashio::config.has_value 'CONFIG_LOCATION'; then
|
||||||
CONFIGSOURCE="$(bashio::config "CONFIG_LOCATION")"
|
CONFIGSOURCE="$(bashio::config "CONFIG_LOCATION")"
|
||||||
[[ "$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
|
||||||
|
|
||||||
if [[ "$CONFIGLOCATION" == "/config" && -f "/homeassistant/addons_config/${slug}/config.yaml" && ! -L "/homeassistant/addons_config/${slug}" ]]; then
|
if [[ "$CONFIGLOCATION" == "/config" && -f "/homeassistant/addons_config/${slug}/config.yaml" && ! -L "/homeassistant/addons_config/${slug}" ]]; then
|
||||||
echo "Migrating config.yaml to $CONFIGLOCATION"
|
echo "Migrating config.yaml to $CONFIGLOCATION"
|
||||||
mv "/homeassistant/addons_config/${slug}/config.yaml" "$CONFIGSOURCE"
|
mv "/homeassistant/addons_config/${slug}/config.yaml" "$CONFIGSOURCE"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
chmod -R 755 "$(dirname "$CONFIGSOURCE")"
|
chmod -R 755 "$(dirname "$CONFIGSOURCE")"
|
||||||
|
|
||||||
HAS_PYTHON=false; command -v python3 &>/dev/null && HAS_PYTHON=true
|
HAS_PYTHON=false
|
||||||
HAS_YQ=false; command -v yq &>/dev/null && HAS_YQ=true
|
command -v python3 &>/dev/null && HAS_PYTHON=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::exit.nok "yq not found – script not executed."
|
$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."
|
||||||
mkdir -p "$(dirname "$CONFIGSOURCE")"
|
mkdir -p "$(dirname "$CONFIGSOURCE")"
|
||||||
if [[ -f /templates/config.yaml ]]; then
|
if [[ -f /templates/config.yaml ]]; then
|
||||||
cp /templates/config.yaml "$CONFIGSOURCE"
|
cp /templates/config.yaml "$CONFIGSOURCE"
|
||||||
else
|
else
|
||||||
curl -fsSL "https://raw.githubusercontent.com/alexbelgium/hassio-addons/master/.templates/config.template" -o "$CONFIGSOURCE"
|
curl -fsSL "https://raw.githubusercontent.com/alexbelgium/hassio-addons/master/.templates/config.template" -o "$CONFIGSOURCE"
|
||||||
fi
|
fi
|
||||||
bashio::log.green "Edit $CONFIGSOURCE then restart the add‑on."
|
bashio::log.green "Edit $CONFIGSOURCE then restart the add‑on."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
shell_escape() { printf '%q' "$1"; }
|
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"
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
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"
|
||||||
yq eval ".${name}" "$SECRETSFILE" 2>/dev/null || true
|
yq eval ".${name}" "$SECRETSFILE" 2>/dev/null || true
|
||||||
}
|
}
|
||||||
|
|
||||||
# Safe double-quote for .env and /etc/environment (bash and python compatible)
|
# Safe double-quote for .env and /etc/environment (bash and python compatible)
|
||||||
dq_escape() {
|
dq_escape() {
|
||||||
# Escape only embedded double quotes and dollar signs for shell (not for YAML)
|
# Escape only embedded double quotes and dollar signs for shell (not for YAML)
|
||||||
printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g; s/\$/\\$/g'
|
printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g; s/\$/\\$/g'
|
||||||
}
|
}
|
||||||
|
|
||||||
while IFS= read -r LINE; do
|
while IFS= read -r LINE; do
|
||||||
[[ -z "$LINE" || "$LINE" != *=* ]] && continue
|
[[ -z "$LINE" || "$LINE" != *=* ]] && continue
|
||||||
KEY="${LINE%%=*}"
|
KEY="${LINE%%=*}"
|
||||||
VALUE="${LINE#*=}"
|
VALUE="${LINE#*=}"
|
||||||
# !secret handling
|
# !secret handling
|
||||||
if [[ "$VALUE" =~ ^!secret[[:space:]]+(.+) ]]; then
|
if [[ "$VALUE" =~ ^!secret[[:space:]]+(.+) ]]; then
|
||||||
NAME="${BASH_REMATCH[1]}"
|
NAME="${BASH_REMATCH[1]}"
|
||||||
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:]]}"
|
||||||
SAFE_VALUE=$(shell_escape "$VALUE")
|
VALUE="${VALUE%%[[:space:]]}"
|
||||||
export "$KEY=$VALUE"
|
SAFE_VALUE=$(shell_escape "$VALUE")
|
||||||
if $HAS_PYTHON; then
|
export "$KEY=$VALUE"
|
||||||
python3 - "$KEY" "$VALUE" <<'PY'
|
if $HAS_PYTHON; then
|
||||||
|
python3 - "$KEY" "$VALUE" <<'PY'
|
||||||
import json, os, pathlib, sys
|
import json, os, pathlib, sys
|
||||||
k, v = sys.argv[1:3]
|
k, v = sys.argv[1:3]
|
||||||
p = pathlib.Path('/env.py')
|
p = pathlib.Path('/env.py')
|
||||||
@@ -93,17 +97,17 @@ with p.open('a') as f:
|
|||||||
f.write(f"os.environ[{json.dumps(k)}] = {json.dumps(v)}\n")
|
f.write(f"os.environ[{json.dumps(k)}] = {json.dumps(v)}\n")
|
||||||
os.environ[k] = v
|
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")
|
||||||
|
|
||||||
bashio::log.green "Environment variables successfully loaded."
|
bashio::log.green "Environment variables successfully loaded."
|
||||||
|
|||||||
Reference in New Issue
Block a user