Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
689469eb32 |
No files matched your search
@@ -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 <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.
|
||||
func (r *Resolver) Application() core.Application { return r.app }
|
||||
|
||||
|
||||
@@ -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
@@ -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 <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): 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),
|
||||
|
||||
Reference in new issue
Block a user