feat(show): add helm show schema cli command

Signed-off-by: Andrea Serrecchia <and.serrecchia@gmail.com>
pull/30978/head
Andrea Serrecchia 4 months ago
parent 0a8194c9a3
commit 0ea2e9a910

@ -40,6 +40,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
@ -137,6 +139,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
}

@ -22,6 +22,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)
@ -96,6 +113,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)

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

@ -150,6 +150,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)
}

Loading…
Cancel
Save