configcmd.OneOf for alternative fields; completion.Files for paths
UndocumentedKeys reported the unused alternatives of a struct like SecretRef (env/file/value) as undocumented; a struct implementing the marker configcmd.OneOf now counts as documented with one alternative, and upgrade offers none of the others to a file that chose one. completion.Tree gets the key completion.Files: that level completes file and directory paths besides command names (bash, zsh, fish, PowerShell), for programs started as "<program> [path]" like yamc. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
83f4228e66
commit
131d82642c
6 files changed
+244
-13
No files matched your search
@@ -427,3 +427,74 @@ func TestFilenameAndFind(t *testing.T) {
|
||||
t.Fatalf("Find: %v, %v", l.Path, err)
|
||||
}
|
||||
}
|
||||
|
||||
type secretRef struct {
|
||||
Env string `yaml:"env,omitempty"`
|
||||
File string `yaml:"file,omitempty"`
|
||||
Value string `yaml:"value,omitempty"`
|
||||
}
|
||||
|
||||
func (secretRef) ConfigOneOf() {}
|
||||
|
||||
type oneOfSchema struct {
|
||||
Auth struct {
|
||||
Type string `yaml:"type"`
|
||||
Token secretRef `yaml:"token"`
|
||||
Email *secretRef `yaml:"email"`
|
||||
} `yaml:"auth"`
|
||||
}
|
||||
|
||||
const oneOfTemplate = `auth:
|
||||
type: bearer
|
||||
# exactly one of env, file or value
|
||||
token:
|
||||
env: TOKEN
|
||||
email:
|
||||
file: /run/secrets/email
|
||||
`
|
||||
|
||||
func TestUndocumentedKeysOneOf(t *testing.T) {
|
||||
got, err := UndocumentedKeys(oneOfTemplate, oneOfSchema{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != 0 {
|
||||
t.Errorf("UndocumentedKeys(one alternative set) = %v, want none", got)
|
||||
}
|
||||
// No alternative in the template: every one is undocumented.
|
||||
got, _ = UndocumentedKeys("auth:\n type: bearer\n token: {}\n email:\n env: E\n", oneOfSchema{})
|
||||
if want := "auth.token.env auth.token.file auth.token.value"; strings.Join(got, " ") != want {
|
||||
t.Errorf("UndocumentedKeys(empty token) = %v, want %s", got, want)
|
||||
}
|
||||
// A struct without the marker still needs every field.
|
||||
type plain struct {
|
||||
Token struct {
|
||||
Env string `yaml:"env"`
|
||||
File string `yaml:"file"`
|
||||
} `yaml:"token"`
|
||||
}
|
||||
got, _ = UndocumentedKeys("token:\n env: X\n", plain{})
|
||||
if want := "token.file"; strings.Join(got, " ") != want {
|
||||
t.Errorf("UndocumentedKeys(plain) = %v, want %s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMissingOneOfOffersNoOtherAlternative(t *testing.T) {
|
||||
file := "auth:\n type: bearer\n token:\n file: /run/secrets/token\n email:\n value: me@example.org\n"
|
||||
got, err := Missing([]byte(file), []byte(oneOfTemplate), oneOfSchema{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != 0 {
|
||||
t.Errorf("Missing() = %v, want nothing (each secret has its alternative)", got)
|
||||
}
|
||||
// Without the schema the template's alternative is offered as before.
|
||||
got, _ = Missing([]byte(file), []byte(oneOfTemplate), nil)
|
||||
var keys []string
|
||||
for _, a := range got {
|
||||
keys = append(keys, a.Key)
|
||||
}
|
||||
if want := "auth.token.env auth.email.file"; strings.Join(keys, " ") != want {
|
||||
t.Errorf("Missing(no schema) = %v, want %s", keys, want)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user