You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
1.0 KiB

1 year ago
  1. #!/bin/sh
  2. set -e
  3. ME=$(basename $0)
  4. auto_envsubst() {
  5. local template_dir="${NGINX_ENVSUBST_TEMPLATE_DIR:-/etc/nginx/templates}"
  6. local suffix="${NGINX_ENVSUBST_TEMPLATE_SUFFIX:-.template}"
  7. local output_dir="${NGINX_ENVSUBST_OUTPUT_DIR:-/etc/nginx/conf.d}"
  8. local template defined_envs relative_path output_path subdir
  9. defined_envs=$(printf '${%s} ' $(env | cut -d= -f1))
  10. [ -d "$template_dir" ] || return 0
  11. if [ ! -w "$output_dir" ]; then
  12. echo >&3 "$ME: ERROR: $template_dir exists, but $output_dir is not writable"
  13. return 0
  14. fi
  15. find "$template_dir" -follow -type f -name "*$suffix" -print | while read -r template; do
  16. relative_path="${template#$template_dir/}"
  17. output_path="$output_dir/${relative_path%$suffix}"
  18. subdir=$(dirname "$relative_path")
  19. # create a subdirectory where the template file exists
  20. mkdir -p "$output_dir/$subdir"
  21. echo >&3 "$ME: Running envsubst on $template to $output_path"
  22. envsubst "$defined_envs" < "$template" > "$output_path"
  23. done
  24. }
  25. auto_envsubst
  26. exit 0