Implements core/paths (Config/Data/State/Cache/Runtime/Log/Bin
directory resolution with canonical-vs-search-dir separation, XDG on
Unix, native APPDATA/LOCALAPPDATA/PROGRAMDATA layering on Windows,
~/Library on macOS Desktop, injectable environment/platform for
deterministic cross-platform tests) and core/config (file search,
first-match resolution, layered YAML merging, safe ${VAR} expansion,
Explain/doctor introspection) on top of the shared Application/
Kind/Scope model in the core package.
100 lines
2.4 KiB
Go
100 lines
2.4 KiB
Go
package config
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"yetano.dev/yetano/core/paths"
|
|
)
|
|
|
|
// expandTree walks a value produced by yaml.Unmarshal into interface{}
|
|
// (maps, slices, scalars) and expands ${VAR}/$VAR references in every
|
|
// string value it finds. Map keys and non-string scalars are left
|
|
// untouched (spec section 30: "no expansion of YAML keys", "expansion
|
|
// only in string values").
|
|
func expandTree(v any, env paths.Environment) any {
|
|
switch t := v.(type) {
|
|
case string:
|
|
return expandString(t, env)
|
|
case map[string]any:
|
|
out := make(map[string]any, len(t))
|
|
for k, val := range t {
|
|
out[k] = expandTree(val, env)
|
|
}
|
|
return out
|
|
case []any:
|
|
out := make([]any, len(t))
|
|
for i, val := range t {
|
|
out[i] = expandTree(val, env)
|
|
}
|
|
return out
|
|
default:
|
|
return v
|
|
}
|
|
}
|
|
|
|
func isNameStart(c byte) bool {
|
|
return c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
|
|
}
|
|
|
|
func isNameChar(c byte) bool {
|
|
return isNameStart(c) || (c >= '0' && c <= '9')
|
|
}
|
|
|
|
// expandString performs plain ${VAR} and $VAR substitution -- never a
|
|
// shell: there is no $(...), no backticks, no command execution of any
|
|
// kind, because this scanner simply has no grammar for them (spec
|
|
// section 30). A variable that is not set in env is left literally
|
|
// unexpanded (e.g. "${MISSING}" stays in the result) rather than
|
|
// silently becoming an empty string, so a typo is visible in the
|
|
// resolved configuration instead of turning into a wrong-but-valid
|
|
// path. A variable that *is* set, even to "", expands to its value --
|
|
// this is how "unset" and "set but empty" are told apart (spec section
|
|
// 30).
|
|
func expandString(s string, env paths.Environment) string {
|
|
if !strings.Contains(s, "$") {
|
|
return s
|
|
}
|
|
var b strings.Builder
|
|
for i := 0; i < len(s); {
|
|
c := s[i]
|
|
if c != '$' || i+1 >= len(s) {
|
|
b.WriteByte(c)
|
|
i++
|
|
continue
|
|
}
|
|
if s[i+1] == '{' {
|
|
end := strings.IndexByte(s[i+2:], '}')
|
|
if end < 0 {
|
|
b.WriteByte(c)
|
|
i++
|
|
continue
|
|
}
|
|
name := s[i+2 : i+2+end]
|
|
if val, ok := env.Lookup(name); ok {
|
|
b.WriteString(val)
|
|
} else {
|
|
b.WriteString(s[i : i+2+end+1])
|
|
}
|
|
i += 2 + end + 1
|
|
continue
|
|
}
|
|
if isNameStart(s[i+1]) {
|
|
j := i + 1
|
|
for j < len(s) && isNameChar(s[j]) {
|
|
j++
|
|
}
|
|
name := s[i+1 : j]
|
|
if val, ok := env.Lookup(name); ok {
|
|
b.WriteString(val)
|
|
} else {
|
|
b.WriteString(s[i:j])
|
|
}
|
|
i = j
|
|
continue
|
|
}
|
|
b.WriteByte(c)
|
|
i++
|
|
}
|
|
return b.String()
|
|
}
|