Add platform-independent path/config resolution library
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.
This commit is contained in:
1 parent
027b327c3d
commit
13801bec11
41 files changed
+4834
-2
No files matched your search
+101
@@ -0,0 +1,101 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// mergeMaps overlays over onto base in place and returns base (spec
|
||||
// section 28's V1 merge semantics):
|
||||
//
|
||||
// - scalars: the higher-priority layer's value replaces the lower one.
|
||||
// - maps: merged recursively, key by key.
|
||||
// - lists: replaced wholesale by the higher-priority layer's list --
|
||||
// never concatenated element-by-element.
|
||||
// - explicit YAML null: removes the key from the merged result (a
|
||||
// layer can use "key: null" to intentionally unset a lower layer's
|
||||
// value), rather than being stored as a literal nil.
|
||||
//
|
||||
// over is expected to be the *higher-priority* layer; base accumulates
|
||||
// lower-priority layers applied first, per LoadMerged's lowest-to-
|
||||
// highest iteration order.
|
||||
func mergeMaps(base, over map[string]any) map[string]any {
|
||||
if base == nil {
|
||||
base = map[string]any{}
|
||||
}
|
||||
for k, v := range over {
|
||||
if v == nil {
|
||||
delete(base, k)
|
||||
continue
|
||||
}
|
||||
if overMap, ok := v.(map[string]any); ok {
|
||||
if baseMap, ok := base[k].(map[string]any); ok {
|
||||
base[k] = mergeMaps(baseMap, overMap)
|
||||
continue
|
||||
}
|
||||
}
|
||||
base[k] = v
|
||||
}
|
||||
return base
|
||||
}
|
||||
|
||||
// readLayer reads and parses path as a YAML mapping, returning an empty
|
||||
// map (not an error) if the file does not exist -- callers already know
|
||||
// which layers exist via Layer.Exists; this is a convenience for callers
|
||||
// that don't check first.
|
||||
func readLayer(path string) (map[string]any, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if os.IsNotExist(err) {
|
||||
return map[string]any{}, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("config: read %s: %w", path, err)
|
||||
}
|
||||
var m map[string]any
|
||||
if err := yaml.Unmarshal(data, &m); err != nil {
|
||||
return nil, fmt.Errorf("config: parse %s: %w", path, err)
|
||||
}
|
||||
if m == nil {
|
||||
m = map[string]any{}
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// LoadMerged loads every existing layer, lowest priority first, merging
|
||||
// each one on top of the last (spec sections 27/28), optionally
|
||||
// expanding ${VAR}/$VAR in string values (WithEnvExpansion, default
|
||||
// on), and unmarshals the merged result into target (a pointer, as for
|
||||
// yaml.Unmarshal).
|
||||
func (r *Resolver) LoadMerged(target any) error {
|
||||
all, err := r.layers()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
merged := map[string]any{}
|
||||
for i := len(all) - 1; i >= 0; i-- {
|
||||
l := all[i]
|
||||
if !l.Exists {
|
||||
continue
|
||||
}
|
||||
m, err := readLayer(l.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if r.envExpansion {
|
||||
m = expandTree(m, r.env).(map[string]any)
|
||||
}
|
||||
merged = mergeMaps(merged, m)
|
||||
}
|
||||
|
||||
data, err := yaml.Marshal(merged)
|
||||
if err != nil {
|
||||
return fmt.Errorf("config: re-marshal merged result: %w", err)
|
||||
}
|
||||
if err := yaml.Unmarshal(data, target); err != nil {
|
||||
return fmt.Errorf("config: unmarshal merged result: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user