fix(onboard): fix yaml_get fallback — strip inline comments and fix greedy colon match

Two bugs in the grep+sed fallback (triggered when yq is unavailable):

1. Greedy colon match: `s/.*: *//` consumed the *last* `: ` in the line, so
   values containing a colon (e.g. `systemd:magicmirror.service`) were
   silently truncated to the portion after the last colon.
   Fix: `s/^[[:space:]]*[^:]*:[[:space:]]*//' — anchored at line start,
   key chars are `[^:]*` (no colons), so only the first `: ` separator is removed.

2. Inline YAML comment not stripped: `first_contact: pi@pimirror2.local   # ...`
   returned the full tail including `#`, breaking callers like ssh-copy-id.
   Fix: add `s/[[:space:]]\+#.*$//` — requires at least one space before `#`
   to preserve bare `#` characters inside a value.

Also add leading/trailing whitespace trim as a separate pass.
Both bugs affect any node.yaml field that has an inline comment or a colon
in its value; all ten fields in hosts/lustro/node.yaml now parse correctly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Oskar Kapala 2026-06-08 15:16:06 +02:00
parent 931fd46e62
commit eed0ad0635

View file

@ -65,8 +65,15 @@ yaml_get() {
yq -r ".${key} // empty" "$file" 2>/dev/null
else
# fallback: extract last segment of key, match " key: value"
# Strip inline YAML comment (space(s)+'#'+rest) and surrounding whitespace.
# Pattern uses \+ (BRE one-or-more) so a bare '#' inside a value is preserved.
local leaf="${key##*.}"
grep -E "^\s*${leaf}:" "$file" | head -1 | sed 's/.*: *//' | tr -d '"' | tr -d "'"
grep -E "^\s*${leaf}:" "$file" | head -1 \
| sed -e 's/^[[:space:]]*[^:]*:[[:space:]]*//' \
-e 's/[[:space:]]\+#.*$//' \
-e 's/^[[:space:]]*//' \
-e 's/[[:space:]]*$//' \
| tr -d '"' | tr -d "'"
fi
}