From e86c025f596adab03627a054541a7b2ee34e5ab9 Mon Sep 17 00:00:00 2001 From: Eric Claeys <83164203+EricClaeys@users.noreply.github.com> Date: Fri, 30 Aug 2024 23:11:51 -0600 Subject: [PATCH] Update installUpgradeFunctions.sh: add update_array_field() It updates a value in an array, or deletes the array entry the value is at. --- scripts/installUpgradeFunctions.sh | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/scripts/installUpgradeFunctions.sh b/scripts/installUpgradeFunctions.sh index 340a9814e..a893d574e 100644 --- a/scripts/installUpgradeFunctions.sh +++ b/scripts/installUpgradeFunctions.sh @@ -436,6 +436,31 @@ function update_json_file() # [-d] field, new value, file, [type] } +#### +# Update a field in an array or delete the array index the field is at. +function update_array_field() +{ + local FILE="${1}" + local ARRAY="${2}" + local FIELD="${3}" # may be "" + local VALUE="${4}" + local NEW_VALUE="${5}" # a value or "--delete" + + local I="$( getJSONarrayIndex "${FILE}" "${ARRAY}" "${VALUE}" )" + [[ ${I} -eq -1 ]] && return + + if [[ ${NEW_VALUE} == "--delete" ]]; then + update_json_file -d ".${ARRAY}[${I}]" "" "${FILE}" + else + local URL=".${ARRAY}[${I}].${FIELD}" + local V="$( settings "${URL}" "${FILE}" )" + if [[ ${V} != ${NEW_VALUE} ]]; then + update_json_file "${URL}" "${NEW_VALUE}" "${FILE}" + fi + fi +} + + # Replace all the ${NEED_TO_UPDATE} placeholders. function replace_website_placeholders() {