configcmd: config show, Doctor, KnownKey and a Layered hook

"config show [--json] [--show-secrets]" prints the configuration with
the origin of every value (secrets redacted), for every program.
Spec.Layered lets a program whose configuration is merged from several
inputs (family file, a section of it, its own file -- the yamase
family) run path, validate and show over all layers, with its own
unknown-option rules; unknown options are warnings there. Doctor(report,
spec, explicit) is the config part of a doctor command. UnknownKey
carries the layer it is in; KnownKey tells whether a dotted key is an
option of a config struct.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
terminarandClaude Opus 5.5 committed 2026-10-25 17:29:19 +01:00
1 parent 185211e304
commit fa52990e3e
7 files changed
+758 -11

No files matched your search

+37 -4
View File
@@ -6,6 +6,7 @@
// <program> config validate
// <program> config upgrade [--write]
// <program> config path
// <program> config show [--json] [--show-secrets]
//
// The command behaves the same in every program; what differs is only
// the program's own part, passed in as a Spec: its name and
@@ -33,6 +34,11 @@
// Exit codes: 0 success, 1 the command ran and found a problem (invalid
// configuration, no file found, refusing to overwrite), 2 usage error.
//
// "config show" prints the configuration with the origin of every value.
// A program merged from several inputs (a family-wide file, a section
// of it, its own file) sets Spec.Layered so that path, validate and show
// work on all of them (see Layered).
//
// The package does not depend on cobra: a cobra program wires Run into
// a command with flag parsing disabled, taking --config out of the
// arguments itself, the same way it wires core/doctor.
@@ -113,6 +119,12 @@ type Spec struct {
// Options are passed through to core/config; tests use them to
// inject a platform, environment, root and working directory.
Options []coreconfig.Option
// Layered, when set, makes path, validate and show work on a
// configuration merged from several inputs (see Layered); create
// and upgrade still work on Filename. nil: the program reads the
// single file Find selects.
Layered *Layered
}
func (s Spec) filename() string {
@@ -131,7 +143,7 @@ func Run(s Spec, explicit string, args []string, stdout, stderr io.Writer) int {
}
sub, rest := args[0], args[1:]
switch sub {
case "create", "validate", "upgrade", "path":
case "create", "validate", "upgrade", "path", "show":
if s.None {
fmt.Fprintf(stdout, "%s has no configuration file\n", s.Name)
return ExitOK
@@ -141,11 +153,25 @@ func Run(s Spec, explicit string, args []string, stdout, stderr io.Writer) int {
case "create":
return s.create(explicit, rest, stdout, stderr)
case "validate":
if s.Layered != nil {
if code := parse(s.newFlagSet("validate", stderr), rest); code >= 0 {
return code
}
return s.layeredValidate(explicit, stdout, stderr)
}
return s.validate(explicit, rest, stdout, stderr)
case "upgrade":
return s.upgrade(explicit, rest, stdout, stderr)
case "path":
if s.Layered != nil {
if code := parse(s.newFlagSet("path", stderr), rest); code >= 0 {
return code
}
return s.layeredPath(explicit, stdout, stderr)
}
return s.path(explicit, rest, stdout, stderr)
case "show":
return s.show(explicit, rest, stdout, stderr)
case "help", "-h", "-help", "--help":
usage(s, stdout)
return ExitOK
@@ -158,9 +184,13 @@ func Run(s Spec, explicit string, args []string, stdout, stderr io.Writer) int {
// Subcommands are the config subcommands, for a program's completion
// tree and help text.
var Subcommands = []string{"create", "validate", "upgrade", "path"}
var Subcommands = []string{"create", "validate", "upgrade", "path", "show"}
func usage(s Spec, w io.Writer) {
pathHelp := "list the locations searched for " + s.filename() + ", highest priority first"
if s.Layered != nil {
pathHelp = "list every configuration layer, highest priority first"
}
fmt.Fprintf(w, `usage: %[1]s config <subcommand> [--config <file>]
create [--minimal|--full] [--force] [--output <file>]
@@ -169,8 +199,11 @@ func usage(s Spec, w io.Writer) {
upgrade [--write]
show the options of "create --full" the file does not mention
yet, as commented blocks; --write adds them to the file
path list the locations searched for %[2]s, highest priority first
`, s.Name, s.filename())
path %[3]s
show [--json] [--show-secrets]
print the configuration with the file every value comes from;
secrets are redacted unless --show-secrets
`, s.Name, s.filename(), pathHelp)
}
// newFlagSet returns a flag set for one subcommand whose usage errors go