about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2018-05-08T09·15+0200
committerVincent Ambo <github@tazj.in>2018-05-08T09·23+0200
commitb8c32640196618b697b3c6ca58d416097f87dd3d (patch)
tree5dd1bc9e5b1db70ff6d39b890bd45438703dbdf0
parent84dcc294bfe0c5037efa1cf1e98aaf0d2727fbb7 (diff)
fix(main): Handle errors & logic when templating to directory
This does several changes to the new "template to directory" feature
introduced in the previous commit:

1. Errors are now "handled". In classic Go-style, it is of course all
   too easy to do absolutely nothing with errors, but we can't have
   that. I'm onto you, Renee French's husband!

2. Resource sets containing similarly named files are now templated
   correctly by prepending the resource set name to the filename.

3. In the same vein as the previous point, nested resource sets are
   now also output correctly by replacing slashes (`/`) with
   dashes (`-`) to guarantee that the output files are a flat list.

Some minor cosmetic fixes have also been applied.
-rw-r--r--main.go41
1 files changed, 34 insertions, 7 deletions
diff --git a/main.go b/main.go
index 7d0b33aa772e..bd5d4cbaf1e7 100644
--- a/main.go
+++ b/main.go
@@ -19,6 +19,7 @@ import (
 	"fmt"
 	"os"
 	"os/exec"
+	"strings"
 
 	"github.com/tazjin/kontemplate/context"
 	"github.com/tazjin/kontemplate/templater"
@@ -99,19 +100,45 @@ func templateCommand() {
 			continue
 		}
 
-		for _, r := range rs.Resources {
-			fmt.Fprintf(os.Stderr, "Rendered file %s/%s:\n", rs.Name, r.Filename)
-			if *templateOutputDir != "" {
-				os.MkdirAll(*templateOutputDir, 0777)
-				f, _ := os.Create(*templateOutputDir + "/" + r.Filename)
-				fmt.Fprintf(f, r.Rendered)
-			} else {
+		if *templateOutputDir != "" {
+			templateIntoDirectory(templateOutputDir, rs)
+		} else {
+			for _, r := range rs.Resources {
+				fmt.Fprintf(os.Stderr, "Rendered file %s/%s:\n", rs.Name, r.Filename)
 				fmt.Println(r.Rendered)
 			}
 		}
 	}
 }
 
+func templateIntoDirectory(outputDir *string, rs templater.RenderedResourceSet) {
+	// Attempt to create the output directory if it does not
+	// already exist:
+	if err := os.MkdirAll(*templateOutputDir, 0775); err != nil {
+		app.Fatalf("Could not create output directory: %v\n", err)
+	}
+
+	// Nested resource sets may contain slashes in their names.
+	// These are replaced with dashes for the purpose of writing a
+	// flat list of output files:
+	setName := strings.Replace(rs.Name, "/", "-", -1)
+
+	for _, r := range rs.Resources {
+		filename := fmt.Sprintf("%s/%s-%s", *templateOutputDir, setName, r.Filename)
+		fmt.Fprintf(os.Stderr, "Writing file %s\n", filename)
+
+		file, err := os.Create(filename)
+		if err != nil {
+			app.Fatalf("Could not create file %s: %v\n", filename, err)
+		}
+
+		_, err = fmt.Fprintf(file, r.Rendered)
+		if err != nil {
+			app.Fatalf("Error writing file %s: %v\n", filename, err)
+		}
+	}
+}
+
 func applyCommand() {
 	ctx, resources := loadContextAndResources(applyFile)