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>
pull/31574/head
vitorfloriano 1 week ago
parent f05c21b6b6
commit 63a89a62f0

@ -0,0 +1 @@
version.BuildInfo{Version:"v4.1", GitCommit:"", GitTreeState:"", GoVersion:"", KubeClientVersion:"v1.20"}

@ -0,0 +1,5 @@
Version: v4.1
Git Commit:
Git Tree State:
Go Version:
KubeClient Version: v1.20

@ -0,0 +1 @@
Error: invalid output format: "invalid" (valid formats: "go", "human")

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

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

Loading…
Cancel
Save