Skip to content

Commit 49f57eb

Browse files
committed
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}}`.
1 parent 545782a commit 49f57eb

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ func (c *Command) VersionTemplate() string {
607607
if c.HasParent() {
608608
return c.parent.VersionTemplate()
609609
}
610-
return `{{with .Name}}{{printf "%s " .}}{{end}}{{printf "version %s" .Version}}
610+
return `{{with .DisplayName}}{{printf "%s " .}}{{end}}{{printf "version %s" .Version}}
611611
`
612612
}
613613

command_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,24 @@ func TestVersionFlagExecuted(t *testing.T) {
10941094
checkStringContains(t, output, "root version 1.0.0")
10951095
}
10961096

1097+
func TestVersionFlagExecutedDiplayName(t *testing.T) {
1098+
rootCmd := &Command{
1099+
Use: "kubectl-plugin",
1100+
Version: "1.0.0",
1101+
Annotations: map[string]string{
1102+
CommandDisplayNameAnnotation: "kubectl plugin",
1103+
},
1104+
Run: emptyRun,
1105+
}
1106+
1107+
output, err := executeCommand(rootCmd, "--version", "arg1")
1108+
if err != nil {
1109+
t.Errorf("Unexpected error: %v", err)
1110+
}
1111+
1112+
checkStringContains(t, output, "kubectl plugin version 1.0.0")
1113+
}
1114+
10971115
func TestVersionFlagExecutedWithNoName(t *testing.T) {
10981116
rootCmd := &Command{Version: "1.0.0", Run: emptyRun}
10991117

0 commit comments

Comments
 (0)