From 66268d9eee1a23bb2479c54cab44370c18f329c8 Mon Sep 17 00:00:00 2001 From: Dean Coakley Date: Wed, 9 Oct 2019 10:52:00 +0100 Subject: [PATCH] fix(cmd): Add --output option to get values Signed-off-by: Dean Coakley --- cmd/helm/get_values.go | 32 ++++++++++++++++++++++++---- cmd/helm/get_values_test.go | 6 ++++++ cmd/helm/testdata/output/values.json | 1 + pkg/action/get_values.go | 21 +++++------------- 4 files changed, 40 insertions(+), 20 deletions(-) create mode 100644 cmd/helm/testdata/output/values.json diff --git a/cmd/helm/get_values.go b/cmd/helm/get_values.go index 31e1e3711..cd94f49c5 100644 --- a/cmd/helm/get_values.go +++ b/cmd/helm/get_values.go @@ -17,20 +17,26 @@ limitations under the License. package main import ( - "fmt" "io" + "github.com/gosuri/uitable" "github.com/spf13/cobra" "helm.sh/helm/v3/cmd/helm/require" "helm.sh/helm/v3/pkg/action" + "helm.sh/helm/v3/pkg/cli/output" ) var getValuesHelp = ` This command downloads a values file for a given release. ` +type valuesWriter struct { + vals map[string]interface{} +} + func newGetValuesCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { + var outfmt output.Format client := action.NewGetValues(cfg) cmd := &cobra.Command{ @@ -39,17 +45,35 @@ func newGetValuesCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { Long: getValuesHelp, Args: require.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - res, err := client.Run(args[0]) + vals, err := client.Run(args[0]) if err != nil { return err } - fmt.Fprintln(out, res) - return nil + return outfmt.Write(out, &valuesWriter{vals}) }, } f := cmd.Flags() f.IntVar(&client.Version, "revision", 0, "get the named release with revision") f.BoolVarP(&client.AllValues, "all", "a", false, "dump all (computed) values") + bindOutputFlag(cmd, &outfmt) + return cmd } + +func (v valuesWriter) WriteTable(out io.Writer) error { + table := uitable.New() + table.AddRow("USER-SUPPLIED VALUES:") + for k, v := range v.vals { + table.AddRow(k, v) + } + return output.EncodeTable(out, table) +} + +func (v valuesWriter) WriteJSON(out io.Writer) error { + return output.EncodeJSON(out, v) +} + +func (v valuesWriter) WriteYAML(out io.Writer) error { + return output.EncodeYAML(out, v) +} diff --git a/cmd/helm/get_values_test.go b/cmd/helm/get_values_test.go index ee77053e4..cf85e490f 100644 --- a/cmd/helm/get_values_test.go +++ b/cmd/helm/get_values_test.go @@ -32,7 +32,13 @@ func TestGetValuesCmd(t *testing.T) { name: "get values requires release name arg", cmd: "get values", golden: "output/get-values-args.txt", + rels: []*release.Release{release.Mock(&release.MockReleaseOptions{Name: "thomas-guide"})}, wantError: true, + }, { + name: "get values to json", + cmd: "get values thomas-guide --output json", + golden: "output/values.json", + rels: []*release.Release{release.Mock(&release.MockReleaseOptions{Name: "thomas-guide"})}, }} runTestCmd(t, tests) } diff --git a/cmd/helm/testdata/output/values.json b/cmd/helm/testdata/output/values.json new file mode 100644 index 000000000..831dfbcfb --- /dev/null +++ b/cmd/helm/testdata/output/values.json @@ -0,0 +1 @@ +{"name": "value"} diff --git a/pkg/action/get_values.go b/pkg/action/get_values.go index e5f4f4f04..54457079e 100644 --- a/pkg/action/get_values.go +++ b/pkg/action/get_values.go @@ -17,8 +17,6 @@ limitations under the License. package action import ( - "sigs.k8s.io/yaml" - "helm.sh/helm/v3/pkg/chartutil" ) @@ -40,29 +38,20 @@ func NewGetValues(cfg *Configuration) *GetValues { } // Run executes 'helm get values' against the given release. -func (g *GetValues) Run(name string) (string, error) { +func (g *GetValues) Run(name string) (map[string]interface{}, error) { res, err := g.cfg.releaseContent(name, g.Version) if err != nil { - return "", err + return nil, err } // If the user wants all values, compute the values and return. if g.AllValues { cfg, err := chartutil.CoalesceValues(res.Chart, res.Config) if err != nil { - return "", err - } - cfgStr, err := cfg.YAML() - if err != nil { - return "", err + return nil, err } - return cfgStr, nil - } - - resConfig, err := yaml.Marshal(res.Config) - if err != nil { - return "", err + return cfg, nil } - return string(resConfig), nil + return res.Chart.Values, nil }