From 57bd8a71b0002b00a13b6a4bfa17e45839b73795 Mon Sep 17 00:00:00 2001 From: Dong Gang Date: Mon, 13 Jul 2020 18:59:09 +0800 Subject: [PATCH 1/4] feature(show): support jsonpath output for `helm show value` Signed-off-by: Dong Gang --- cmd/helm/show.go | 3 +++ pkg/action/show.go | 24 +++++++++++++++++------- pkg/action/show_test.go | 14 ++++++++++++++ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/cmd/helm/show.go b/cmd/helm/show.go index c122d8476..b374a3594 100644 --- a/cmd/helm/show.go +++ b/cmd/helm/show.go @@ -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) { diff --git a/pkg/action/show.go b/pkg/action/show.go index 9baa9cf43..a5544492b 100644 --- a/pkg/action/show.go +++ b/pkg/action/show.go @@ -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)) + } } } } diff --git a/pkg/action/show_test.go b/pkg/action/show_test.go index 7be9daaa5..0251a528d 100644 --- a/pkg/action/show_test.go +++ b/pkg/action/show_test.go @@ -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) + } +} From 1dfe66aa85bf09cd1f09271c2810c5deb9f22454 Mon Sep 17 00:00:00 2001 From: Dong Gang Date: Mon, 13 Jul 2020 19:11:40 +0800 Subject: [PATCH 2/4] fix the code style error Signed-off-by: Dong Gang --- cmd/helm/show.go | 2 +- pkg/action/show.go | 10 +++++----- pkg/action/show_test.go | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cmd/helm/show.go b/cmd/helm/show.go index b374a3594..60ed0ab8e 100644 --- a/cmd/helm/show.go +++ b/cmd/helm/show.go @@ -152,7 +152,7 @@ func addShowFlags(subCmd *cobra.Command, client *action.Show) { 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") + f.StringVar(&client.JSONPathTemplate, "jsonpath", "", "use jsonpath output format") } addChartPathOptionsFlags(f, &client.ChartPathOptions) diff --git a/pkg/action/show.go b/pkg/action/show.go index a5544492b..73d989884 100644 --- a/pkg/action/show.go +++ b/pkg/action/show.go @@ -20,12 +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` @@ -55,7 +55,7 @@ type Show struct { ChartPathOptions Devel bool OutputFormat ShowOutputFormat - JsonPathTemplate string + JSONPathTemplate string chart *chart.Chart // for testing } @@ -89,10 +89,10 @@ 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 s.JSONPathTemplate != "" { + printer, err := printers.NewJSONPathPrinter(s.JSONPathTemplate) if err != nil { - return "", fmt.Errorf("error parsing jsonpath %s, %v\n", s.JsonPathTemplate, err) + return "", fmt.Errorf("error parsing jsonpath %s, %v", s.JSONPathTemplate, err) } printer.Execute(&out, s.chart.Values) } else { diff --git a/pkg/action/show_test.go b/pkg/action/show_test.go index 0251a528d..a2efdc8ac 100644 --- a/pkg/action/show_test.go +++ b/pkg/action/show_test.go @@ -72,7 +72,7 @@ func TestShowNoValues(t *testing.T) { func TestShowValuesByJsonPathFormat(t *testing.T) { client := NewShow(ShowValues) - client.JsonPathTemplate = "{$.nestedKey.simpleKey}" + client.JSONPathTemplate = "{$.nestedKey.simpleKey}" client.chart = buildChart(withSampleValues()) output, err := client.Run("") if err != nil { From 3ee7047827b0fc5b87fb2d8bde6bb64fa93fcf73 Mon Sep 17 00:00:00 2001 From: Dong Gang Date: Wed, 15 Jul 2020 11:09:01 +0800 Subject: [PATCH 3/4] polish the help text of flag Signed-off-by: Dong Gang --- cmd/helm/show.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/helm/show.go b/cmd/helm/show.go index 60ed0ab8e..fe5eaee26 100644 --- a/cmd/helm/show.go +++ b/cmd/helm/show.go @@ -152,7 +152,7 @@ func addShowFlags(subCmd *cobra.Command, client *action.Show) { 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") + f.StringVar(&client.JSONPathTemplate, "jsonpath", "", "supply a JSONPath expression to filter the output") } addChartPathOptionsFlags(f, &client.ChartPathOptions) From df4708a9de8a6f4c89dca7555a5b338c8298128d Mon Sep 17 00:00:00 2001 From: Dong Gang Date: Wed, 5 Aug 2020 10:37:00 +0800 Subject: [PATCH 4/4] polish the error handler Signed-off-by: Dong Gang --- pkg/action/show.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/action/show.go b/pkg/action/show.go index 73d989884..4eab2b4fd 100644 --- a/pkg/action/show.go +++ b/pkg/action/show.go @@ -20,6 +20,7 @@ import ( "fmt" "strings" + "github.com/pkg/errors" "k8s.io/cli-runtime/pkg/printers" "sigs.k8s.io/yaml" @@ -92,7 +93,7 @@ func (s *Show) Run(chartpath string) (string, error) { if s.JSONPathTemplate != "" { printer, err := printers.NewJSONPathPrinter(s.JSONPathTemplate) if err != nil { - return "", fmt.Errorf("error parsing jsonpath %s, %v", s.JSONPathTemplate, err) + return "", errors.Wrapf(err, "error parsing jsonpath %s", s.JSONPathTemplate) } printer.Execute(&out, s.chart.Values) } else {