Skip to content

Commit

Permalink
support output-dir when running 'helm template' (#3144)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rajatjindal authored and technosophos committed Nov 21, 2017
1 parent 4c21a0e commit 7f13e13
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
68 changes: 68 additions & 0 deletions cmd/helm/template.go
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"regexp"
"strings"
Expand All @@ -39,6 +40,10 @@ import (
tversion "k8s.io/helm/pkg/version"
)

const defaultDirectoryPermission = 0755

var whitespaceRegex = regexp.MustCompile(`^\s*$`)

const templateDesc = `
Render chart templates locally and display the output.
Expand All @@ -64,6 +69,7 @@ type templateCmd struct {
releaseName string
renderFiles []string
kubeVersion string
outputDir string
}

func newTemplateCmd(out io.Writer) *cobra.Command {
Expand All @@ -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.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
}
Expand Down Expand Up @@ -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 == "" {
t.namespace = defaultNamespace()
}
Expand Down Expand Up @@ -248,8 +263,61 @@ func (t *templateCmd) run(cmd *cobra.Command, args []string) error {
if strings.HasPrefix(b, "_") {
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.Println(data)
}
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
}
3 changes: 2 additions & 1 deletion docs/helm/helm_template.md
Expand Up @@ -31,6 +31,7 @@ helm template [flags] CHART
--name-template string specify template used to name the release
--namespace string namespace to install the release into
--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)
-f, --values valueFiles specify values in a YAML file (can specify multiple) (default [])
```
Expand All @@ -49,4 +50,4 @@ helm template [flags] CHART
### SEE ALSO
* [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

0 comments on commit 7f13e13

Please sign in to comment.