From 3082db494ac49709f5caf2d5b1240eeecf0abe26 Mon Sep 17 00:00:00 2001 From: Mohan Prasath Date: Wed, 19 Dec 2018 14:22:23 +0530 Subject: [PATCH] feat(helm): output flag for helm inspect to provide data as json values output flag is added to `helm inspect`, so the output format type can be set to json or yaml eg: `helm inspect chart mychart/ --output=json` closes: [5070](https://github.com/helm/helm/issues/5070) Signed-off-by: Mohan Prasath --- cmd/helm/inspect.go | 63 +++++++++++++++++++++++++------- cmd/helm/inspect_test.go | 7 ++-- docs/helm/helm_inspect.md | 3 +- docs/helm/helm_inspect_chart.md | 3 +- docs/helm/helm_inspect_values.md | 3 +- 5 files changed, 60 insertions(+), 19 deletions(-) diff --git a/cmd/helm/inspect.go b/cmd/helm/inspect.go index 844116bc5..ca89ee398 100644 --- a/cmd/helm/inspect.go +++ b/cmd/helm/inspect.go @@ -17,12 +17,14 @@ limitations under the License. package main import ( + "encoding/json" "fmt" + "io" + "strings" + "github.com/ghodss/yaml" "github.com/golang/protobuf/ptypes/any" "github.com/spf13/cobra" - "io" - "strings" "k8s.io/helm/pkg/chartutil" ) @@ -50,15 +52,16 @@ of the README file ` type inspectCmd struct { - chartpath string - output string - verify bool - keyring string - out io.Writer - version string - repoURL string - username string - password string + chartpath string + output string + outputFormat string + verify bool + keyring string + out io.Writer + version string + repoURL string + username string + password string certFile string keyFile string @@ -70,6 +73,8 @@ const ( valuesOnly = "values" readmeOnly = "readme" all = "all" + yamlFormat = "yaml" + jsonFormat = "json" ) var readmeFileNames = []string{"readme.md", "readme.txt", "readme"} @@ -211,6 +216,12 @@ func newInspectCmd(out io.Writer) *cobra.Command { subCmd.Flags().StringVar(&insp.caFile, caFile, "", caFiledesc) } + outputFormat := "output" + outputFormatDesc := "output represents the output format. One of: json|yaml" + inspectCommand.Flags().StringVar(&insp.outputFormat, outputFormat, yamlFormat, outputFormatDesc) + valuesSubCmd.Flags().StringVar(&insp.outputFormat, outputFormat, yamlFormat, outputFormatDesc) + chartSubCmd.Flags().StringVar(&insp.outputFormat, outputFormat, yamlFormat, outputFormatDesc) + for _, subCmd := range cmds[1:] { inspectCommand.AddCommand(subCmd) } @@ -223,7 +234,17 @@ func (i *inspectCmd) run() error { if err != nil { return err } - cf, err := yaml.Marshal(chrt.Metadata) + + if err := i.validateFlags(); err != nil { + return err + } + + var cf []byte + if i.outputFormat == yamlFormat { + cf, err = yaml.Marshal(chrt.Metadata) + } else { + cf, err = json.Marshal(chrt.Metadata) + } if err != nil { return err } @@ -236,7 +257,16 @@ func (i *inspectCmd) run() error { if i.output == all { fmt.Fprintln(i.out, "---") } - fmt.Fprintln(i.out, chrt.Values.Raw) + + cValues := []byte(chrt.Values.Raw) + if i.outputFormat == jsonFormat { + cValues, err = yaml.YAMLToJSON(cValues) + if err != nil { + return err + } + } + + fmt.Fprintln(i.out, string(cValues)) } if i.output == readmeOnly || i.output == all { @@ -252,6 +282,13 @@ func (i *inspectCmd) run() error { return nil } +func (i *inspectCmd) validateFlags() error { + if i.outputFormat == jsonFormat || i.outputFormat == yamlFormat { + return nil + } + return fmt.Errorf("invalid output format provided =%s, expected values json,yaml", i.outputFormat) +} + func findReadme(files []*any.Any) (file *any.Any) { for _, file := range files { if containsString(readmeFileNames, strings.ToLower(file.TypeUrl), nil) { diff --git a/cmd/helm/inspect_test.go b/cmd/helm/inspect_test.go index b9dbf2ab6..eb0bb1109 100644 --- a/cmd/helm/inspect_test.go +++ b/cmd/helm/inspect_test.go @@ -27,9 +27,10 @@ func TestInspect(t *testing.T) { b := bytes.NewBuffer(nil) insp := &inspectCmd{ - chartpath: "testdata/testcharts/alpine", - output: all, - out: b, + chartpath: "testdata/testcharts/alpine", + output: all, + out: b, + outputFormat: "yaml", } insp.run() diff --git a/docs/helm/helm_inspect.md b/docs/helm/helm_inspect.md index 86689eeaa..93d36d8d6 100644 --- a/docs/helm/helm_inspect.md +++ b/docs/helm/helm_inspect.md @@ -23,6 +23,7 @@ helm inspect [CHART] [flags] -h, --help help for inspect --key-file string identify HTTPS client using this SSL key file --keyring string path to the keyring containing public verification keys (default "~/.gnupg/pubring.gpg") + --output string output represents the output format. One of: json|yaml (default "yaml") --password string chart repository password where to locate the requested chart --repo string chart repository url where to locate the requested chart --username string chart repository username where to locate the requested chart @@ -49,4 +50,4 @@ helm inspect [CHART] [flags] * [helm inspect readme](helm_inspect_readme.md) - shows inspect readme * [helm inspect values](helm_inspect_values.md) - shows inspect values -###### Auto generated by spf13/cobra on 1-Aug-2018 +###### Auto generated by spf13/cobra on 19-Dec-2018 diff --git a/docs/helm/helm_inspect_chart.md b/docs/helm/helm_inspect_chart.md index 2b9adbb7e..144196c3b 100644 --- a/docs/helm/helm_inspect_chart.md +++ b/docs/helm/helm_inspect_chart.md @@ -21,6 +21,7 @@ helm inspect chart [CHART] [flags] -h, --help help for chart --key-file string identify HTTPS client using this SSL key file --keyring string path to the keyring containing public verification keys (default "~/.gnupg/pubring.gpg") + --output string output represents the output format. One of: json|yaml (default "yaml") --password string chart repository password where to locate the requested chart --repo string chart repository url where to locate the requested chart --username string chart repository username where to locate the requested chart @@ -44,4 +45,4 @@ helm inspect chart [CHART] [flags] * [helm inspect](helm_inspect.md) - inspect a chart -###### Auto generated by spf13/cobra on 1-Aug-2018 +###### Auto generated by spf13/cobra on 19-Dec-2018 diff --git a/docs/helm/helm_inspect_values.md b/docs/helm/helm_inspect_values.md index 9cca2fc32..79c64587d 100644 --- a/docs/helm/helm_inspect_values.md +++ b/docs/helm/helm_inspect_values.md @@ -21,6 +21,7 @@ helm inspect values [CHART] [flags] -h, --help help for values --key-file string identify HTTPS client using this SSL key file --keyring string path to the keyring containing public verification keys (default "~/.gnupg/pubring.gpg") + --output string output represents the output format. One of: json|yaml (default "yaml") --password string chart repository password where to locate the requested chart --repo string chart repository url where to locate the requested chart --username string chart repository username where to locate the requested chart @@ -44,4 +45,4 @@ helm inspect values [CHART] [flags] * [helm inspect](helm_inspect.md) - inspect a chart -###### Auto generated by spf13/cobra on 1-Aug-2018 +###### Auto generated by spf13/cobra on 19-Dec-2018