Compare commits
No commits in common. "8fa13ae52bd5a36c89bf366899ce46cf5dc6e9db" and "9d8b1c594874c51ffa86deb321fb4d8dc58f7bc3" have entirely different histories.
8fa13ae52b
...
9d8b1c5948
@ -4,34 +4,9 @@
|
|||||||
# - keep the user's existing value for any key the template still defines
|
# - keep the user's existing value for any key the template still defines
|
||||||
# - add new template keys (CFG_REQUIREMENT_CONFIGS_AUTO_UPDATE)
|
# - add new template keys (CFG_REQUIREMENT_CONFIGS_AUTO_UPDATE)
|
||||||
# - drop keys the template no longer defines (CFG_REQUIREMENT_CONFIGS_AUTO_DELETE)
|
# - drop keys the template no longer defines (CFG_REQUIREMENT_CONFIGS_AUTO_DELETE)
|
||||||
# - take the COMMENT (title, description, [options], **DEV** / **ADVANCED**
|
|
||||||
# markers) from the template, so label / metadata / marker changes shipped
|
|
||||||
# in a release reach existing installs without nuking user values.
|
|
||||||
# Structure, ordering and comments follow the template. Non-interactive, atomic,
|
# Structure, ordering and comments follow the template. Non-interactive, atomic,
|
||||||
# and keeps a <file>.bak. Refuses to act on a missing/empty template so a broken
|
# and keeps a <file>.bak. Refuses to act on a missing/empty template so a broken
|
||||||
# clone can never wipe a live config.
|
# clone can never wipe a live config.
|
||||||
|
|
||||||
# Split `CFG_KEY=value...#comment` into its value-part and comment-part.
|
|
||||||
# Used by reconcileConfigFile to swap the template's comment onto the user's
|
|
||||||
# value. Sets _reconcile_value and _reconcile_comment as side outputs.
|
|
||||||
_reconcileSplitValueComment()
|
|
||||||
{
|
|
||||||
local line="$1"
|
|
||||||
# Drop the "CFG_KEY=" prefix; remainder is value [whitespace #comment].
|
|
||||||
local rest="${line#*=}"
|
|
||||||
if [[ "$rest" == *"#"* ]]; then
|
|
||||||
_reconcile_value="${rest%%#*}"
|
|
||||||
# Strip trailing whitespace from the value half.
|
|
||||||
_reconcile_value="${_reconcile_value%"${_reconcile_value##*[![:space:]]}"}"
|
|
||||||
_reconcile_comment="#${rest#*#}"
|
|
||||||
else
|
|
||||||
_reconcile_value="$rest"
|
|
||||||
# Strip trailing whitespace from the value half.
|
|
||||||
_reconcile_value="${_reconcile_value%"${_reconcile_value##*[![:space:]]}"}"
|
|
||||||
_reconcile_comment=""
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
reconcileConfigFile()
|
reconcileConfigFile()
|
||||||
{
|
{
|
||||||
local live="$1" template="$2"
|
local live="$1" template="$2"
|
||||||
@ -41,34 +16,18 @@ reconcileConfigFile()
|
|||||||
[[ -f "$live" ]] || return 0
|
[[ -f "$live" ]] || return 0
|
||||||
runInstallOp test -s "$template" 2>/dev/null || return 0
|
runInstallOp test -s "$template" 2>/dev/null || return 0
|
||||||
|
|
||||||
declare -A live_value emitted
|
declare -A live_line emitted
|
||||||
local line key
|
local line key
|
||||||
while IFS= read -r line; do
|
while IFS= read -r line; do
|
||||||
if [[ "$line" =~ ^(CFG_[A-Za-z0-9_]+)= ]]; then
|
[[ "$line" =~ ^(CFG_[A-Za-z0-9_]+)= ]] && live_line["${BASH_REMATCH[1]}"]="$line"
|
||||||
key="${BASH_REMATCH[1]}"
|
|
||||||
_reconcileSplitValueComment "$line"
|
|
||||||
live_value["$key"]="$_reconcile_value"
|
|
||||||
fi
|
|
||||||
done < <(runInstallOp cat "$live")
|
done < <(runInstallOp cat "$live")
|
||||||
|
|
||||||
local tmp; tmp=$(mktemp)
|
local tmp; tmp=$(mktemp)
|
||||||
local tmpl_value tmpl_comment merged
|
|
||||||
while IFS= read -r line; do
|
while IFS= read -r line; do
|
||||||
if [[ "$line" =~ ^(CFG_[A-Za-z0-9_]+)= ]]; then
|
if [[ "$line" =~ ^(CFG_[A-Za-z0-9_]+)= ]]; then
|
||||||
key="${BASH_REMATCH[1]}"
|
key="${BASH_REMATCH[1]}"
|
||||||
_reconcileSplitValueComment "$line"
|
if [[ -n "${live_line[$key]+x}" ]]; then
|
||||||
tmpl_value="$_reconcile_value"
|
printf '%s\n' "${live_line[$key]}" >> "$tmp" # keep the user's value
|
||||||
tmpl_comment="$_reconcile_comment"
|
|
||||||
|
|
||||||
if [[ -n "${live_value[$key]+x}" ]]; then
|
|
||||||
# Keep the user's value, take the template's comment (so
|
|
||||||
# title/description/options/markers stay in sync with the repo).
|
|
||||||
if [[ -n "$tmpl_comment" ]]; then
|
|
||||||
merged="${key}=${live_value[$key]} ${tmpl_comment}"
|
|
||||||
else
|
|
||||||
merged="${key}=${live_value[$key]}"
|
|
||||||
fi
|
|
||||||
printf '%s\n' "$merged" >> "$tmp"
|
|
||||||
emitted["$key"]=1
|
emitted["$key"]=1
|
||||||
elif [[ "$do_add" == "true" ]]; then
|
elif [[ "$do_add" == "true" ]]; then
|
||||||
printf '%s\n' "$line" >> "$tmp" # new key, template default
|
printf '%s\n' "$line" >> "$tmp" # new key, template default
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user