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?
This commit is contained in:
polarathene 2023-09-04 11:44:31 +12:00
parent 6dfd553ae0
commit 01f7db0965
1 changed files with 1 additions and 2 deletions

View File

@ -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}")
}