2020-04-14 20:22:34 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2020-05-23 15:50:31 +00:00
|
|
|
#####################################################################
|
|
|
|
# Substitutes the given environment variables in the given files.
|
|
|
|
# Parameters:
|
|
|
|
# $1: Comma-delimited list of file paths
|
|
|
|
# $2: Comma-delimited list of environment variables
|
|
|
|
# $3: File path to index.html (optional)
|
|
|
|
#####################################################################
|
|
|
|
|
2020-05-17 13:50:00 +00:00
|
|
|
echo 'Substituting environment variables...'
|
|
|
|
|
2020-05-17 02:59:05 +00:00
|
|
|
# The first parameter is a comma-delimited list of paths to files which should be substituted
|
2020-04-14 20:22:34 +00:00
|
|
|
if [[ -z $1 ]]; then
|
2020-05-17 02:59:05 +00:00
|
|
|
echo 'ERROR: No target files given.'
|
2020-04-14 20:22:34 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-05-20 02:20:03 +00:00
|
|
|
# The second parameter is a comma-delimited list of environment variable names
|
|
|
|
if [[ -z $2 ]]; then
|
|
|
|
echo 'ERROR: No environment variables given.'
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Convert "VAR1,VAR2,VAR3,..." to "\$VAR1 \$VAR2 \$VAR3 ..."
|
|
|
|
env_list="\\\$${2//,/ \\\$}" # "\" and "$" are escaped as "\\" and "\$"
|
|
|
|
for file_path in ${1//,/ }
|
2020-05-17 02:59:05 +00:00
|
|
|
do
|
2020-05-17 13:50:00 +00:00
|
|
|
echo "replacing $env_list in $file_path"
|
|
|
|
|
2020-05-17 02:59:05 +00:00
|
|
|
# Replace strings in the given file(s) in env_list
|
2020-05-17 13:50:00 +00:00
|
|
|
envsubst "$env_list" < "$file_path" > "$file_path".tmp && mv "$file_path".tmp "$file_path"
|
2020-04-14 20:22:34 +00:00
|
|
|
|
2020-05-17 13:50:00 +00:00
|
|
|
echo '...'
|
2020-05-17 02:59:05 +00:00
|
|
|
done
|
2020-04-14 20:22:34 +00:00
|
|
|
|
2020-05-17 13:50:00 +00:00
|
|
|
echo 'Finished substituting environment variables.'
|
2020-05-20 02:20:03 +00:00
|
|
|
for env_var in ${2//,/ }
|
|
|
|
do
|
|
|
|
echo "$env_var = ${!env_var}"
|
|
|
|
done
|
2020-05-17 13:50:00 +00:00
|
|
|
|
2020-05-23 15:50:31 +00:00
|
|
|
# The third parameter is the path to the index.html file
|
|
|
|
# Rewrite base href in index.html.
|
|
|
|
# Use @ as a sed delimiter because $BASE_HREF will contain a / character
|
|
|
|
if [[ -z $3 ]]; then
|
|
|
|
# Execute all other commands with parameters
|
|
|
|
exit 0
|
|
|
|
else
|
|
|
|
sed -i -e 's@<base href\=\"\/\">@<base href\=\"'"$BASE_HREF"'\">@' "$3"
|
|
|
|
|
|
|
|
# Execute all other commands with parameters
|
|
|
|
exec "${@:4}"
|
|
|
|
fi
|
|
|
|
|
2020-05-23 15:31:06 +00:00
|
|
|
|