Skip to content

Commit

Permalink
Use display name in version template
Browse files Browse the repository at this point in the history
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}}`.
  • Loading branch information
nirs committed Aug 24, 2024
1 parent 0eb1362 commit 5557551
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
`
}

Expand Down
18 changes: 18 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down

0 comments on commit 5557551

Please sign in to comment.