Merge pull request #8438 from donggangcj/feature/support-jsonpath

feature(show): support jsonpath output for `helm show value`
pull/8566/head
Martin Hickey 5 years ago committed by GitHub
commit a5047b2f6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -151,6 +151,9 @@ func addShowFlags(subCmd *cobra.Command, client *action.Show) {
f := subCmd.Flags()
f.BoolVar(&client.Devel, "devel", false, "use development versions, too. Equivalent to version '>0.0.0-0'. If --version is set, this is ignored")
if subCmd.Name() == "values" {
f.StringVar(&client.JSONPathTemplate, "jsonpath", "", "supply a JSONPath expression to filter the output")
}
addChartPathOptionsFlags(f, &client.ChartPathOptions)
err := subCmd.RegisterFlagCompletionFunc("version", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {

@ -20,6 +20,8 @@ import (
"fmt"
"strings"
"github.com/pkg/errors"
"k8s.io/cli-runtime/pkg/printers"
"sigs.k8s.io/yaml"
"helm.sh/helm/v3/pkg/chart"
@ -54,6 +56,7 @@ type Show struct {
ChartPathOptions
Devel bool
OutputFormat ShowOutputFormat
JSONPathTemplate string
chart *chart.Chart // for testing
}
@ -87,12 +90,20 @@ func (s *Show) Run(chartpath string) (string, error) {
if s.OutputFormat == ShowAll {
fmt.Fprintln(&out, "---")
}
if s.JSONPathTemplate != "" {
printer, err := printers.NewJSONPathPrinter(s.JSONPathTemplate)
if err != nil {
return "", errors.Wrapf(err, "error parsing jsonpath %s", s.JSONPathTemplate)
}
printer.Execute(&out, s.chart.Values)
} else {
for _, f := range s.chart.Raw {
if f.Name == chartutil.ValuesfileName {
fmt.Fprintln(&out, string(f.Data))
}
}
}
}
if s.OutputFormat == ShowReadme || s.OutputFormat == ShowAll {
if s.OutputFormat == ShowAll {

@ -69,3 +69,17 @@ func TestShowNoValues(t *testing.T) {
t.Errorf("expected empty values buffer, got %s", output)
}
}
func TestShowValuesByJsonPathFormat(t *testing.T) {
client := NewShow(ShowValues)
client.JSONPathTemplate = "{$.nestedKey.simpleKey}"
client.chart = buildChart(withSampleValues())
output, err := client.Run("")
if err != nil {
t.Fatal(err)
}
expect := "simpleValue"
if output != expect {
t.Errorf("Expected\n%q\nGot\n%q\n", expect, output)
}
}

Loading…
Cancel
Save