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:
terminar committed 2026-09-19 16:26:01 +02:00
1 parent 027b327c3d
commit 13801bec11
41 files changed
+4834 -2

No files matched your search

+169
View File
@@ -0,0 +1,169 @@
// Package config resolves, searches, and layers a Yetano application's
// configuration *files* -- built on top of core/paths, which only
// resolves *directories* and knows nothing about file contents (see
// AGENTS.md: "paths must not implicitly parse configuration").
//
// A Resolver is obtained via For (or New):
//
// app := core.Application{Name: "yet", Kind: core.CLI}
// cfg := config.For(app)
//
// selected, found, err := cfg.Resolve() // highest-priority existing file
// all, err := cfg.ResolveAll() // every layer, with metadata
//
// var target MyConfig
// err = cfg.LoadMerged(&target) // lowest-to-highest priority merge
//
// # Search order and merge order are the same hierarchy
//
// Resolve/ResolveAll walk the priority hierarchy highest-to-lowest
// (first match wins); LoadMerged walks the exact same hierarchy
// lowest-to-highest (each layer overlays the previous one). Both derive
// from one ordered []Layer -- see layers() in layers.go -- so there is
// never a second, independently-maintained ordering to keep in sync.
//
// # Priority hierarchy
//
// Highest to lowest, on Unix:
//
// working directory (./) -- toggle: WithWorkingDirectory
// <app>'s user config dir (paths.ConfigDir/ConfigSearchDirs)
// <app>'s system config dir -- toggle: WithSystemConfig
// the shared Yetano config dir -- toggle: WithYetanoDefaults (opt-in, off by default)
//
// On Windows, the user/system tier expands to the same three-layer
// LOCALAPPDATA > APPDATA > PROGRAMDATA search core/paths already
// resolves (see core/paths's package doc); config only adds the working
// directory layer on top and the Yetano-defaults layer below.
//
// # Environment expansion
//
// A loaded file's string values may reference ${VAR}/$VAR (see
// expand.go); this is plain variable substitution, never a shell -- no
// $(...), no backticks, no command execution of any kind.
package config
import (
"yetano.dev/yetano/core"
"yetano.dev/yetano/core/paths"
)
// Filename derives the default configuration file name for an
// application: "<name>.yaml".
func Filename(app core.Application) string {
return app.Name + ".yaml"
}
// Resolver resolves, searches, and layers one application's
// configuration files.
type Resolver struct {
app core.Application
paths *paths.Resolver
filename string
workdir bool
workdirPath string
systemConfig bool
yetanoDefaults bool
envExpansion bool
env paths.Environment
pathsOpts []paths.Option
}
// Option configures a Resolver constructed by New/For.
type Option func(*Resolver)
// New constructs a Resolver for app.
func New(app core.Application, opts ...Option) (*Resolver, error) {
if err := app.Validate(); err != nil {
return nil, err
}
r := &Resolver{
app: app,
filename: Filename(app),
workdir: true,
workdirPath: ".",
systemConfig: true,
envExpansion: true,
env: paths.OSEnvironment,
}
for _, opt := range opts {
opt(r)
}
pr, err := paths.New(app, append([]paths.Option{paths.WithEnvironmentProvider(r.env)}, r.pathsOpts...)...)
if err != nil {
return nil, err
}
r.paths = pr
return r, nil
}
// For is an alias for New, matching the spec's `config.For(app)` spelling.
func For(app core.Application, opts ...Option) (*Resolver, error) {
return New(app, opts...)
}
// WithFilename overrides the configuration file name (default
// "<app-name>.yaml").
func WithFilename(name string) Option {
return func(r *Resolver) { r.filename = name }
}
// WithWorkingDirectory enables or disables the working-directory layer
// (spec section 23) -- important to disable for services, which have no
// meaningful "current directory" a human invoked them from.
func WithWorkingDirectory(enabled bool) Option {
return func(r *Resolver) { r.workdir = enabled }
}
// WithWorkdirPath overrides the directory used for the working-directory
// layer (default "."). Mainly useful for deterministic tests.
func WithWorkdirPath(path string) Option {
return func(r *Resolver) { r.workdirPath = path }
}
// WithSystemConfig enables or disables the system-wide configuration
// layer (/etc/<app>, %PROGRAMDATA%\<app>, ...). Enabled by default.
func WithSystemConfig(enabled bool) Option {
return func(r *Resolver) { r.systemConfig = enabled }
}
// WithYetanoDefaults enables the shared, ecosystem-wide Yetano
// configuration directory (spec section 4) as the lowest-priority
// fallback layer. Disabled by default: an application must opt in.
func WithYetanoDefaults(enabled bool) Option {
return func(r *Resolver) { r.yetanoDefaults = enabled }
}
// WithEnvExpansion enables or disables ${VAR}/$VAR expansion of string
// values when loading a file (LoadMerged). Enabled by default.
func WithEnvExpansion(enabled bool) Option {
return func(r *Resolver) { r.envExpansion = enabled }
}
// WithEnvironment injects a fixed environment variable map, used both
// for ${VAR} expansion and (via WithEnvironmentProvider on the
// underlying paths.Resolver) for locating the configuration directories
// themselves -- so a test can set HOME once and have it apply
// consistently everywhere.
func WithEnvironment(env map[string]string) Option {
return func(r *Resolver) { r.env = paths.MapEnvironment(env) }
}
// WithEnvironmentProvider injects a custom paths.Environment
// implementation, for the same purposes as WithEnvironment.
func WithEnvironmentProvider(env paths.Environment) Option {
return func(r *Resolver) { r.env = env }
}
// WithPathsOptions passes additional options through to the underlying
// core/paths.Resolver (e.g. paths.WithPlatform, paths.WithRoot).
func WithPathsOptions(opts ...paths.Option) Option {
return func(r *Resolver) { r.pathsOpts = append(r.pathsOpts, opts...) }
}
// Application returns the Application this Resolver was built for.
func (r *Resolver) Application() core.Application { return r.app }
// Filename returns the configuration file name this Resolver searches
// for in every layer.
func (r *Resolver) Filename() string { return r.filename }