From 63a89a62f0246730ed3a3aa459418843f70e9407 Mon Sep 17 00:00:00 2001 From: vitorfloriano <107767584+vitorfloriano@users.noreply.github.com> Date: Tue, 23 Dec 2025 15:40:16 -0300 Subject: [PATCH] feat(helm): Add --output flag to version subcommand The version subcommand now includes the --output flag for formatting the version representation. This commit adds the 'go' and 'human' arguments for printing the raw go struct and a human-friendly table format, respectively. Signed-off-by: vitorfloriano <107767584+vitorfloriano@users.noreply.github.com> --- pkg/cmd/testdata/output/version-output-go.txt | 1 + .../testdata/output/version-output-human.txt | 5 +++ .../output/version-output-invalid.txt | 1 + pkg/cmd/version.go | 35 ++++++++++++++++++- pkg/cmd/version_test.go | 13 +++++++ 5 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 pkg/cmd/testdata/output/version-output-go.txt create mode 100644 pkg/cmd/testdata/output/version-output-human.txt create mode 100644 pkg/cmd/testdata/output/version-output-invalid.txt diff --git a/pkg/cmd/testdata/output/version-output-go.txt b/pkg/cmd/testdata/output/version-output-go.txt new file mode 100644 index 000000000..1f4cf4d4a --- /dev/null +++ b/pkg/cmd/testdata/output/version-output-go.txt @@ -0,0 +1 @@ +version.BuildInfo{Version:"v4.1", GitCommit:"", GitTreeState:"", GoVersion:"", KubeClientVersion:"v1.20"} diff --git a/pkg/cmd/testdata/output/version-output-human.txt b/pkg/cmd/testdata/output/version-output-human.txt new file mode 100644 index 000000000..7ce1fd20d --- /dev/null +++ b/pkg/cmd/testdata/output/version-output-human.txt @@ -0,0 +1,5 @@ +Version: v4.1 +Git Commit: +Git Tree State: +Go Version: +KubeClient Version: v1.20 diff --git a/pkg/cmd/testdata/output/version-output-invalid.txt b/pkg/cmd/testdata/output/version-output-invalid.txt new file mode 100644 index 000000000..83e5117ef --- /dev/null +++ b/pkg/cmd/testdata/output/version-output-invalid.txt @@ -0,0 +1 @@ +Error: invalid output format: "invalid" (valid formats: "go", "human") diff --git a/pkg/cmd/version.go b/pkg/cmd/version.go index 80fb0d712..8829f74dd 100644 --- a/pkg/cmd/version.go +++ b/pkg/cmd/version.go @@ -19,6 +19,7 @@ package cmd import ( "fmt" "io" + "text/tabwriter" "text/template" "github.com/spf13/cobra" @@ -41,6 +42,11 @@ version.BuildInfo{Version:"v3.2.1", GitCommit:"fe51cd1e31e6a202cba7dead9552a6d41 built, and "dirty" if the binary was built from locally modified code. - GoVersion is the version of Go that was used to compile Helm. +The --output flag allows you to change the representation with the following options: + +- "go": Prints the version in a raw Go struct. +- "human": Prints the version in a human-readable format. + When using the --template flag the following properties are available to use in the template: @@ -55,6 +61,7 @@ For example, --template='Version: {{.Version}}' outputs 'Version: v3.2.1'. type versionOptions struct { short bool template string + output string } func newVersionCmd(out io.Writer) *cobra.Command { @@ -73,6 +80,7 @@ func newVersionCmd(out io.Writer) *cobra.Command { f := cmd.Flags() f.BoolVar(&o.short, "short", false, "print the version number") f.StringVar(&o.template, "template", "", "template for version string format") + f.StringVarP(&o.output, "output", "o", "", "output format (options: \"go\", \"human\")") return cmd } @@ -85,7 +93,18 @@ func (o *versionOptions) run(out io.Writer) error { } return tt.Execute(out, version.Get()) } - fmt.Fprintln(out, formatVersion(o.short)) + + switch o.output { + case "human": + return printVersionHuman(out) + case "go": + fmt.Fprintln(out, formatVersion(false)) + case "": + fmt.Fprintln(out, formatVersion(o.short)) + return nil + default: + return fmt.Errorf("invalid output format: %q (valid formats: \"go\", \"human\")", o.output) + } return nil } @@ -99,3 +118,17 @@ func formatVersion(short bool) string { } return fmt.Sprintf("%#v", v) } + +func printVersionHuman(out io.Writer) error { + v := version.Get() + + w := tabwriter.NewWriter(out, 0, 0, 2, ' ', 0) + + fmt.Fprintf(w, "Version:\t%s\n", v.Version) + fmt.Fprintf(w, "Git Commit:\t%s\n", v.GitCommit) + fmt.Fprintf(w, "Git Tree State:\t%s\n", v.GitTreeState) + fmt.Fprintf(w, "Go Version:\t%s\n", v.GoVersion) + fmt.Fprintf(w, "KubeClient Version:\t%s\n", v.KubeClientVersion) + + return w.Flush() +} diff --git a/pkg/cmd/version_test.go b/pkg/cmd/version_test.go index 9551de767..ea6ce6584 100644 --- a/pkg/cmd/version_test.go +++ b/pkg/cmd/version_test.go @@ -32,6 +32,19 @@ func TestVersion(t *testing.T) { name: "template", cmd: "version --template='Version: {{.Version}}'", golden: "output/version-template.txt", + }, { + name: "output go", + cmd: "version -o go", + golden: "output/version-output-go.txt", + }, { + name: "output human", + cmd: "version -o human", + golden: "output/version-output-human.txt", + }, { + name: "invalid output format", + cmd: "version -o invalid", + wantError: true, + golden: "output/version-output-invalid.txt", }} runTestCmd(t, tests) }