Files
core/config/layers.go
T
terminarandClaude Sonnet 5 689469eb32 config: add WithExplicitPath for a CLI's own --config flag
A highest-priority, must-exist override layer above the working
directory -- the common "flag always wins, but a typo'd path should
fail loudly" pattern most Yetano CLIs need, generalized here instead
of reimplementing it in each application (first consumer: yet's own
--config flag, see the yet submodule).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-19 16:50:22 +02:00

131 lines
3.5 KiB
Go

package config
import (
"fmt"
"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
// PriorityExplicit is higher than every other layer (including the
// working directory), for WithExplicitPath's "--config <path>" use
// case: an explicit override always wins, no exceptions.
const PriorityExplicit = 1000
func statExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
// layers builds the full, ordered layer list (spec sections 21/22): an
// explicit override path (if set; see WithExplicitPath), then 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.explicitPath != "" {
if !statExists(r.explicitPath) {
return nil, fmt.Errorf("config: explicit path %s does not exist", r.explicitPath)
}
out = append(out, Layer{
Path: r.explicitPath,
Source: paths.APIOverride,
Priority: PriorityExplicit,
})
}
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
}