@@ -123,21 +123,28 @@ if test -f "${CONFIG_FILE}"; then
123
123
# create temporary default config
124
124
valhalla_build_config > ${TMP_CONFIG_FILE} || exit 1
125
125
126
- # for each path in the temp config (excluding array indices)
127
- jq -r ' paths | select(map(type) | index("number") | not ) | "." + join(".")' ${TMP_CONFIG_FILE} | while read key ; do
126
+ # collect additions to make from the temp config (treating arrays as atomic values)
127
+ additions=$( jq --slurpfile A " ${TMP_CONFIG_FILE} " --slurpfile B " ${CONFIG_FILE} " -n '
128
+ def push(p;k): p+[k];
129
+ def missing(a;b;p):
130
+ if a|type=="object" then
131
+ reduce (a|keys_unsorted[]) as $k ([];
132
+ .+(
133
+ if b|has($k)|not then [{path:push(p;$k), dot:(push(p;$k)|join(".")), value:a[$k]}]
134
+ elif (a[$k]|type=="object") and (b[$k]|type=="object") then missing(a[$k];b[$k];push(p;$k))
135
+ else [] end))
136
+ else [] end;
137
+ missing($A[0];$B[0];[])
138
+ ' )
128
139
129
- # if the key path does not exist in the existing config
130
- jq -e " ${key} | if type == \" null\" then false else true end" ${CONFIG_FILE} > /dev/null
131
- if [ $? -eq 1 ]; then
140
+ echo " $additions " | jq -r ' .[] | "added \(.dot) with value \(.value)"'
132
141
133
- # get its value from the temp config
134
- newval=$( jq " ${key} " ${TMP_CONFIG_FILE} )
135
- echo " INFO: copied new config entry ${key} =${newval} into existing config."
136
-
137
- # and set it on the new one
138
- jq --argjson d " ${newval} " " ${key} = \$ d" " ${CONFIG_FILE} " | sponge " ${CONFIG_FILE} "
139
- fi
140
- done
142
+ # add all additions
143
+ jq --argjson additions " $additions " '
144
+ reduce $additions[] as $a (.;
145
+ setpath($a.path; $a.value)
146
+ )
147
+ ' " ${CONFIG_FILE} " | sponge " ${CONFIG_FILE} "
141
148
142
149
rm ${TMP_CONFIG_FILE}
143
150
fi
0 commit comments