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
@@ -0,0 +1,111 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"yetano.dev/yetano/core"
|
||||
"yetano.dev/yetano/core/paths"
|
||||
)
|
||||
|
||||
// Layer is one location in the configuration search/merge hierarchy
|
||||
// (spec section 48). Search order is descending Priority (first match
|
||||
// wins); LoadMerged walks the exact same list in reverse (lowest
|
||||
// priority first, so a higher-priority layer overlays it).
|
||||
type Layer struct {
|
||||
Path string
|
||||
Source paths.Source
|
||||
Priority int
|
||||
Exists bool
|
||||
Selected bool
|
||||
}
|
||||
|
||||
// PriorityYetanoDefaults is lower than any layer core/paths itself
|
||||
// resolves (see paths.PrioritySystem), so the shared Yetano config
|
||||
// directory (spec section 4, opt-in via WithYetanoDefaults) is always
|
||||
// the last fallback.
|
||||
const PriorityYetanoDefaults = 0
|
||||
|
||||
func statExists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// layers builds the full, ordered layer list (spec sections 21/22): the
|
||||
// working directory (if enabled), then core/paths's own ConfigSearchDirs
|
||||
// for this application (optionally dropping the system-tier layer), then
|
||||
// -- opt-in only -- the shared Yetano defaults directory.
|
||||
func (r *Resolver) layers() ([]Layer, error) {
|
||||
var out []Layer
|
||||
|
||||
if r.workdir {
|
||||
out = append(out, Layer{
|
||||
Path: paths.JoinFamily(r.paths.Platform(), r.workdirPath, r.filename),
|
||||
Source: paths.WorkingDirectory,
|
||||
Priority: paths.PriorityWorkingDirectory,
|
||||
})
|
||||
}
|
||||
|
||||
dirs, err := r.paths.ConfigSearchDirs()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, d := range dirs {
|
||||
if !r.systemConfig && d.Priority <= paths.PrioritySystem {
|
||||
continue
|
||||
}
|
||||
out = append(out, Layer{
|
||||
Path: paths.JoinDirFile(d.Path, r.filename),
|
||||
Source: d.Source,
|
||||
Priority: d.Priority,
|
||||
})
|
||||
}
|
||||
|
||||
if r.yetanoDefaults {
|
||||
layer, err := r.yetanoDefaultsLayer()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, layer)
|
||||
}
|
||||
|
||||
for i := range out {
|
||||
out[i].Exists = statExists(out[i].Path)
|
||||
}
|
||||
selected := false
|
||||
for i := range out {
|
||||
if out[i].Exists && !selected {
|
||||
out[i].Selected = true
|
||||
selected = true
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// yetanoDefaultsLayer resolves the ecosystem-wide shared configuration
|
||||
// directory (spec section 4: "~/.config/yetano/", or the platform
|
||||
// equivalent) as its own pseudo-Application named "yetano" -- always
|
||||
// CLI-shaped (XDG-flavored on every OS), since it is a shared,
|
||||
// admin/CLI-facing location, not tied to any one application's Kind.
|
||||
func (r *Resolver) yetanoDefaultsLayer() (Layer, error) {
|
||||
yetanoApp := core.Application{Name: "yetano", Kind: core.CLI, Scope: r.app.Scope}
|
||||
yp, err := paths.New(yetanoApp, append([]paths.Option{paths.WithEnvironmentProvider(r.env)}, r.pathsOpts...)...)
|
||||
if err != nil {
|
||||
return Layer{}, err
|
||||
}
|
||||
ex, err := yp.Explain()
|
||||
if err != nil {
|
||||
return Layer{}, err
|
||||
}
|
||||
var dir string
|
||||
var source paths.Source
|
||||
for _, p := range ex.Paths {
|
||||
if p.Kind == paths.ConfigPath {
|
||||
dir, source = p.Path, p.Source
|
||||
}
|
||||
}
|
||||
return Layer{
|
||||
Path: paths.JoinDirFile(dir, r.filename),
|
||||
Source: source,
|
||||
Priority: PriorityYetanoDefaults,
|
||||
}, nil
|
||||
}
|
||||
Reference in new issue
Block a user