Skip to content

Commit

Permalink
feat(helm): output option for helm get values command, allow json and…
Browse files Browse the repository at this point in the history
… yaml formats (helm#4596)

Signed-off-by: Arash Deshmeh <adeshmeh@ca.ibm.com>
  • Loading branch information
adshmh authored and Matthew Fisher committed Sep 10, 2018
1 parent 5b23632 commit b4b693c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
41 changes: 33 additions & 8 deletions cmd/helm/get_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package main

import (
"encoding/json"
"fmt"
"io"

Expand All @@ -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 {
Expand All @@ -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
}

Expand All @@ -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)
}
}
33 changes: 32 additions & 1 deletion cmd/helm/get_values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion docs/helm/helm_get_values.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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

0 comments on commit b4b693c

Please sign in to comment.