mirror of
https://github.com/docker-mailserver/docker-mailserver.git
synced 2025-06-30 20:44:50 +02:00
refactor: improve loop and vars manipulation
Co-authored-by: Georg Lauterbach <44545919+georglauterbach@users.noreply.github.com>
This commit is contained in:
parent
95d41b1e96
commit
3d87bd855e
1 changed files with 15 additions and 12 deletions
|
@ -249,24 +249,27 @@ function __environment_variables_export() {
|
|||
# This function sets any environment variable with a value from a referenced file
|
||||
# when an equivalent ENV with a `__FILE` suffix exists with a valid file path as the value.
|
||||
function __environment_variables_from_files() {
|
||||
for ENV_WITH_FILE_REF in $(env | grep -Po '^.+?__FILE'); do
|
||||
while read -r ENV_WITH_FILE_REF; do
|
||||
# Store the ENV name without the `__FILE` suffix:
|
||||
local TARGET_ENV="${ENV_WITH_FILE_REF/__FILE/}"
|
||||
local TARGET_ENV_NAME="${ENV_WITH_FILE_REF/__FILE/}"
|
||||
local -n TARGET_ENV="${ENV_WITH_FILE_REF/__FILE/}"
|
||||
# Store the value of the `__FILE` ENV:
|
||||
local FILE_PATH="${!ENV_WITH_FILE_REF}"
|
||||
|
||||
# Skip sourcing form `__FILE` if ENV is already set:
|
||||
if [[ -n "${!TARGET_ENV}" ]]; then
|
||||
_log 'warn' "ENV value will not be sourced from '${ENV_WITH_FILE_REF}' since '${TARGET_ENV}' is already set"
|
||||
# Skip sourcing from `__FILE` if ENV is already set:
|
||||
if [[ -v TARGET_ENV ]]; then
|
||||
_log 'warn' "Ignoring '${TARGET_ENV_NAME}' since '${ENV_WITH_FILE_REF}' is also set"
|
||||
continue
|
||||
fi
|
||||
|
||||
# Otherwise retrieve the value from file and set the ENV or fail if invalid reference:
|
||||
if [[ -f "${FILE_PATH}" ]]; then
|
||||
_log 'info' "Getting secret ${TARGET_ENV} from ${FILE_PATH}"
|
||||
printf -v "${TARGET_ENV}" '%s' "$(< "${FILE_PATH}")"
|
||||
else
|
||||
_log 'error' "File ${FILE_PATH} does not exist, defined in ${ENV_WITH_FILE_REF}"
|
||||
# Otherwise, retrieve the value from a file and set
|
||||
# the ENV, or fail if the reference is invalid:
|
||||
if [[ ! -f ${FILE_PATH} ]]; then
|
||||
_log 'warn' "File defined for secret '${TARGET_ENV_NAME}' with path '${FILE_PATH}' does not exist"
|
||||
continue
|
||||
fi
|
||||
done
|
||||
|
||||
_log 'info' "Getting secret '${TARGET_ENV_NAME}' from '${FILE_PATH}'"
|
||||
TARGET_ENV="$(< "${FILE_PATH}")"
|
||||
done < <(env | grep -Po '^.+?__FILE')
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue