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 <openmohan@gmail.com>
pull/5075/head
Mohan Prasath 7 years ago
parent e7bd4da683
commit 3082db494a

@ -17,12 +17,14 @@ limitations under the License.
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"io"
"strings"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
"github.com/golang/protobuf/ptypes/any" "github.com/golang/protobuf/ptypes/any"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"io"
"strings"
"k8s.io/helm/pkg/chartutil" "k8s.io/helm/pkg/chartutil"
) )
@ -50,15 +52,16 @@ of the README file
` `
type inspectCmd struct { type inspectCmd struct {
chartpath string chartpath string
output string output string
verify bool outputFormat string
keyring string verify bool
out io.Writer keyring string
version string out io.Writer
repoURL string version string
username string repoURL string
password string username string
password string
certFile string certFile string
keyFile string keyFile string
@ -70,6 +73,8 @@ const (
valuesOnly = "values" valuesOnly = "values"
readmeOnly = "readme" readmeOnly = "readme"
all = "all" all = "all"
yamlFormat = "yaml"
jsonFormat = "json"
) )
var readmeFileNames = []string{"readme.md", "readme.txt", "readme"} 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) 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:] { for _, subCmd := range cmds[1:] {
inspectCommand.AddCommand(subCmd) inspectCommand.AddCommand(subCmd)
} }
@ -223,7 +234,17 @@ func (i *inspectCmd) run() error {
if err != nil { if err != nil {
return err 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 { if err != nil {
return err return err
} }
@ -236,7 +257,16 @@ func (i *inspectCmd) run() error {
if i.output == all { if i.output == all {
fmt.Fprintln(i.out, "---") 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 { if i.output == readmeOnly || i.output == all {
@ -252,6 +282,13 @@ func (i *inspectCmd) run() error {
return nil 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) { func findReadme(files []*any.Any) (file *any.Any) {
for _, file := range files { for _, file := range files {
if containsString(readmeFileNames, strings.ToLower(file.TypeUrl), nil) { if containsString(readmeFileNames, strings.ToLower(file.TypeUrl), nil) {

@ -27,9 +27,10 @@ func TestInspect(t *testing.T) {
b := bytes.NewBuffer(nil) b := bytes.NewBuffer(nil)
insp := &inspectCmd{ insp := &inspectCmd{
chartpath: "testdata/testcharts/alpine", chartpath: "testdata/testcharts/alpine",
output: all, output: all,
out: b, out: b,
outputFormat: "yaml",
} }
insp.run() insp.run()

@ -23,6 +23,7 @@ helm inspect [CHART] [flags]
-h, --help help for inspect -h, --help help for inspect
--key-file string identify HTTPS client using this SSL key file --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") --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 --password string chart repository password where to locate the requested chart
--repo string chart repository url 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 --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 readme](helm_inspect_readme.md) - shows inspect readme
* [helm inspect values](helm_inspect_values.md) - shows inspect values * [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

@ -21,6 +21,7 @@ helm inspect chart [CHART] [flags]
-h, --help help for chart -h, --help help for chart
--key-file string identify HTTPS client using this SSL key file --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") --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 --password string chart repository password where to locate the requested chart
--repo string chart repository url 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 --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 * [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

@ -21,6 +21,7 @@ helm inspect values [CHART] [flags]
-h, --help help for values -h, --help help for values
--key-file string identify HTTPS client using this SSL key file --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") --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 --password string chart repository password where to locate the requested chart
--repo string chart repository url 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 --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 * [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

Loading…
Cancel
Save