From 49f57ebdfd8fbd0ab5fac59d2e30b94e2b6aa8ca Mon Sep 17 00:00:00 2001 From: Nir Soffer Date: Sat, 24 Aug 2024 06:23:29 +0300 Subject: [PATCH] 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}}`. --- command.go | 2 +- command_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/command.go b/command.go index f570d1e56..4cd712bcb 100644 --- a/command.go +++ b/command.go @@ -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}} ` } diff --git a/command_test.go b/command_test.go index caf8c121a..cd44992bd 100644 --- a/command_test.go +++ b/command_test.go @@ -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}