feat(helm): output option for helm get values command, allow json and yaml formats (#4596)

Signed-off-by: Arash Deshmeh <adeshmeh@ca.ibm.com>
pull/4626/head
adshmh 6 years ago committed by Matthew Fisher
parent 5b23632446
commit b4b693c316

@ -17,6 +17,7 @@ limitations under the License.
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"io" "io"
@ -36,6 +37,7 @@ type getValuesCmd struct {
out io.Writer out io.Writer
client helm.Interface client helm.Interface
version int32 version int32
output string
} }
func newGetValuesCmd(client helm.Interface, out io.Writer) *cobra.Command { 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) settings.AddFlagsTLS(f)
f.Int32Var(&get.version, "revision", 0, "get the named release with revision") f.Int32Var(&get.version, "revision", 0, "get the named release with revision")
f.BoolVarP(&get.allValues, "all", "a", false, "dump all (computed) values") 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 return cmd
} }
@ -72,20 +75,42 @@ func (g *getValuesCmd) run() error {
return prettyError(err) 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 the user wants all values, compute the values and return.
if g.allValues { if g.allValues {
cfg, err := chartutil.CoalesceValues(res.Release.Chart, res.Release.Config) values, err = chartutil.CoalesceValues(res.Release.Chart, res.Release.Config)
if err != nil { if err != nil {
return err return err
} }
cfgStr, err := cfg.YAML() }
result, err := formatValues(g.output, values)
if err != nil { if err != nil {
return err return err
} }
fmt.Fprintln(g.out, cfgStr) fmt.Fprintln(g.out, result)
return nil return nil
} }
fmt.Fprintln(g.out, res.Release.Config.Raw) func formatValues(format string, values chartutil.Values) (string, error) {
return nil 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)
}
} }

@ -23,22 +23,53 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/helm/pkg/helm" "k8s.io/helm/pkg/helm"
"k8s.io/helm/pkg/proto/hapi/chart"
"k8s.io/helm/pkg/proto/hapi/release" "k8s.io/helm/pkg/proto/hapi/release"
) )
func TestGetValuesCmd(t *testing.T) { 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{ tests := []releaseCase{
{ {
name: "get values with a release", name: "get values with a release",
resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "thomas-guide"}), resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "thomas-guide"}),
args: []string{"thomas-guide"}, args: []string{"thomas-guide"},
expected: "name: \"value\"", expected: "name: value",
rels: []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "thomas-guide"})}, 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", name: "get values requires release name arg",
err: true, 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 { cmd := func(c *helm.FakeClient, out io.Writer) *cobra.Command {
return newGetValuesCmd(c, out) return newGetValuesCmd(c, out)

@ -17,6 +17,7 @@ helm get values [flags] RELEASE_NAME
``` ```
-a, --all dump all (computed) values -a, --all dump all (computed) values
-h, --help help for 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 --revision int32 get the named release with revision
--tls enable TLS for request --tls enable TLS for request
--tls-ca-cert string path to TLS CA certificate file (default "$HELM_HOME/ca.pem") --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 * [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

Loading…
Cancel
Save