feat(helm): add templates subcmd to 'helm inspect'

When 'helm inspect templates' is run, this will print all templates file
in the output

Signed-off-by: qiminghu.hqm <qiminghu.hqm@ant-financial.com>
pull/6415/head
qiminghu.hqm 6 years ago
parent 9608be3e0d
commit 8d2ae179f4

@ -45,6 +45,10 @@ of the Charts.yaml file
readmeChartDesc = ` readmeChartDesc = `
This command inspects a chart (directory, file, or URL) and displays the contents This command inspects a chart (directory, file, or URL) and displays the contents
of the README file of the README file
`
inspectTemplatesDesc = `
This command inspects a chart (directory, file, or URL) and displays the contents
of all templates file
` `
) )
@ -66,10 +70,11 @@ type inspectCmd struct {
} }
const ( const (
chartOnly = "chart" chartOnly = "chart"
valuesOnly = "values" valuesOnly = "values"
readmeOnly = "readme" readmeOnly = "readme"
all = "all" templatesOnly = "templates"
all = "all"
) )
var readmeFileNames = []string{"readme.md", "readme.txt", "readme"} var readmeFileNames = []string{"readme.md", "readme.txt", "readme"}
@ -143,7 +148,23 @@ func newInspectCmd(out io.Writer) *cobra.Command {
}, },
} }
cmds := []*cobra.Command{inspectCommand, readmeSubCmd, valuesSubCmd, chartSubCmd} templatesSubCmd := &cobra.Command{
Use: "templates [CHART]",
Short: "shows inspect templates",
Long: inspectTemplatesDesc,
RunE: func(cmd *cobra.Command, args []string) error {
insp.output = templatesOnly
if err := checkArgsLength(len(args), "chart name"); err != nil {
return err
}
if err := insp.prepare(args[0]); err != nil {
return err
}
return insp.run()
},
}
cmds := []*cobra.Command{inspectCommand, readmeSubCmd, valuesSubCmd, chartSubCmd, templatesSubCmd}
vflag := "verify" vflag := "verify"
vdesc := "Verify the provenance data for this chart" vdesc := "Verify the provenance data for this chart"
for _, subCmd := range cmds { for _, subCmd := range cmds {
@ -259,6 +280,19 @@ func (i *inspectCmd) run() error {
} }
fmt.Fprintln(i.out, string(readme.Value)) fmt.Fprintln(i.out, string(readme.Value))
} }
if (i.output == templatesOnly || i.output == all) && chrt.Templates != nil {
for index, template := range chrt.Templates {
if index != 0 || i.output == all {
fmt.Fprintln(i.out, "---")
}
fmt.Fprintln(i.out, template.Name)
fmt.Fprintln(i.out, string(template.Data))
}
}
return nil return nil
} }
@ -284,3 +318,4 @@ func containsString(slice []string, s string, modifier func(s string) string) bo
} }
return false return false
} }

@ -18,6 +18,7 @@ package main
import ( import (
"bytes" "bytes"
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"strings" "strings"
@ -49,15 +50,22 @@ func TestInspect(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
parts := strings.SplitN(b.String(), "---", 3) templatesData, err := ioutil.ReadFile("testdata/testcharts/alpine/templates/alpine-pod.yaml")
if len(parts) != 3 { if err != nil {
t.Fatalf("Expected 2 parts, got %d", len(parts)) t.Fatal(err)
}
templatesOutput := fmt.Sprintf("templates/alpine-pod.yaml\n%s", string(templatesData))
parts := strings.SplitN(b.String(), "---", 4)
if len(parts) != 4 {
t.Fatalf("Expected 4 parts, got %d", len(parts))
} }
expect := []string{ expect := []string{
strings.Replace(strings.TrimSpace(string(cdata)), "\r", "", -1), strings.Replace(strings.TrimSpace(string(cdata)), "\r", "", -1),
strings.Replace(strings.TrimSpace(string(data)), "\r", "", -1), strings.Replace(strings.TrimSpace(string(data)), "\r", "", -1),
strings.Replace(strings.TrimSpace(string(readmeData)), "\r", "", -1), strings.Replace(strings.TrimSpace(string(readmeData)), "\r", "", -1),
strings.Replace(strings.TrimSpace(templatesOutput), "\r", "", -1),
} }
// Problem: ghodss/yaml doesn't marshal into struct order. To solve, we // Problem: ghodss/yaml doesn't marshal into struct order. To solve, we
@ -144,3 +152,4 @@ func TestInspectPreReleaseChart(t *testing.T) {
}) })
} }
} }

Loading…
Cancel
Save