feature(show): support jsonpath output for `helm show value`

Signed-off-by: Dong Gang <dong.gang@daocloud.io>
pull/8438/head
Dong Gang 4 years ago
parent 7dd42d7c06
commit 57bd8a71b0

@ -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", "", "use jsonpath output format")
}
addChartPathOptionsFlags(f, &client.ChartPathOptions)
err := subCmd.RegisterFlagCompletionFunc("version", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {

@ -20,11 +20,12 @@ import (
"fmt"
"strings"
"helm.sh/helm/v3/pkg/chartutil"
"k8s.io/cli-runtime/pkg/printers"
"sigs.k8s.io/yaml"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
)
// ShowOutputFormat is the format of the output of `helm show`
@ -52,9 +53,10 @@ func (o ShowOutputFormat) String() string {
// It provides the implementation of 'helm show' and its respective subcommands.
type Show struct {
ChartPathOptions
Devel bool
OutputFormat ShowOutputFormat
chart *chart.Chart // for testing
Devel bool
OutputFormat ShowOutputFormat
JsonPathTemplate string
chart *chart.Chart // for testing
}
// NewShow creates a new Show object with the given configuration.
@ -87,9 +89,17 @@ func (s *Show) Run(chartpath string) (string, error) {
if s.OutputFormat == ShowAll {
fmt.Fprintln(&out, "---")
}
for _, f := range s.chart.Raw {
if f.Name == chartutil.ValuesfileName {
fmt.Fprintln(&out, string(f.Data))
if s.JsonPathTemplate != "" {
printer, err := printers.NewJSONPathPrinter(s.JsonPathTemplate)
if err != nil {
return "", fmt.Errorf("error parsing jsonpath %s, %v\n", s.JsonPathTemplate, err)
}
printer.Execute(&out, s.chart.Values)
} else {
for _, f := range s.chart.Raw {
if f.Name == chartutil.ValuesfileName {
fmt.Fprintln(&out, string(f.Data))
}
}
}
}

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