mirror of
https://github.com/spf13/cobra
synced 2025-04-27 17:17:20 +00:00
Use display name in version template
The version template used `{{.Name}}` but for plugins you want to use `{{.DisplayName}}` to be consistent with other help output. With this change will show: $ kubectl plugin --version kubectl plugin version 1.0.0 This may cause issues for programs processing the output if the program assumes that the name of the program never contains spaces, and the version string is the third word. Users can use their own template if they think that this is an issue. If we think that this can break users we can drop this change and let users opt in by setting their own template using `{{.DisplayName}}`.
This commit is contained in:
parent
0eb136226a
commit
555755133d
2 changed files with 19 additions and 1 deletions
|
@ -607,7 +607,7 @@ func (c *Command) VersionTemplate() string {
|
|||
if c.HasParent() {
|
||||
return c.parent.VersionTemplate()
|
||||
}
|
||||
return `{{with .Name}}{{printf "%s " .}}{{end}}{{printf "version %s" .Version}}
|
||||
return `{{with .DisplayName}}{{printf "%s " .}}{{end}}{{printf "version %s" .Version}}
|
||||
`
|
||||
}
|
||||
|
||||
|
|
|
@ -1094,6 +1094,24 @@ func TestVersionFlagExecuted(t *testing.T) {
|
|||
checkStringContains(t, output, "root version 1.0.0")
|
||||
}
|
||||
|
||||
func TestVersionFlagExecutedDiplayName(t *testing.T) {
|
||||
rootCmd := &Command{
|
||||
Use: "kubectl-plugin",
|
||||
Version: "1.0.0",
|
||||
Annotations: map[string]string{
|
||||
CommandDisplayNameAnnotation: "kubectl plugin",
|
||||
},
|
||||
Run: emptyRun,
|
||||
}
|
||||
|
||||
output, err := executeCommand(rootCmd, "--version", "arg1")
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
checkStringContains(t, output, "kubectl plugin version 1.0.0")
|
||||
}
|
||||
|
||||
func TestVersionFlagExecutedWithNoName(t *testing.T) {
|
||||
rootCmd := &Command{Version: "1.0.0", Run: emptyRun}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue