about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNiklas Wik <niklas.wik@nokia.com>2018-04-23T05·12+0300
committerVincent Ambo <github@tazj.in>2018-05-08T09·23+0200
commit84dcc294bfe0c5037efa1cf1e98aaf0d2727fbb7 (patch)
tree475c58a59e715bd35c126c4e34be3fddd9b5ebb8
parent3ea3bed7acda9b365c45d268342fb52e236f95ed (diff)
feat(main): Support output directories in template function.
This introduces a new command line flag `--output` (or `-o` for short)
which makes it possible to template all specified resource sets into a
folder (instead of to stdout) when using `kontemplate template`.
-rw-r--r--main.go13
1 files changed, 10 insertions, 3 deletions
diff --git a/main.go b/main.go
index 624af0f2c87c..7d0b33aa772e 100644
--- a/main.go
+++ b/main.go
@@ -38,8 +38,9 @@ var (
 	excludes = app.Flag("exclude", "Resource sets to exclude explicitly").Short('e').Strings()
 
 	// Commands
-	template     = app.Command("template", "Template resource sets and print them")
-	templateFile = template.Arg("file", "Cluster configuration file to use").Required().String()
+	template          = app.Command("template", "Template resource sets and print them")
+	templateFile      = template.Arg("file", "Cluster configuration file to use").Required().String()
+	templateOutputDir = template.Flag("output", "Output directory in which to save templated files instead of printing them").Short('o').String()
 
 	apply       = app.Command("apply", "Template resources and pass to 'kubectl apply'")
 	applyFile   = apply.Arg("file", "Cluster configuration file to use").Required().String()
@@ -100,7 +101,13 @@ func templateCommand() {
 
 		for _, r := range rs.Resources {
 			fmt.Fprintf(os.Stderr, "Rendered file %s/%s:\n", rs.Name, r.Filename)
-			fmt.Println(r.Rendered)
+			if *templateOutputDir != "" {
+				os.MkdirAll(*templateOutputDir, 0777)
+				f, _ := os.Create(*templateOutputDir + "/" + r.Filename)
+				fmt.Fprintf(f, r.Rendered)
+			} else {
+				fmt.Println(r.Rendered)
+			}
 		}
 	}
 }