2023-03-31 22:29:59 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
if [ -n "$CERC_SCRIPT_DEBUG" ]; then
|
|
|
|
set -x
|
|
|
|
fi
|
|
|
|
if [[ $# -ne 2 ]]; then
|
|
|
|
echo "Illegal number of parameters" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
config_file_name=$1
|
|
|
|
webapp_files_dir=$2
|
2023-04-11 13:20:38 +00:00
|
|
|
if ! [[ -f ${config_file_name} ]]; then
|
2023-03-31 22:29:59 +00:00
|
|
|
echo "Config file ${config_file_name} does not exist" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2023-04-11 13:20:38 +00:00
|
|
|
if ! [[ -d ${webapp_files_dir} ]]; then
|
2023-03-31 22:29:59 +00:00
|
|
|
echo "Webapp directory ${webapp_files_dir} does not exist" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
# First some magic using yq to translate our yaml config file into an array of key value pairs like:
|
|
|
|
# LACONIC_HOSTED_CONFIG_<path-through-objects>=<value>
|
2023-08-22 18:32:45 +00:00
|
|
|
readarray -t config_kv_pair_array < <( yq '.. | ([path | join("_"), .] | join("=") )' ${config_file_name} | sort -r | sed -e '$ d' | sed 's/^/LACONIC_HOSTED_CONFIG_/' )
|
2023-03-31 22:29:59 +00:00
|
|
|
declare -p config_kv_pair_array
|
|
|
|
# Then iterate over that kv array making the template substitution in our web app files
|
|
|
|
for kv_pair_string in "${config_kv_pair_array[@]}"
|
|
|
|
do
|
|
|
|
kv_pair=(${kv_pair_string//=/ })
|
|
|
|
template_string_to_replace=${kv_pair[0]}
|
|
|
|
template_value_to_substitute=${kv_pair[1]}
|
2023-04-18 02:46:05 +00:00
|
|
|
template_value_to_substitute_expanded=${template_value_to_substitute//LACONIC_HOSTED_ENDPOINT/${LACONIC_HOSTED_ENDPOINT}}
|
2023-03-31 22:29:59 +00:00
|
|
|
# Run find and sed to do the substitution of one variable over all files
|
|
|
|
# See: https://stackoverflow.com/a/21479607/1701505
|
2023-04-18 02:46:05 +00:00
|
|
|
echo "Substituting: ${template_string_to_replace} = ${template_value_to_substitute_expanded}"
|
2023-03-31 22:29:59 +00:00
|
|
|
# Note: we do not escape our strings, on the expectation they do not container the '#' char.
|
2023-04-18 02:46:05 +00:00
|
|
|
find ${webapp_files_dir} -type f -exec sed -i 's#'${template_string_to_replace}'#'${template_value_to_substitute_expanded}'#g' {} +
|
2023-03-31 22:29:59 +00:00
|
|
|
done
|