From 84dcc294bfe0c5037efa1cf1e98aaf0d2727fbb7 Mon Sep 17 00:00:00 2001 From: Niklas Wik Date: Mon, 23 Apr 2018 08:12:38 +0300 Subject: 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`. --- main.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 624af0f2c8..7d0b33aa77 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) + } } } } -- cgit 1.4.1