Updated get_values to support json output

pull/3226/head
Withnale 8 years ago
parent 16571305bb
commit fe82f15994

@ -17,7 +17,9 @@ limitations under the License.
package main
import (
"encoding/json"
"fmt"
"github.com/ghodss/yaml"
"io"
"github.com/spf13/cobra"
@ -33,6 +35,7 @@ This command downloads a values file for a given release.
type getValuesCmd struct {
release string
allValues bool
outfmt string
out io.Writer
client helm.Interface
version int32
@ -60,16 +63,37 @@ func newGetValuesCmd(client helm.Interface, out io.Writer) *cobra.Command {
cmd.Flags().Int32Var(&get.version, "revision", 0, "get the named release with revision")
cmd.Flags().BoolVarP(&get.allValues, "all", "a", false, "dump all (computed) values")
cmd.Flags().StringVarP(&get.outfmt, "output", "o", "yaml", "output the status in the specified format (json or yaml)")
return cmd
}
func (g *getValuesCmd) printValues(vals string) error {
switch g.outfmt {
case "json":
out := map[string]interface{}{}
err := yaml.Unmarshal([]byte(vals), &out)
if err != nil {
return err
}
json, err := json.MarshalIndent(out, "", " ")
if err != nil {
return err
}
fmt.Fprintln(g.out, string(json))
default:
fmt.Fprintln(g.out, vals)
}
return nil
}
// getValues implements 'helm get values'
func (g *getValuesCmd) run() error {
res, err := g.client.ReleaseContent(g.release, helm.ContentReleaseVersion(g.version))
if err != nil {
return prettyError(err)
}
cfg := res.Release.Config.Raw
// If the user wants all values, compute the values and return.
if g.allValues {
cfg, err := chartutil.CoalesceValues(res.Release.Chart, res.Release.Config)
@ -80,10 +104,9 @@ func (g *getValuesCmd) run() error {
if err != nil {
return err
}
fmt.Fprintln(g.out, cfgStr)
g.printValues(cfgStr)
return nil
}
fmt.Fprintln(g.out, res.Release.Config.Raw)
g.printValues(cfg)
return nil
}

@ -45,3 +45,24 @@ func TestGetValuesCmd(t *testing.T) {
}
runReleaseCases(t, tests, cmd)
}
func TestGetValuesCmdJson(t *testing.T) {
tests := []releaseCase{
{
name: "get values with a release in JSON",
resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "thomas-guide"}),
flags: []string{"--output", "json"},
args: []string{"thomas-guide"},
expected: "{\n \"name\": \"value\"\n}\n",
rels: []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "thomas-guide"})},
},
{
name: "get values requires release name arg",
err: true,
},
}
cmd := func(c *helm.FakeClient, out io.Writer) *cobra.Command {
return newGetValuesCmd(c, out)
}
runReleaseCases(t, tests, cmd)
}

@ -17,6 +17,7 @@ helm get values [flags] RELEASE_NAME
```
-a, --all dump all (computed) values
-o, --output string output the status in 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")

Loading…
Cancel
Save