From c51334235af03bc0348dd8164e057ffb49ff5023 Mon Sep 17 00:00:00 2001 From: Adam Reese Date: Fri, 16 Dec 2016 14:41:35 -0800 Subject: [PATCH] feat(helm): add short version output add `--short` flag to version --- cmd/helm/version.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/cmd/helm/version.go b/cmd/helm/version.go index 16dbc0e67..4a93d58cd 100644 --- a/cmd/helm/version.go +++ b/cmd/helm/version.go @@ -27,6 +27,7 @@ import ( "google.golang.org/grpc/codes" "k8s.io/helm/pkg/helm" + pb "k8s.io/helm/pkg/proto/hapi/version" "k8s.io/helm/pkg/version" ) @@ -53,6 +54,7 @@ type versionCmd struct { client helm.Interface showClient bool showServer bool + short bool } func newVersionCmd(c helm.Interface, out io.Writer) *cobra.Command { @@ -80,8 +82,9 @@ func newVersionCmd(c helm.Interface, out io.Writer) *cobra.Command { }, } f := cmd.Flags() - f.BoolVarP(&version.showClient, "client", "c", false, "if set, show the client version") - f.BoolVarP(&version.showServer, "server", "s", false, "if set, show the server version") + f.BoolVarP(&version.showClient, "client", "c", false, "client version only") + f.BoolVarP(&version.showServer, "server", "s", false, "server version only") + f.BoolVar(&version.short, "short", false, "print the version number") return cmd } @@ -90,7 +93,7 @@ func (v *versionCmd) run() error { if v.showClient { cv := version.GetVersionProto() - fmt.Fprintf(v.out, "Client: %#v\n", cv) + fmt.Fprintf(v.out, "Client: %s\n", formatVersion(cv, v.short)) } if !v.showServer { @@ -107,6 +110,13 @@ func (v *versionCmd) run() error { } return errors.New("cannot connect to Tiller") } - fmt.Fprintf(v.out, "Server: %#v\n", resp.Version) + fmt.Fprintf(v.out, "Server: %s\n", formatVersion(resp.Version, v.short)) return nil } + +func formatVersion(v *pb.Version, short bool) string { + if short { + return fmt.Sprintf("%s+g%s", v.SemVer, v.GitCommit[:7]) + } + return fmt.Sprintf("%#v", v) +}