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
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) {

@ -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()

@ -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

@ -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

@ -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

Loading…
Cancel
Save