1
0
Fork 0
pixelfed/docker/shared/root/docker/entrypoint.d/05-templating.sh

61 lines
2.1 KiB
Bash
Raw Normal View History

2024-01-04 16:08:01 +00:00
#!/bin/bash
2024-01-15 17:16:00 +00:00
: "${ENTRYPOINT_ROOT:="/docker"}"
# shellcheck source=SCRIPTDIR/../helpers.sh
source "${ENTRYPOINT_ROOT}/helpers.sh"
2024-01-04 16:08:01 +00:00
2024-01-05 00:11:20 +00:00
entrypoint-set-script-name "$0"
2024-01-04 16:08:01 +00:00
2024-01-04 21:55:24 +00:00
# Show [git diff] of templates being rendered (will help verify output)
2024-01-15 17:16:00 +00:00
: "${ENTRYPOINT_SHOW_TEMPLATE_DIFF:=1}"
2024-01-04 21:55:24 +00:00
# Directory where templates can be found
2024-01-15 17:16:00 +00:00
: "${ENTRYPOINT_TEMPLATE_DIR:=/docker/templates/}"
2024-01-04 21:55:24 +00:00
# Root path to write template template_files to (default is '', meaning it will be written to /<path>)
2024-01-15 17:16:00 +00:00
: "${ENTRYPOINT_TEMPLATE_OUTPUT_PREFIX:=}"
2024-01-04 16:08:01 +00:00
2024-01-04 21:55:24 +00:00
declare template_file relative_template_file_path output_file_dir
2024-01-04 16:08:01 +00:00
2024-01-04 21:55:24 +00:00
# load all dot-env config files
load-config-files
2024-01-04 16:08:01 +00:00
2024-01-04 20:55:04 +00:00
# export all dot-env variables so they are available in templating
2024-01-15 17:16:00 +00:00
#
# shellcheck disable=SC2068
2024-01-04 20:55:04 +00:00
export ${seen_dot_env_variables[@]}
2024-01-04 16:08:01 +00:00
2024-01-04 21:55:24 +00:00
find "${ENTRYPOINT_TEMPLATE_DIR}" -follow -type f -print | while read -r template_file; do
# Example: template_file=/docker/templates/usr/local/etc/php/php.ini
# The file path without the template dir prefix ($ENTRYPOINT_TEMPLATE_DIR)
#
# Example: /usr/local/etc/php/php.ini
relative_template_file_path="${template_file#"${ENTRYPOINT_TEMPLATE_DIR}"}"
# Adds optional prefix to the output file path
#
# Example: /usr/local/etc/php/php.ini
output_file_path="${ENTRYPOINT_TEMPLATE_OUTPUT_PREFIX}/${relative_template_file_path}"
# Remove the file from the path
#
# Example: /usr/local/etc/php
output_file_dir=$(dirname "${output_file_path}")
2024-01-04 16:08:01 +00:00
2024-01-04 21:55:24 +00:00
# Ensure the output directory is writable
if ! is-writable "${output_file_dir}"; then
log-error-and-exit "${output_file_dir} is not writable"
2024-01-04 20:55:04 +00:00
fi
2024-01-04 16:08:01 +00:00
2024-01-04 21:55:24 +00:00
# Create the output directory if it doesn't exists
2024-01-04 22:16:25 +00:00
ensure-directory-exists "${output_file_dir}"
2024-01-04 16:08:01 +00:00
2024-01-04 21:55:24 +00:00
# Render the template
log-info "Running [gomplate] on [${template_file}] --> [${output_file_path}]"
2024-02-22 14:56:33 +00:00
gomplate <"${template_file}" >"${output_file_path}"
2024-01-04 16:08:01 +00:00
2024-01-04 20:55:04 +00:00
# Show the diff from the envsubst command
if is-true "${ENTRYPOINT_SHOW_TEMPLATE_DIFF}"; then
git --no-pager diff --color=always "${template_file}" "${output_file_path}" || : # ignore diff exit code
2024-01-04 20:55:04 +00:00
fi
done