support output-dir when running 'helm template' (#3144)

* support output-dir when running 'helm template'

* add --output-dir to documentation

* when writing to file, dont add additional document

* trigger another ci build. make test-unit works for me

* dont write blank files

* return err instead of panic
pull/3185/head
Rajat Jindal 7 years ago committed by Matt Butcher
parent 4c21a0e739
commit 7f13e13a71

@ -21,6 +21,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"path"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
@ -39,6 +40,10 @@ import (
tversion "k8s.io/helm/pkg/version" tversion "k8s.io/helm/pkg/version"
) )
const defaultDirectoryPermission = 0755
var whitespaceRegex = regexp.MustCompile(`^\s*$`)
const templateDesc = ` const templateDesc = `
Render chart templates locally and display the output. Render chart templates locally and display the output.
@ -64,6 +69,7 @@ type templateCmd struct {
releaseName string releaseName string
renderFiles []string renderFiles []string
kubeVersion string kubeVersion string
outputDir string
} }
func newTemplateCmd(out io.Writer) *cobra.Command { func newTemplateCmd(out io.Writer) *cobra.Command {
@ -88,6 +94,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.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.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.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 return cmd
} }
@ -126,6 +133,14 @@ func (t *templateCmd) run(cmd *cobra.Command, args []string) error {
} }
} }
// verify that output-dir exists if provided
if t.outputDir != "" {
_, err = os.Stat(t.outputDir)
if os.IsNotExist(err) {
return fmt.Errorf("output-dir '%s' does not exist", t.outputDir)
}
}
if t.namespace == "" { if t.namespace == "" {
t.namespace = defaultNamespace() t.namespace = defaultNamespace()
} }
@ -248,8 +263,61 @@ func (t *templateCmd) run(cmd *cobra.Command, args []string) error {
if strings.HasPrefix(b, "_") { if strings.HasPrefix(b, "_") {
continue continue
} }
if t.outputDir != "" {
// blank template after execution
if whitespaceRegex.MatchString(data) {
continue
}
err = writeToFile(t.outputDir, m.Name, data)
if err != nil {
return err
}
continue
}
fmt.Printf("---\n# Source: %s\n", m.Name) fmt.Printf("---\n# Source: %s\n", m.Name)
fmt.Println(data) fmt.Println(data)
} }
return nil return nil
} }
// write the <data> to <output-dir>/<name>
func writeToFile(outputDir string, name string, data string) error {
outfileName := strings.Join([]string{outputDir, name}, string(filepath.Separator))
err := ensureDirectoryForFile(outfileName)
if err != nil {
return err
}
f, err := os.Create(outfileName)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(fmt.Sprintf("##---\n# Source: %s\n%s", name, data))
if err != nil {
return err
}
fmt.Printf("wrote %s\n", outfileName)
return nil
}
// check if the directory exists to create file. creates if don't exists
func ensureDirectoryForFile(file string) error {
baseDir := path.Dir(file)
_, err := os.Stat(baseDir)
if err != nil && !os.IsNotExist(err) {
return err
}
err = os.MkdirAll(baseDir, defaultDirectoryPermission)
if err != nil {
return err
}
return nil
}

@ -31,6 +31,7 @@ helm template [flags] CHART
--name-template string specify template used to name the release --name-template string specify template used to name the release
--namespace string namespace to install the release into --namespace string namespace to install the release into
--notes show the computed NOTES.txt file as well --notes show the computed NOTES.txt file as well
--output-dir string writes the executed templates to files in output-dir instead of stdout
--set stringArray set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2) --set stringArray set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
-f, --values valueFiles specify values in a YAML file (can specify multiple) (default []) -f, --values valueFiles specify values in a YAML file (can specify multiple) (default [])
``` ```
@ -49,4 +50,4 @@ helm template [flags] CHART
### SEE ALSO ### SEE ALSO
* [helm](helm.md) - The Helm package manager for Kubernetes. * [helm](helm.md) - The Helm package manager for Kubernetes.
###### Auto generated by spf13/cobra on 7-Nov-2017 ###### Auto generated by spf13/cobra on 15-Nov-2017

Loading…
Cancel
Save