configfamily: one config file per program plus an optional family file
The generic part of yamase-core/pkg/configfile (yamase ADR-0074): per tier a program merges the family file's top level, its section of the family file and its own <program>.yaml, then --config. Includes unknown-option rules for shared files, the config command on top of configcmd.Layered with "create --shared", and doctor/logging helpers. yaiam is the first user; yamase can move onto it later. configcmd: a layered Optional program without any file now validates its defaults and passes doctor instead of failing, as the single-file path already did. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5471863163
commit
c11197bb37
9 files changed
+979
-3
No files matched your search
@@ -85,8 +85,8 @@ type Spec struct {
|
||||
Filename string
|
||||
|
||||
// Optional says the program also runs without a configuration file
|
||||
// (on built-in defaults): "validate" and "upgrade" then report that
|
||||
// no file exists instead of failing.
|
||||
// (on built-in defaults): "validate", "upgrade", "show" and Doctor
|
||||
// then report that no file exists instead of failing.
|
||||
Optional bool
|
||||
|
||||
// None says the program has no configuration at all; every
|
||||
|
||||
+16
-1
@@ -39,7 +39,8 @@ type Layered struct {
|
||||
Unknown func(e *Effective) ([]UnknownKey, error)
|
||||
|
||||
// Validate parses and validates the merged configuration (YAML) the
|
||||
// way the program does at startup, including files it names.
|
||||
// way the program does at startup, including files it names. For an
|
||||
// Optional program without any file it gets nil (the defaults).
|
||||
Validate func(merged []byte) error
|
||||
|
||||
// Used reports whether the program reads the option key, for "config
|
||||
@@ -179,6 +180,16 @@ func (s Spec) layeredPath(explicit string, stdout, stderr io.Writer) int {
|
||||
|
||||
func (s Spec) layeredValidate(explicit string, stdout, stderr io.Writer) int {
|
||||
e, err := s.Layered.Load(explicit)
|
||||
if err != nil && s.Optional && explicit == "" && errors.Is(err, ErrNotFound) {
|
||||
if s.Layered.Validate != nil {
|
||||
if err := s.Layered.Validate(nil); err != nil {
|
||||
fmt.Fprintf(stderr, "%s: no configuration file, and the built-in defaults are invalid: %v\n", s.Name, err)
|
||||
return ExitProblem
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(stdout, "no configuration file found -- %s runs on its built-in defaults (\"%s config path\" lists the locations)\n", s.Name, s.Name)
|
||||
return ExitOK
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "%s config validate: %v\n", s.Name, err)
|
||||
return ExitProblem
|
||||
@@ -273,6 +284,10 @@ func Doctor(r *doctor.Report, s Spec, explicit string) *Effective {
|
||||
}
|
||||
}
|
||||
e, err := s.effective(explicit)
|
||||
if err != nil && s.Optional && explicit == "" && errors.Is(err, ErrNotFound) {
|
||||
r.Check("config file found", true, "none, "+s.Name+" runs on its built-in defaults")
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
note := err.Error()
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
|
||||
Reference in new issue
Block a user