From f908decf1b12860b3e5e71bc334e605addac03c0 Mon Sep 17 00:00:00 2001 From: Rajat Jindal Date: Tue, 14 Nov 2017 23:54:34 -0800 Subject: [PATCH 1/4] support output-dir for 'helm template' cmd --- cmd/helm/template.go | 65 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/cmd/helm/template.go b/cmd/helm/template.go index 92f6ed738..dc0c3c098 100644 --- a/cmd/helm/template.go +++ b/cmd/helm/template.go @@ -21,6 +21,7 @@ import ( "fmt" "io" "os" + "path" "path/filepath" "regexp" "strings" @@ -39,6 +40,7 @@ import ( tversion "k8s.io/helm/pkg/version" ) +const defaultDirectoryPermission = 0755 const templateDesc = ` Render chart templates locally and display the output. @@ -64,6 +66,7 @@ type templateCmd struct { releaseName string renderFiles []string kubeVersion string + outputDir string } func newTemplateCmd(out io.Writer) *cobra.Command { @@ -126,6 +129,14 @@ func (t *templateCmd) run(cmd *cobra.Command, args []string) error { } } + // verify that output-dir exists if provided + if outputDir != "" { + _, err = os.Stat(outputDir) + if os.IsNotExist(err) { + panic(fmt.Sprintf("output-dir '%s' does not exist", outputDir)) + } + } + if t.namespace == "" { t.namespace = defaultNamespace() } @@ -248,8 +259,58 @@ func (t *templateCmd) run(cmd *cobra.Command, args []string) error { if strings.HasPrefix(b, "_") { continue } - fmt.Printf("---\n# Source: %s\n", m.Name) - fmt.Println(data) + writeData(m.name, data) } return nil } + +func writeData(name string, data string) { + if outputDir != "" { + writeToFile(name, data) + return + } + + writeToStdout(name, data) +} + +// write the to stdout +func writeToStdout(name string, data string) { + fmt.Printf("---\n# Source: %s\n", name) + fmt.Printf(data) +} + +// write the to / +func writeToFile(name string, data string) { + outfileName := strings.Join([]string{outputDir, name}, string(filepath.Separator)) + ensureDirectoryForFile(outfileName) + + f, err := os.Create(outfileName) + if err != nil { + panic(err) + } + + defer f.Close() + + _, err = f.WriteString(fmt.Sprintf("---\n# Source: %s\n%s", name, data)) + + if err != nil { + panic(err) + } + + fmt.Printf("wrote %s\n", outfileName) + return +} + +// check if the directory exists to create file. creates if don't exists +func ensureDirectoryForFile(file string) { + baseDir := path.Dir(file) + _, err := os.Stat(baseDir) + if !os.IsNotExist(err) { + return + } + + err = os.MkdirAll(baseDir, defaultDirectoryPermission) + if err != nil { + panic(err) + } +} From 6bc36001c5e44535de31455170fdfcd5c3d62cb8 Mon Sep 17 00:00:00 2001 From: Rajat Jindal Date: Wed, 15 Nov 2017 00:04:42 -0800 Subject: [PATCH 2/4] fix build errors --- cmd/helm/template.go | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/cmd/helm/template.go b/cmd/helm/template.go index dc0c3c098..8ac1ebf1d 100644 --- a/cmd/helm/template.go +++ b/cmd/helm/template.go @@ -91,6 +91,7 @@ func newTemplateCmd(out io.Writer) *cobra.Command { f.StringArrayVar(&t.values, "set", []string{}, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)") f.StringVar(&t.nameTemplate, "name-template", "", "specify template used to name the release") f.StringVar(&t.kubeVersion, "kube-version", "", "override the Kubernetes version used as Capabilities.KubeVersion.Major/Minor (e.g. 1.7)") + f.StringVar(&t.outputDir, "output-dir", "", "writes the executed templates to files in output-dir instead of stdout") return cmd } @@ -130,10 +131,10 @@ func (t *templateCmd) run(cmd *cobra.Command, args []string) error { } // verify that output-dir exists if provided - if outputDir != "" { - _, err = os.Stat(outputDir) + if t.outputDir != "" { + _, err = os.Stat(t.outputDir) if os.IsNotExist(err) { - panic(fmt.Sprintf("output-dir '%s' does not exist", outputDir)) + panic(fmt.Sprintf("output-dir '%s' does not exist", t.outputDir)) } } @@ -259,28 +260,19 @@ func (t *templateCmd) run(cmd *cobra.Command, args []string) error { if strings.HasPrefix(b, "_") { continue } - writeData(m.name, data) - } - return nil -} -func writeData(name string, data string) { - if outputDir != "" { - writeToFile(name, data) - return + if t.outputDir != nil { + writeToFile(t.outputDir, name, data) + continue + } + fmt.Printf("---\n# Source: %s\n", name) + fmt.Printf(data) } - - writeToStdout(name, data) -} - -// write the to stdout -func writeToStdout(name string, data string) { - fmt.Printf("---\n# Source: %s\n", name) - fmt.Printf(data) + return nil } // write the to / -func writeToFile(name string, data string) { +func writeToFile(outputDir string, name string, data string) { outfileName := strings.Join([]string{outputDir, name}, string(filepath.Separator)) ensureDirectoryForFile(outfileName) From dc1b3c92e4c32f2031c2a04474c94b43f14b1187 Mon Sep 17 00:00:00 2001 From: Rajat Jindal Date: Wed, 15 Nov 2017 00:17:05 -0800 Subject: [PATCH 3/4] fix build err --- cmd/helm/template.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/helm/template.go b/cmd/helm/template.go index 8ac1ebf1d..2f1f3e48a 100644 --- a/cmd/helm/template.go +++ b/cmd/helm/template.go @@ -261,11 +261,11 @@ func (t *templateCmd) run(cmd *cobra.Command, args []string) error { continue } - if t.outputDir != nil { - writeToFile(t.outputDir, name, data) + if t.outputDir != "" { + writeToFile(t.outputDir, m.name, data) continue } - fmt.Printf("---\n# Source: %s\n", name) + fmt.Printf("---\n# Source: %s\n", m.name) fmt.Printf(data) } return nil From 418a8badb4b88e5b8b90d15f19bb72c4dfeddda2 Mon Sep 17 00:00:00 2001 From: Rajat Jindal Date: Wed, 15 Nov 2017 00:19:33 -0800 Subject: [PATCH 4/4] use right Name --- cmd/helm/template.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/helm/template.go b/cmd/helm/template.go index 2f1f3e48a..abbd92dd1 100644 --- a/cmd/helm/template.go +++ b/cmd/helm/template.go @@ -262,10 +262,10 @@ func (t *templateCmd) run(cmd *cobra.Command, args []string) error { } if t.outputDir != "" { - writeToFile(t.outputDir, m.name, data) + writeToFile(t.outputDir, m.Name, data) continue } - fmt.Printf("---\n# Source: %s\n", m.name) + fmt.Printf("---\n# Source: %s\n", m.Name) fmt.Printf(data) } return nil