1 Commits
Author SHA1 Message Date
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
3 changed files with 110 additions and 4 deletions

No files matched your search

+12
View File
@@ -67,6 +67,7 @@ type Resolver struct {
envExpansion bool envExpansion bool
env paths.Environment env paths.Environment
pathsOpts []paths.Option pathsOpts []paths.Option
explicitPath string
} }
// Option configures a Resolver constructed by New/For. // Option configures a Resolver constructed by New/For.
@@ -161,6 +162,17 @@ func WithPathsOptions(opts ...paths.Option) Option {
return func(r *Resolver) { r.pathsOpts = append(r.pathsOpts, opts...) } return func(r *Resolver) { r.pathsOpts = append(r.pathsOpts, opts...) }
} }
// WithExplicitPath adds path as the highest-priority layer -- above the
// working directory -- for a CLI's own explicit "--config <path>"-style
// flag. Unlike every other (optional) layer, an explicit path that does
// not exist is an error from Resolve/ResolveAll/LoadMerged/Explain,
// rather than being silently skipped: a typo'd --config flag should
// fail loudly, not fall through to a different layer. Passing "" (the
// default) disables this layer entirely.
func WithExplicitPath(path string) Option {
return func(r *Resolver) { r.explicitPath = path }
}
// Application returns the Application this Resolver was built for. // Application returns the Application this Resolver was built for.
func (r *Resolver) Application() core.Application { return r.app } func (r *Resolver) Application() core.Application { return r.app }
+75
View File
@@ -0,0 +1,75 @@
package config
import (
"path/filepath"
"testing"
"yetano.dev/yetano/core/paths"
)
func TestWithExplicitPathWinsOverEverything(t *testing.T) {
root := t.TempDir()
writeFile(t, filepath.Join(root, "etc", "yet", "yet.yaml"), "layer: system\n")
writeFile(t, filepath.Join(root, "home", "test", ".config", "yet", "yet.yaml"), "layer: user\n")
explicit := filepath.Join(root, "explicit.yaml")
writeFile(t, explicit, "layer: explicit\n")
env := map[string]string{"HOME": "/home/test"}
r := mustResolver(t, yetApp(),
WithEnvironment(env),
WithPathsOptions(paths.WithPlatform("linux"), paths.WithRoot(root)),
WithWorkdirPath(filepath.Join(root, "workdir")),
WithExplicitPath(explicit),
)
layer, found, err := r.Resolve()
if err != nil {
t.Fatalf("Resolve: %v", err)
}
if !found || layer.Path != explicit {
t.Fatalf("Resolve() = %+v, found=%v, want the explicit path to win", layer, found)
}
if layer.Priority != PriorityExplicit {
t.Errorf("layer.Priority = %d, want %d", layer.Priority, PriorityExplicit)
}
}
func TestWithExplicitPathMissingIsAnError(t *testing.T) {
root := t.TempDir()
env := map[string]string{"HOME": "/home/test"}
r := mustResolver(t, yetApp(),
WithEnvironment(env),
WithPathsOptions(paths.WithPlatform("linux"), paths.WithRoot(root)),
WithExplicitPath(filepath.Join(root, "does-not-exist.yaml")),
)
if _, _, err := r.Resolve(); err == nil {
t.Fatal("Resolve() error = nil, want an error for a missing explicit path")
}
if _, err := r.ResolveAll(); err == nil {
t.Fatal("ResolveAll() error = nil, want an error for a missing explicit path")
}
var target struct{}
if err := r.LoadMerged(&target); err == nil {
t.Fatal("LoadMerged() error = nil, want an error for a missing explicit path")
}
}
func TestWithExplicitPathEmptyDisablesLayer(t *testing.T) {
root := t.TempDir()
env := map[string]string{"HOME": "/home/test"}
r := mustResolver(t, yetApp(),
WithEnvironment(env),
WithPathsOptions(paths.WithPlatform("linux"), paths.WithRoot(root)),
WithWorkdirPath(filepath.Join(root, "workdir")),
WithExplicitPath(""),
)
layers, err := r.ResolveAll()
if err != nil {
t.Fatalf("ResolveAll: %v", err)
}
for _, l := range layers {
if l.Priority == PriorityExplicit {
t.Fatalf("ResolveAll() included an explicit layer despite WithExplicitPath(\"\"): %v", layers)
}
}
}
+23 -4
View File
@@ -1,6 +1,7 @@
package config package config
import ( import (
"fmt"
"os" "os"
"yetano.dev/yetano/core" "yetano.dev/yetano/core"
@@ -25,18 +26,36 @@ type Layer struct {
// the last fallback. // the last fallback.
const PriorityYetanoDefaults = 0 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 { func statExists(path string) bool {
_, err := os.Stat(path) _, err := os.Stat(path)
return err == nil return err == nil
} }
// layers builds the full, ordered layer list (spec sections 21/22): the // layers builds the full, ordered layer list (spec sections 21/22): an
// working directory (if enabled), then core/paths's own ConfigSearchDirs // explicit override path (if set; see WithExplicitPath), then the
// for this application (optionally dropping the system-tier layer), then // working directory (if enabled), then core/paths's own
// -- opt-in only -- the shared Yetano defaults directory. // 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) { func (r *Resolver) layers() ([]Layer, error) {
var out []Layer 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 { if r.workdir {
out = append(out, Layer{ out = append(out, Layer{
Path: paths.JoinFamily(r.paths.Platform(), r.workdirPath, r.filename), Path: paths.JoinFamily(r.paths.Platform(), r.workdirPath, r.filename),