From 8d2ae179f4ecf68719d89b78be50e97b522a41f2 Mon Sep 17 00:00:00 2001 From: "qiminghu.hqm" Date: Thu, 12 Sep 2019 00:40:35 +0800 Subject: [PATCH] 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 --- cmd/helm/inspect.go | 45 +++++++++++++++++++++++++++++++++++----- cmd/helm/inspect_test.go | 15 +++++++++++--- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/cmd/helm/inspect.go b/cmd/helm/inspect.go index d8ac6b2ef..c7e651852 100644 --- a/cmd/helm/inspect.go +++ b/cmd/helm/inspect.go @@ -45,6 +45,10 @@ of the Charts.yaml file readmeChartDesc = ` This command inspects a chart (directory, file, or URL) and displays the contents 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 ( - chartOnly = "chart" - valuesOnly = "values" - readmeOnly = "readme" - all = "all" + chartOnly = "chart" + valuesOnly = "values" + readmeOnly = "readme" + templatesOnly = "templates" + all = "all" ) 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" vdesc := "Verify the provenance data for this chart" for _, subCmd := range cmds { @@ -259,6 +280,19 @@ func (i *inspectCmd) run() error { } 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 } @@ -284,3 +318,4 @@ func containsString(slice []string, s string, modifier func(s string) string) bo } return false } + diff --git a/cmd/helm/inspect_test.go b/cmd/helm/inspect_test.go index c4ce005b0..22dfe96d3 100644 --- a/cmd/helm/inspect_test.go +++ b/cmd/helm/inspect_test.go @@ -18,6 +18,7 @@ package main import ( "bytes" + "fmt" "io/ioutil" "os" "strings" @@ -49,15 +50,22 @@ func TestInspect(t *testing.T) { if err != nil { t.Fatal(err) } - parts := strings.SplitN(b.String(), "---", 3) - if len(parts) != 3 { - t.Fatalf("Expected 2 parts, got %d", len(parts)) + templatesData, err := ioutil.ReadFile("testdata/testcharts/alpine/templates/alpine-pod.yaml") + if err != nil { + 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{ strings.Replace(strings.TrimSpace(string(cdata)), "\r", "", -1), strings.Replace(strings.TrimSpace(string(data)), "\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 @@ -144,3 +152,4 @@ func TestInspectPreReleaseChart(t *testing.T) { }) } } +