mirror of
https://github.com/alexbelgium/hassio-addons.git
synced 2026-01-11 02:11:01 +01:00
Revert
This commit is contained in:
@@ -27,75 +27,90 @@ fi
|
|||||||
# echo "All addon options were exported as variables"
|
# echo "All addon options were exported as variables"
|
||||||
mapfile -t arr < <(jq -r 'keys[]' "${JSONSOURCE}")
|
mapfile -t arr < <(jq -r 'keys[]' "${JSONSOURCE}")
|
||||||
|
|
||||||
|
# Escape special characters using printf and enclose in double quotes
|
||||||
|
sanitize_variable() {
|
||||||
|
local raw="$1" # original value
|
||||||
|
local escaped # value after printf %q
|
||||||
|
# Check if the value is an array
|
||||||
|
if [[ "$raw" == \[* ]]; then
|
||||||
|
echo "One of your options is an array, skipping"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
printf -v escaped '%q' "$raw"
|
||||||
|
# If nothing changed, return the original.
|
||||||
|
if [[ "$raw" == "$escaped" ]]; then
|
||||||
|
printf '%s' "$raw"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
# Otherwise protect the escaped string with double quotes.
|
||||||
|
printf '"%s"' "$escaped"
|
||||||
|
}
|
||||||
|
|
||||||
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 options is an array, skipping"
|
bashio::log.warning "One of your option is an array, skipping"
|
||||||
continue
|
else
|
||||||
fi
|
# Sanitize variable
|
||||||
|
VALUE=$(sanitize_variable "$VALUE")
|
||||||
# Check if secret
|
# Continue for single values
|
||||||
if [[ "$VALUE" == *"!secret "* ]]; then
|
|
||||||
echo "secret detected"
|
|
||||||
# Get argument
|
|
||||||
secret=${VALUE#*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
|
|
||||||
fi
|
|
||||||
# Check if single match
|
|
||||||
secretnum=$(sed -n "/$secret:/=" "$SECRETSOURCE")
|
|
||||||
[[ "$secretnum" == *' '* ]] && bashio::exit.nok "There are multiple matches for your password name. Please check your secrets.yaml file"
|
|
||||||
# Get text
|
|
||||||
secret=$(sed -n "/$secret:/p" "$SECRETSOURCE")
|
|
||||||
secret=${secret#*: }
|
|
||||||
VALUE="$secret"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Data validation and export
|
|
||||||
if [[ "$KEYS" =~ ^[^[:space:]]+$ ]]; then
|
|
||||||
line="${KEYS}=${VALUE}"
|
line="${KEYS}=${VALUE}"
|
||||||
# Show in log
|
# 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
|
||||||
|
fi
|
||||||
|
# Check if single match
|
||||||
|
secretnum=$(sed -n "/$secret:/=" "$SECRETSOURCE")
|
||||||
|
[[ "$secretnum" == *' '* ]] && bashio::exit.nok "There are multiple matches for your password name. Please check your secrets.yaml file"
|
||||||
|
# Get text
|
||||||
|
secret=$(sed -n "/$secret:/p" "$SECRETSOURCE")
|
||||||
|
secret=${secret#*: }
|
||||||
|
line="${line%%=*}='$secret'"
|
||||||
|
VALUE="$secret"
|
||||||
|
fi
|
||||||
|
# text
|
||||||
if bashio::config.false "verbose" || [[ "${KEYS,,}" == *"pass"* ]]; then
|
if bashio::config.false "verbose" || [[ "${KEYS,,}" == *"pass"* ]]; then
|
||||||
bashio::log.blue "${KEYS}=******"
|
bashio::log.blue "${KEYS}=******"
|
||||||
else
|
else
|
||||||
bashio::log.blue "$line"
|
bashio::log.blue "$line"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Export the variable to run scripts
|
######################################
|
||||||
|
# Export the variable to run scripts #
|
||||||
|
######################################
|
||||||
|
# shellcheck disable=SC2163
|
||||||
export "$line"
|
export "$line"
|
||||||
|
# export to python
|
||||||
# Export to python
|
|
||||||
if command -v "python3" &>/dev/null; then
|
if command -v "python3" &>/dev/null; then
|
||||||
[ ! -f /env.py ] && echo "import os" >/env.py
|
[ ! -f /env.py ] && echo "import os" >/env.py
|
||||||
# Escape single quotes in VALUE
|
# Escape \
|
||||||
VALUE_ESCAPED="${VALUE//\'/\'\"\'\"\'}"
|
VALUEPY="${VALUE//\\/\\\\}"
|
||||||
echo "os.environ['${KEYS}'] = '${VALUE_ESCAPED}'" >>/env.py
|
# Avoid " and '
|
||||||
|
VALUEPY="${VALUEPY//[\"\']/}"
|
||||||
|
echo "os.environ['${KEYS}'] = '$VALUEPY'" >>/env.py
|
||||||
python3 /env.py
|
python3 /env.py
|
||||||
fi
|
fi
|
||||||
|
# set .env
|
||||||
# Set .env
|
|
||||||
echo "$line" >>/.env || true
|
echo "$line" >>/.env || true
|
||||||
|
# set /etc/environment
|
||||||
# Set /etc/environment
|
|
||||||
mkdir -p /etc
|
mkdir -p /etc
|
||||||
echo "$line" >>/etc/environment
|
echo "$line" >>/etc/environment
|
||||||
|
|
||||||
# For non s6
|
# 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/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/*run* &>/dev/null; then sed -i "1a export $line" /etc/cont-init.d/*run* 2>/dev/null; fi
|
if cat /etc/cont-init.d/*run* &>/dev/null; then sed -i "1a export $line" /etc/cont-init.d/*run* 2>/dev/null; fi
|
||||||
|
|
||||||
# For s6
|
# For s6
|
||||||
if [ -d /var/run/s6/container_environment ]; then printf "%s" "${VALUE}" >/var/run/s6/container_environment/"${KEYS}"; fi
|
if [ -d /var/run/s6/container_environment ]; then printf "%s" "${VALUE}" >/var/run/s6/container_environment/"${KEYS}"; fi
|
||||||
echo "export ${KEYS}='${VALUE}'" >>~/.bashrc
|
echo "export ${KEYS}='${VALUE}'" >>~/.bashrc
|
||||||
else
|
|
||||||
bashio::log.red "Skipping line that does not follow the correct structure: $line"
|
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user