Improved handling of ENV_VARIABLE_PREFIX

#718
This commit is contained in:
Geoff Bourne 2021-02-20 17:30:50 -06:00
parent 92a15ea85d
commit c056b9d2dd

View File

@ -5,7 +5,7 @@
: ${ENV_VARIABLE_PREFIX:=CFG_} : ${ENV_VARIABLE_PREFIX:=CFG_}
if isTrue "${REPLACE_ENV_VARIABLES}"; then if isTrue "${REPLACE_ENV_VARIABLES}"; then
log "Replacing env variables in configs that match the prefix $ENV_VARIABLE_PREFIX..." log "Replacing env variables in configs that match the prefix $ENV_VARIABLE_PREFIX ..."
# File excludes # File excludes
fileExcludes= fileExcludes=
@ -22,28 +22,27 @@ if isTrue "${REPLACE_ENV_VARIABLES}"; then
isDebugging && echo "Using find file exclusions: $fileExcludes" isDebugging && echo "Using find file exclusions: $fileExcludes"
isDebugging && echo "Using find directory exclusions: $dirExcludes" isDebugging && echo "Using find directory exclusions: $dirExcludes"
while IFS='=' read -r name value ; do for name in $(compgen -v $ENV_VARIABLE_PREFIX); do
# check if name of env variable matches the prefix # check if name of env variable matches the prefix
# sanity check environment variables to avoid code injections # sanity check environment variables to avoid code injections
if [[ "$name" = $ENV_VARIABLE_PREFIX* ]] \ # Read content from file environment
&& [[ $value =~ ^[0-9a-zA-Z_:/=?.+\-]*$ ]] \ if [[ $name = *"_FILE" ]]; then
&& [[ $name =~ ^[0-9a-zA-Z_\-]*$ ]]; then value=$(<${!name})
# Read content from file environment name="${name%_FILE}"
if [[ $name = *"_FILE" ]] && [[ -f $value ]]; then else
name="${name/_FILE/}" value=${!name}
value=$(<$value)
fi
log "Replacing $name with $value ..."
find /data/ \
$dirExcludes \
-type f \
\( -name "*.yml" -or -name "*.yaml" -or -name "*.txt" -or -name "*.cfg" \
-or -name "*.conf" -or -name "*.properties" \) \
$fileExcludes \
-exec sed -i 's#${'"$name"'}#'"$value"'#g' {} \;
fi fi
done < <(env)
log "Replacing $name with $value ..."
find /data/ \
$dirExcludes \
-type f \
\( -name "*.yml" -or -name "*.yaml" -or -name "*.txt" -or -name "*.cfg" \
-or -name "*.conf" -or -name "*.properties" \) \
$fileExcludes \
-exec sed -i 's#${'"$name"'}#'"$value"'#g' {} \;
done
fi fi
exec ${SCRIPTS:-/}start-minecraftFinalSetup $@ #DO NOT COMMIT
#exec ${SCRIPTS:-/}start-minecraftFinalSetup $@