feat(helm) add '--template' option to 'helm get' for custom formatting

Signed-off-by: Jeff Knurek <j.knurek@travelaudience.com>
pull/5496/head
Jeff Knurek 6 years ago
parent 6c760297c0
commit 0a3ebb816b

@ -41,10 +41,11 @@ chart, the supplied values, and the generated manifest file.
var errReleaseRequired = errors.New("release name is required")
type getCmd struct {
release string
out io.Writer
client helm.Interface
version int32
release string
out io.Writer
client helm.Interface
version int32
template string
}
func newGetCmd(client helm.Interface, out io.Writer) *cobra.Command {
@ -73,12 +74,12 @@ func newGetCmd(client helm.Interface, out io.Writer) *cobra.Command {
f := cmd.Flags()
settings.AddFlagsTLS(f)
f.Int32Var(&get.version, "revision", 0, "get the named release with revision")
f.StringVar(&get.template, "template", "", "go template for formatting the output, eg: {{.Release.Name}}")
cmd.AddCommand(newGetValuesCmd(nil, out))
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)
@ -92,5 +93,9 @@ func (g *getCmd) run() error {
if err != nil {
return prettyError(err)
}
if g.template != "" {
return tpl(g.template, res, g.out)
}
return printRelease(g.out, res.Release)
}

@ -35,6 +35,13 @@ func TestGetCmd(t *testing.T) {
expected: "REVISION: 1\nRELEASED: (.*)\nCHART: foo-0.1.0-beta.1\nUSER-SUPPLIED VALUES:\nname: \"value\"\nCOMPUTED VALUES:\nname: value\n\nHOOKS:\n---\n# pre-install-hook\n" + helm.MockHookTemplate + "\nMANIFEST:",
rels: []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "thomas-guide"})},
},
{
name: "get with a formatted release",
resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "elevated-turkey"}),
args: []string{"elevated-turkey", "--template", "{{.Release.Chart.Metadata.Version}}"},
expected: "0.1.0-beta.1",
rels: []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "elevated-turkey"})},
},
{
name: "get requires release name arg",
err: true,

@ -1,75 +0,0 @@
/*
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
}

@ -1,47 +0,0 @@
/*
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)
})
}

@ -66,7 +66,7 @@ func printRelease(out io.Writer, rel *release.Release) error {
return tpl(printReleaseTemplate, data, out)
}
func tpl(t string, vals map[string]interface{}, out io.Writer) error {
func tpl(t string, vals interface{}, out io.Writer) error {
tt, err := template.New("_").Parse(t)
if err != nil {
return err

@ -26,6 +26,7 @@ helm get [flags] RELEASE_NAME
```
-h, --help help for get
--revision int32 get the named release with revision
--template string go template for formatting the output, eg: {{.Release.Name}}
--tls enable TLS for request
--tls-ca-cert string path to TLS CA certificate file (default "$HELM_HOME/ca.pem")
--tls-cert string path to TLS certificate file (default "$HELM_HOME/cert.pem")
@ -53,6 +54,5 @@ helm get [flags] RELEASE_NAME
* [helm get manifest](helm_get_manifest.md) - download the manifest for a named release
* [helm get notes](helm_get_notes.md) - displays the notes of the named release
* [helm get values](helm_get_values.md) - download the values file for a named release
* [helm get version](helm_get_version.md) - download the chart version for a named release
###### Auto generated by spf13/cobra on 21-Mar-2019
###### Auto generated by spf13/cobra on 25-Mar-2019

Loading…
Cancel
Save