add ability to substitute env vars with _file vars

This commit is contained in:
donor_extortion 2023-10-13 21:34:51 -06:00
parent 872acd329a
commit d275c47b07
1 changed files with 35 additions and 0 deletions

View File

@ -1,6 +1,41 @@
#!/usr/bin/env sh
set -e
# usage: file_env VAR [DEFAULT]
# Set $VAR to the contents of the path specified by $VAR_FILE. Useful for docker secrets
file_env() {
VAR="$1"
FILE_VAR="${VAR}_FILE"
eval "VAR_EXPANDED=\"\${$VAR}\""
eval "FILE_VAR_EXPANDED=\"\${$FILE_VAR}\""
DEFAULT="${2:-}"
if [ "${VAR_EXPANDED:-}" ] && [ "${FILE_VAR_EXPANDED:-}" ]; then
echo >&2 "error: both $VAR and $FILE_VAR are set (but are exclusive)"
exit 1
fi
VAL="$DEFAULT"
if [ "${VAR_EXPANDED:-}" ]; then
VAL="${VAR_EXPANDED}"
elif [ "${FILE_VAR_EXPANDED:-}" ]; then
if [ -f "${FILE_VAR_EXPANDED}" ]; then
VAL="$(cat "${FILE_VAR_EXPANDED}")"
else
echo >&2 "error: couldn't find file at '$FILE_VAR_EXPANDED'"
exit 1
fi
fi
export "$VAR"="$VAL"
unset "$FILE_VAR"
}
# Substitue all env vars starting with VIKUNJA and ending with _FILE
for var in $(env | sed -n "s/^\(VIKUNJA.*\)_FILE=.*$/\1/p"); do
file_env "${var}"
done
if [ -n "$PUID" ] && [ "$PUID" -ne 0 ] && \
[ -n "$PGID" ] && [ "$PGID" -ne 0 ] ; then
echo "info: creating the new user vikunja with $PUID:$PGID"