From acb759c07911491f6860619ce3037cec03d275a7 Mon Sep 17 00:00:00 2001
From: Jeff Knurek <j.knurek@travelaudience.com>
Date: Thu, 21 Mar 2019 16:19:02 +0100
Subject: [PATCH] feat(helm) add 'get version' to get the chart version

Signed-off-by: Jeff Knurek <j.knurek@travelaudience.com>
---
 cmd/helm/get.go              |  1 +
 cmd/helm/get_version.go      | 75 ++++++++++++++++++++++++++++++++++++
 cmd/helm/get_version_test.go | 47 ++++++++++++++++++++++
 3 files changed, 123 insertions(+)
 create mode 100644 cmd/helm/get_version.go
 create mode 100644 cmd/helm/get_version_test.go

diff --git a/cmd/helm/get.go b/cmd/helm/get.go
index 20a4c042f..f9ed6fb91 100644
--- a/cmd/helm/get.go
+++ b/cmd/helm/get.go
@@ -78,6 +78,7 @@ func newGetCmd(client helm.Interface, out io.Writer) *cobra.Command {
 	cmd.AddCommand(newGetManifestCmd(nil, out))
 	cmd.AddCommand(newGetHooksCmd(nil, out))
 	cmd.AddCommand(newGetNotesCmd(nil, out))
+	cmd.AddCommand(newGetVersionCmd(nil, out))
 
 	// set defaults from environment
 	settings.InitTLS(f)
diff --git a/cmd/helm/get_version.go b/cmd/helm/get_version.go
new file mode 100644
index 000000000..c76c6245d
--- /dev/null
+++ b/cmd/helm/get_version.go
@@ -0,0 +1,75 @@
+/*
+Copyright The Helm Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package main
+
+import (
+	"fmt"
+	"io"
+
+	"github.com/spf13/cobra"
+
+	"k8s.io/helm/pkg/helm"
+)
+
+var getVersionHelp = "This command fetches the chart version for a given release."
+
+type getVersionCmd struct {
+	release  string
+	out      io.Writer
+	client   helm.Interface
+	revision int32
+}
+
+func newGetVersionCmd(client helm.Interface, out io.Writer) *cobra.Command {
+	get := &getVersionCmd{
+		out:    out,
+		client: client,
+	}
+	cmd := &cobra.Command{
+		Use:     "version [flags] RELEASE_NAME",
+		Short:   "download the chart version for a named release",
+		Long:    getVersionHelp,
+		PreRunE: func(_ *cobra.Command, _ []string) error { return setupConnection() },
+		RunE: func(cmd *cobra.Command, args []string) error {
+			if len(args) == 0 {
+				return errReleaseRequired
+			}
+			get.release = args[0]
+			get.client = ensureHelmClient(get.client)
+			return get.run()
+		},
+	}
+
+	f := cmd.Flags()
+	settings.AddFlagsTLS(f)
+	f.Int32Var(&get.revision, "revision", 0, "get the named release with revision")
+
+	// set defaults from environment
+	settings.InitTLS(f)
+
+	return cmd
+}
+
+// getVersion implements 'helm get version'
+func (g *getVersionCmd) run() error {
+	res, err := g.client.ReleaseContent(g.release, helm.ContentReleaseVersion(g.revision))
+	if err != nil {
+		return prettyError(err)
+	}
+	fmt.Fprintln(g.out, res.Release.Chart.Metadata.Version)
+	return nil
+}
diff --git a/cmd/helm/get_version_test.go b/cmd/helm/get_version_test.go
new file mode 100644
index 000000000..81c9cdfd9
--- /dev/null
+++ b/cmd/helm/get_version_test.go
@@ -0,0 +1,47 @@
+/*
+Copyright The Helm Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package main
+
+import (
+	"io"
+	"testing"
+
+	"github.com/spf13/cobra"
+
+	"k8s.io/helm/pkg/helm"
+	"k8s.io/helm/pkg/proto/hapi/release"
+)
+
+func TestGetVersion(t *testing.T) {
+	tests := []releaseCase{
+		{
+			name:     "get chart version of release",
+			args:     []string{"kodiak"},
+			expected: "0.1.0-beta.1",
+			resp:     helm.ReleaseMock(&helm.MockReleaseOptions{Name: "kodiak"}),
+			rels:     []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "kodiak"})},
+		},
+		{
+			name: "get version without args",
+			args: []string{},
+			err:  true,
+		},
+	}
+	runReleaseCases(t, tests, func(c *helm.FakeClient, out io.Writer) *cobra.Command {
+		return newGetVersionCmd(c, out)
+	})
+}
-- 
GitLab