Last active
February 9, 2025 00:52
-
-
Save anthochamp/d4d9537f52e5b6c42f0866dd823a605f to your computer and use it in GitHub Desktop.
POSIX shell function to replace __FILE environment variables for Docker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# shellcheck disable=SC2120,SC3043 | |
replaceEnvSecrets() { | |
# replaceEnvSecrets 1.0.0 | |
local prefix="${1:-}" | |
for envSecretName in $(export | awk '{print $2}' | grep -oE '^[^=]+' | grep '__FILE$'); do | |
if [ -z "$prefix" ] || printf '%s' "$envSecretName" | grep "^$prefix" >/dev/null; then | |
local envName | |
envName=$(printf '%s' "$envSecretName" | sed 's/__FILE$//') | |
local filePath | |
filePath=$(eval echo '${'"$envSecretName"':-}') | |
if [ -n "$filePath" ]; then | |
if [ -f "$filePath" ]; then | |
echo Using content from "$filePath" file for "$envName" environment variable value. | |
export "$envName"="$(cat -A "$filePath")" | |
unset "$envSecretName" | |
else | |
echo ERROR: Environment variable "$envSecretName" is defined but does not point to a regular file. 1>&2 | |
exit 1 | |
fi | |
fi | |
fi | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment