From 01f7db09656790208121726b72c7525a6f9c9364 Mon Sep 17 00:00:00 2001 From: polarathene <5098581+polarathene@users.noreply.github.com> Date: Mon, 4 Sep 2023 11:44:31 +1200 Subject: [PATCH] chore: Preserve K/V white-space of original source No need to check for a non-empty value to prepend a space (_since an empty value is used via sed anyway?_). Can also be a bit DRY with the sed pattern, matching the key + delimiter (_and all white-space before/after the delimiter until the value_), then capture that for the replacement left-side value while only actually swapping the value for the ENV input value. Should be an improvement, unless there is a scenario that would differ between `` and ` ` as valid value assignments? --- target/scripts/helpers/utils.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/target/scripts/helpers/utils.sh b/target/scripts/helpers/utils.sh index d2d31659..55ad87bf 100644 --- a/target/scripts/helpers/utils.sh +++ b/target/scripts/helpers/utils.sh @@ -129,9 +129,8 @@ function _replace_by_env_in_file() { KEY=${KEY#"${ENV_PREFIX}"} # strip prefix ESCAPED_KEY=$(_escape_for_sed "${KEY,,}") ESCAPED_VALUE=$(_escape_for_sed "${VALUE}") - [[ -n ${ESCAPED_VALUE} ]] && ESCAPED_VALUE=" ${ESCAPED_VALUE}" _log 'trace' "Setting value of '${KEY}' in '${CONFIG_FILE}' to '${VALUE}'" - sed -i -E "s#^${ESCAPED_KEY}[[:space:]]*${KV_DELIMITER}.*#${ESCAPED_KEY} ${KV_DELIMITER}${ESCAPED_VALUE}#g" "${CONFIG_FILE}" + sed -i -E "s#^(${ESCAPED_KEY}[[:space:]]*${KV_DELIMITER}[[:space:]]*).*#\1${ESCAPED_VALUE}#g" "${CONFIG_FILE}" done < <(env | grep "^${ENV_PREFIX}") }