From 689469eb32689eba6e7c4192a9ece966641dfbf9 Mon Sep 17 00:00:00 2001 From: Bjoern Kalkbrenner Date: Sat, 19 Sep 2026 16:50:22 +0200 Subject: [PATCH] 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 --- config/config.go | 12 +++++++ config/explicit_test.go | 75 +++++++++++++++++++++++++++++++++++++++++ config/layers.go | 27 ++++++++++++--- 3 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 config/explicit_test.go diff --git a/config/config.go b/config/config.go index 93e86b9..b8348e8 100644 --- a/config/config.go +++ b/config/config.go @@ -67,6 +67,7 @@ type Resolver struct { envExpansion bool env paths.Environment pathsOpts []paths.Option + explicitPath string } // 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...) } } +// WithExplicitPath adds path as the highest-priority layer -- above the +// working directory -- for a CLI's own explicit "--config "-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. func (r *Resolver) Application() core.Application { return r.app } diff --git a/config/explicit_test.go b/config/explicit_test.go new file mode 100644 index 0000000..ba282f5 --- /dev/null +++ b/config/explicit_test.go @@ -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) + } + } +} diff --git a/config/layers.go b/config/layers.go index 2e20c65..c979da2 100644 --- a/config/layers.go +++ b/config/layers.go @@ -1,6 +1,7 @@ package config import ( + "fmt" "os" "yetano.dev/yetano/core" @@ -25,18 +26,36 @@ type Layer struct { // the last fallback. const PriorityYetanoDefaults = 0 +// PriorityExplicit is higher than every other layer (including the +// working directory), for WithExplicitPath's "--config " 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): 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. +// 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),