diff --git a/scripts/configure_valhalla.sh b/scripts/configure_valhalla.sh index 0e7f28a..dedff59 100755 --- a/scripts/configure_valhalla.sh +++ b/scripts/configure_valhalla.sh @@ -123,21 +123,28 @@ if test -f "${CONFIG_FILE}"; then # create temporary default config valhalla_build_config > ${TMP_CONFIG_FILE} || exit 1 - # for each path in the temp config (excluding array indices) - jq -r 'paths | select(map(type) | index("number") | not ) | "." + join(".")' ${TMP_CONFIG_FILE} | while read key ; do + # collect additions to make from the temp config (treating arrays as atomic values) + additions=$(jq --slurpfile A "${TMP_CONFIG_FILE}" --slurpfile B "${CONFIG_FILE}" -n ' + def push(p;k): p+[k]; + def missing(a;b;p): + if a|type=="object" then + reduce (a|keys_unsorted[]) as $k ([]; + .+( + if b|has($k)|not then [{path:push(p;$k), dot:(push(p;$k)|join(".")), value:a[$k]}] + elif (a[$k]|type=="object") and (b[$k]|type=="object") then missing(a[$k];b[$k];push(p;$k)) + else [] end)) + else [] end; + missing($A[0];$B[0];[]) + ') - # if the key path does not exist in the existing config - jq -e "${key} | if type == \"null\" then false else true end" ${CONFIG_FILE} >/dev/null - if [ $? -eq 1 ]; then + echo "$additions" | jq -r '.[] | "added \(.dot) with value \(.value)"' - # get its value from the temp config - newval=$(jq "${key}" ${TMP_CONFIG_FILE}) - echo "INFO: copied new config entry ${key}=${newval} into existing config." - - # and set it on the new one - jq --argjson d "${newval}" "${key} = \$d" "${CONFIG_FILE}"| sponge "${CONFIG_FILE}" - fi - done + # add all additions + jq --argjson additions "$additions" ' + reduce $additions[] as $a (.; + setpath($a.path; $a.value) + ) + ' "${CONFIG_FILE}" | sponge "${CONFIG_FILE}" rm ${TMP_CONFIG_FILE} fi diff --git a/tests/test.sh b/tests/test.sh index 76e7d14..c86b02a 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -1,3 +1,4 @@ +#!/bin/bash set -u valhalla_image=$1 @@ -123,7 +124,7 @@ if [[ $? -eq 1 ]]; then fi line_count=$(diff -y --suppress-common-lines <(jq --sort-keys . "${custom_file_folder}/valhalla_base.json") <(jq --sort-keys . "${custom_file_folder}/valhalla.json") | wc -l) -if [[ $line_count != "0" ]]; then +if [[ $line_count -ne 0 ]]; then echo "Valhalla config was not updated correctly. Check the generated config files." exit 1 fi @@ -144,7 +145,7 @@ if [[ $? -eq 0 ]]; then fi line_count=$(diff -y --suppress-common-lines <(jq --sort-keys . "${custom_file_folder}/valhalla_base.json") <(jq --sort-keys . "${custom_file_folder}/valhalla.json") | wc -l) -if [[ $line_count != "1" ]]; then +if [[ $line_count -ne 1 ]]; then echo "valhalla.json should not have been updated but was" exit 1 fi