diff --git a/cmd/helm/get_values.go b/cmd/helm/get_values.go index a4f4fc283..38eb5f4bb 100644 --- a/cmd/helm/get_values.go +++ b/cmd/helm/get_values.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "encoding/json" "fmt" "io" @@ -36,6 +37,7 @@ type getValuesCmd struct { out io.Writer client helm.Interface version int32 + output string } func newGetValuesCmd(client helm.Interface, out io.Writer) *cobra.Command { @@ -62,6 +64,7 @@ func newGetValuesCmd(client helm.Interface, out io.Writer) *cobra.Command { settings.AddFlagsTLS(f) f.Int32Var(&get.version, "revision", 0, "get the named release with revision") f.BoolVarP(&get.allValues, "all", "a", false, "dump all (computed) values") + f.StringVar(&get.output, "output", "yaml", "output the specified format (json or yaml)") return cmd } @@ -72,20 +75,42 @@ func (g *getValuesCmd) run() error { return prettyError(err) } + values, err := chartutil.ReadValues([]byte(res.Release.Config.Raw)) + if err != nil { + return err + } + // If the user wants all values, compute the values and return. if g.allValues { - cfg, err := chartutil.CoalesceValues(res.Release.Chart, res.Release.Config) - if err != nil { - return err - } - cfgStr, err := cfg.YAML() + values, err = chartutil.CoalesceValues(res.Release.Chart, res.Release.Config) if err != nil { return err } - fmt.Fprintln(g.out, cfgStr) - return nil } - fmt.Fprintln(g.out, res.Release.Config.Raw) + result, err := formatValues(g.output, values) + if err != nil { + return err + } + fmt.Fprintln(g.out, result) return nil } + +func formatValues(format string, values chartutil.Values) (string, error) { + switch format { + case "", "yaml": + out, err := values.YAML() + if err != nil { + return "", err + } + return out, nil + case "json": + out, err := json.Marshal(values) + if err != nil { + return "", fmt.Errorf("Failed to Marshal JSON output: %s", err) + } + return string(out), nil + default: + return "", fmt.Errorf("Unknown output format %q", format) + } +} diff --git a/cmd/helm/get_values_test.go b/cmd/helm/get_values_test.go index 35c84f2ec..aec5ce0c2 100644 --- a/cmd/helm/get_values_test.go +++ b/cmd/helm/get_values_test.go @@ -23,22 +23,53 @@ import ( "github.com/spf13/cobra" "k8s.io/helm/pkg/helm" + "k8s.io/helm/pkg/proto/hapi/chart" "k8s.io/helm/pkg/proto/hapi/release" ) func TestGetValuesCmd(t *testing.T) { + releaseWithValues := helm.ReleaseMock(&helm.MockReleaseOptions{ + Name: "thomas-guide", + Chart: &chart.Chart{Values: &chart.Config{Raw: `foo2: "bar2"`}}, + Config: &chart.Config{Raw: `foo: "bar"`}, + }) + tests := []releaseCase{ { name: "get values with a release", resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "thomas-guide"}), args: []string{"thomas-guide"}, - expected: "name: \"value\"", + expected: "name: value", rels: []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "thomas-guide"})}, }, + { + name: "get values with json format", + resp: releaseWithValues, + args: []string{"thomas-guide"}, + flags: []string{"--output", "json"}, + expected: "{\"foo\":\"bar\"}", + rels: []*release.Release{releaseWithValues}, + }, + { + name: "get all values with json format", + resp: releaseWithValues, + args: []string{"thomas-guide"}, + flags: []string{"--all", "--output", "json"}, + expected: "{\"foo\":\"bar\",\"foo2\":\"bar2\"}", + rels: []*release.Release{releaseWithValues}, + }, { name: "get values requires release name arg", err: true, }, + { + name: "get values with invalid output format", + resp: releaseWithValues, + args: []string{"thomas-guide"}, + flags: []string{"--output", "INVALID_FORMAT"}, + rels: []*release.Release{releaseWithValues}, + err: true, + }, } cmd := func(c *helm.FakeClient, out io.Writer) *cobra.Command { return newGetValuesCmd(c, out) diff --git a/docs/helm/helm_get_values.md b/docs/helm/helm_get_values.md index 7208d5885..87d21b954 100644 --- a/docs/helm/helm_get_values.md +++ b/docs/helm/helm_get_values.md @@ -17,6 +17,7 @@ helm get values [flags] RELEASE_NAME ``` -a, --all dump all (computed) values -h, --help help for values + --output string output the specified format (json or yaml) (default "yaml") --revision int32 get the named release with revision --tls enable TLS for request --tls-ca-cert string path to TLS CA certificate file (default "$HELM_HOME/ca.pem") @@ -42,4 +43,4 @@ helm get values [flags] RELEASE_NAME * [helm get](helm_get.md) - download a named release -###### Auto generated by spf13/cobra on 10-Aug-2018 +###### Auto generated by spf13/cobra on 7-Sep-2018