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:
Nir Soffer 2024-08-24 06:23:29 +03:00
parent 0eb136226a
commit 555755133d
2 changed files with 19 additions and 1 deletions

View file

@ -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}}
`
}

View file

@ -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}