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>
This commit is contained in:
1 parent
13801bec11
commit
689469eb32
3 files changed
+110
-4
No files matched your search
+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