diff --git a/pkg/action/show.go b/pkg/action/show.go index 4195d69a5..69a2ee59b 100644 --- a/pkg/action/show.go +++ b/pkg/action/show.go @@ -41,6 +41,8 @@ const ( ShowChart ShowOutputFormat = "chart" // ShowValues is the format which only shows the chart's values ShowValues ShowOutputFormat = "values" + // ShowSchema is the format which only shows the chart's schema values + ShowSchema ShowOutputFormat = "schema" // ShowReadme is the format which only shows the chart's README ShowReadme ShowOutputFormat = "readme" // ShowCRDs is the format which only shows the chart's CRDs @@ -138,6 +140,12 @@ func (s *Show) Run(chartpath string) (string, error) { } } } + + if s.OutputFormat == ShowSchema { + if len(s.chart.Schema) != 0 { + fmt.Fprintf(&out, "%s\n", s.chart.Schema) + } + } return out.String(), nil } diff --git a/pkg/action/show_test.go b/pkg/action/show_test.go index faf306f2a..a502d57c6 100644 --- a/pkg/action/show_test.go +++ b/pkg/action/show_test.go @@ -23,6 +23,23 @@ import ( chart "helm.sh/helm/v4/pkg/chart/v2" ) +var valuesSchema = `{ + "$schema": "https://json-schema.org/draft-07/schema#", + "properties":{ + "property1":{ + "description": "desc1", + "properties":{ + "subProp1":{"type": "string"}, + "subProp2":{ + "description": "desc2", + "type": "object" + } + }, + "type": "object" + } + } + }` + func TestShow(t *testing.T) { config := actionConfigFixture(t) client := NewShow(ShowAll, config) @@ -101,6 +118,42 @@ func TestShowValuesByJsonPathFormat(t *testing.T) { } } +func TestShowNoSchema(t *testing.T) { + config := actionConfigFixture(t) + client := NewShow(ShowSchema, config) + client.chart = new(chart.Chart) + client.OutputFormat = ShowSchema + output, err := client.Run("") + if err != nil { + t.Fatal(err) + } + + if len(output) != 0 { + t.Errorf("expected empty values buffer, got %s", output) + } +} + +func withSampleSchema() chartOption { + return func(opts *chartOptions) { + opts.Schema = []byte(valuesSchema) + } +} + +func TestShowSchema(t *testing.T) { + config := actionConfigFixture(t) + client := NewShow(ShowSchema, config) + client.chart = buildChart(withSampleSchema()) + output, err := client.Run("") + if err != nil { + t.Fatal(err) + } + // Like other `helm show` commands, output ends with a trailing newline + expect := valuesSchema + "\n" + if output != expect { + t.Errorf("Expected\n%q\nGot\n%q\n", expect, output) + } +} + func TestShowCRDs(t *testing.T) { config := actionConfigFixture(t) client := NewShow(ShowCRDs, config) diff --git a/pkg/cmd/show.go b/pkg/cmd/show.go index 1c7e7be44..071bd1e34 100644 --- a/pkg/cmd/show.go +++ b/pkg/cmd/show.go @@ -42,6 +42,11 @@ This command inspects a chart (directory, file, or URL) and displays the content of the values.yaml file ` +const showSchemaDesc = ` +This command inspects a chart (directory, file, or URL) and displays the contents +of the values.schema.json file +` + const showChartDesc = ` This command inspects a chart (directory, file, or URL) and displays the contents of the Chart.yaml file @@ -118,6 +123,27 @@ func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { }, } + schemaSubCmd := &cobra.Command{ + Use: "schema [CHART]", + Short: "show the chart's schema values", + Long: showSchemaDesc, + Args: require.ExactArgs(1), + ValidArgsFunction: validArgsFunc, + RunE: func(_ *cobra.Command, args []string) error { + client.OutputFormat = action.ShowSchema + err := addRegistryClient(client) + if err != nil { + return err + } + output, err := runShow(args, client) + if err != nil { + return err + } + fmt.Fprint(out, output) + return nil + }, + } + chartSubCmd := &cobra.Command{ Use: "chart [CHART]", Short: "show the chart's definition", @@ -181,7 +207,7 @@ func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { }, } - cmds := []*cobra.Command{all, readmeSubCmd, valuesSubCmd, chartSubCmd, crdsSubCmd} + cmds := []*cobra.Command{all, readmeSubCmd, valuesSubCmd, schemaSubCmd, chartSubCmd, crdsSubCmd} for _, subCmd := range cmds { addShowFlags(subCmd, client) showCommand.AddCommand(subCmd) diff --git a/pkg/cmd/show_test.go b/pkg/cmd/show_test.go index ff3671dbc..a22584526 100644 --- a/pkg/cmd/show_test.go +++ b/pkg/cmd/show_test.go @@ -153,6 +153,10 @@ func TestShowValuesFileCompletion(t *testing.T) { checkFileCompletion(t, "show values", true) } +func TestShowSchemaFileCompletion(t *testing.T) { + checkFileCompletion(t, "show schema", true) +} + func TestShowCRDsFileCompletion(t *testing.T) { checkFileCompletion(t, "show crds", true) }