mirror of
https://github.com/alexbelgium/hassio-addons.git
synced 2026-06-05 15:15:58 +02:00
Refactor export_option function for better handling
Refactor environment variable export logic to handle array values and streamline secret detection.
This commit is contained in:
@@ -45,76 +45,101 @@ sanitize_variable() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export_option() {
|
||||||
|
local key="$1"
|
||||||
|
local value="$2"
|
||||||
|
local line secret secretnum valuepy
|
||||||
|
|
||||||
|
value=$(sanitize_variable "$value")
|
||||||
|
|
||||||
|
if [[ -z "$value" ]]; then
|
||||||
|
line="${key}=''"
|
||||||
|
else
|
||||||
|
line="${key}='${value//\'/\'\\\'\'}'"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "${line}" == *"!secret "* ]]; then
|
||||||
|
echo "secret detected"
|
||||||
|
secret=${line#*secret }
|
||||||
|
secret="${secret%[\"\']}"
|
||||||
|
if [[ "$SECRETSOURCE" == "false" ]]; then
|
||||||
|
bashio::log.warning "Homeassistant config not mounted, secrets are not supported"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
secretnum=$(sed -n "/$secret:/=" "$SECRETSOURCE")
|
||||||
|
[[ "$secretnum" == *' '* ]] && bashio::exit.nok "There are multiple matches for your password name. Please check your secrets.yaml file"
|
||||||
|
secret=$(sed -n "/$secret:/p" "$SECRETSOURCE")
|
||||||
|
secret=${secret#*: }
|
||||||
|
line="${line%%=*}='$secret'"
|
||||||
|
value="$secret"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if bashio::config.false "verbose" || [[ "${key,,}" == *"pass"* ]]; then
|
||||||
|
bashio::log.blue "${key}=******"
|
||||||
|
else
|
||||||
|
bashio::log.blue "$line"
|
||||||
|
fi
|
||||||
|
|
||||||
|
export "$line"
|
||||||
|
|
||||||
|
if command -v "python3" &> /dev/null; then
|
||||||
|
[ ! -f /env.py ] && echo "import os" > /env.py
|
||||||
|
valuepy="${value//\\/\\\\}"
|
||||||
|
valuepy="${valuepy//[\"\']/}"
|
||||||
|
echo "os.environ['${key}'] = '$valuepy'" >> /env.py
|
||||||
|
python3 /env.py
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "$line" >> /.env || true
|
||||||
|
mkdir -p /etc
|
||||||
|
echo "$line" >> /etc/environment
|
||||||
|
if cat /etc/services.d/*/*run* &> /dev/null; then sed -i "1a export $line" /etc/services.d/*/*run* 2> /dev/null; fi
|
||||||
|
if cat /etc/cont-init.d/*.sh &> /dev/null; then sed -i "1a export $line" /etc/cont-init.d/*.sh 2> /dev/null; fi
|
||||||
|
if [ -d /var/run/s6/container_environment ]; then printf "%s" "${value}" > /var/run/s6/container_environment/"${key}"; fi
|
||||||
|
echo "export ${key}='${value}'" >> ~/.bashrc
|
||||||
|
}
|
||||||
|
|
||||||
for KEYS in "${arr[@]}"; do
|
for KEYS in "${arr[@]}"; do
|
||||||
# export key
|
# export key
|
||||||
VALUE=$(jq -r --raw-output ".\"$KEYS\"" "$JSONSOURCE")
|
VALUE=$(jq -r --raw-output ".\"$KEYS\"" "$JSONSOURCE")
|
||||||
# Check if the value is an array
|
# Check if the value is an array
|
||||||
if [[ "$VALUE" == \[* ]]; then
|
if [[ "$VALUE" == \[* ]]; then
|
||||||
bashio::log.warning "One of your option is an array, skipping"
|
if [[ "$KEYS" == "env_vars" ]]; then
|
||||||
else
|
mapfile -t env_entries < <(jq -c ".\"$KEYS\"[]" "$JSONSOURCE")
|
||||||
# Sanitize variable
|
if [[ "${#env_entries[@]}" -eq 0 ]]; then
|
||||||
VALUE=$(sanitize_variable "$VALUE")
|
|
||||||
# If value is empty, returns an empty value
|
|
||||||
if [[ -z "$VALUE" ]]; then
|
|
||||||
line="${KEYS}=''"
|
|
||||||
else
|
|
||||||
# Continue for single values
|
|
||||||
line="${KEYS}='${VALUE//\'/\'\\\'\'}'"
|
|
||||||
fi
|
|
||||||
# Check if secret
|
|
||||||
if [[ "${line}" == *"!secret "* ]]; then
|
|
||||||
echo "secret detected"
|
|
||||||
# Get argument
|
|
||||||
secret=${line#*secret }
|
|
||||||
# Remove trailing ' or "
|
|
||||||
secret="${secret%[\"\']}"
|
|
||||||
# Stop if secret file not mounted
|
|
||||||
if [[ "$SECRETSOURCE" == "false" ]]; then
|
|
||||||
bashio::log.warning "Homeassistant config not mounted, secrets are not supported"
|
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
# Check if single match
|
env_processed=false
|
||||||
secretnum=$(sed -n "/$secret:/=" "$SECRETSOURCE")
|
for entry in "${env_entries[@]}"; do
|
||||||
[[ "$secretnum" == *' '* ]] && bashio::exit.nok "There are multiple matches for your password name. Please check your secrets.yaml file"
|
if [[ "$entry" == \{* ]]; then
|
||||||
# Get text
|
mapfile -t env_keys < <(jq -r 'keys[]' <<<"$entry")
|
||||||
secret=$(sed -n "/$secret:/p" "$SECRETSOURCE")
|
for env_key in "${env_keys[@]}"; do
|
||||||
secret=${secret#*: }
|
env_value=$(jq -r --arg key "$env_key" '.[$key]' <<<"$entry")
|
||||||
line="${line%%=*}='$secret'"
|
export_option "$env_key" "$env_value"
|
||||||
VALUE="$secret"
|
env_processed=true
|
||||||
fi
|
done
|
||||||
# text
|
elif [[ "${entry:0:1}" == '"' ]]; then
|
||||||
if bashio::config.false "verbose" || [[ "${KEYS,,}" == *"pass"* ]]; then
|
env_pair=$(jq -r '.' <<<"$entry")
|
||||||
bashio::log.blue "${KEYS}=******"
|
if [[ "$env_pair" == *=* ]]; then
|
||||||
|
env_key=${env_pair%%=*}
|
||||||
|
env_value=${env_pair#*=}
|
||||||
|
export_option "$env_key" "$env_value"
|
||||||
|
env_processed=true
|
||||||
|
else
|
||||||
|
bashio::log.warning "env_vars entry '$env_pair' is not in KEY=VALUE format, skipping"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
bashio::log.warning "env_vars entry format not supported, skipping"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
if [[ "$env_processed" == false ]]; then
|
||||||
|
bashio::log.warning "env_vars option format not supported, skipping"
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
bashio::log.blue "$line"
|
bashio::log.warning "One of your option is an array, skipping"
|
||||||
fi
|
fi
|
||||||
|
else
|
||||||
######################################
|
export_option "$KEYS" "$VALUE"
|
||||||
# Export the variable to run scripts #
|
|
||||||
######################################
|
|
||||||
# shellcheck disable=SC2163
|
|
||||||
export "$line"
|
|
||||||
# export to python
|
|
||||||
if command -v "python3" &> /dev/null; then
|
|
||||||
[ ! -f /env.py ] && echo "import os" > /env.py
|
|
||||||
# Escape \
|
|
||||||
VALUEPY="${VALUE//\\/\\\\}"
|
|
||||||
# Avoid " and '
|
|
||||||
VALUEPY="${VALUEPY//[\"\']/}"
|
|
||||||
echo "os.environ['${KEYS}'] = '$VALUEPY'" >> /env.py
|
|
||||||
python3 /env.py
|
|
||||||
fi
|
|
||||||
# set .env
|
|
||||||
echo "$line" >> /.env || true
|
|
||||||
# set /etc/environment
|
|
||||||
mkdir -p /etc
|
|
||||||
echo "$line" >> /etc/environment
|
|
||||||
# For non s6
|
|
||||||
if cat /etc/services.d/*/*run* &> /dev/null; then sed -i "1a export $line" /etc/services.d/*/*run* 2> /dev/null; fi
|
|
||||||
if cat /etc/cont-init.d/*.sh &> /dev/null; then sed -i "1a export $line" /etc/cont-init.d/*.sh 2> /dev/null; fi
|
|
||||||
# For s6
|
|
||||||
if [ -d /var/run/s6/container_environment ]; then printf "%s" "${VALUE}" > /var/run/s6/container_environment/"${KEYS}"; fi
|
|
||||||
echo "export ${KEYS}='${VALUE}'" >> ~/.bashrc
|
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user