about summary refs log tree commit diff
path: root/templater/templater.go
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2017-06-11T19·25+0200
committerVincent Ambo <tazjin@gmail.com>2017-06-11T20·09+0200
commitf3264329b92c93177e771e3786a91e0b9c68dec5 (patch)
tree3d173d310e45649437997f5634d5760db99476c0 /templater/templater.go
parentd76ea59f4c9dcdaace382568d2a4037ccce1ccba (diff)
refactor templater: Add intermediate type to represent rendered RSes
As a first step in resolving #51 this refactors the `templater`
package to return rendered resource sets as a distinct type.

This also fixes #56
Diffstat (limited to 'templater/templater.go')
-rw-r--r--templater/templater.go49
1 files changed, 35 insertions, 14 deletions
diff --git a/templater/templater.go b/templater/templater.go
index 9a00da3a75f6..afb96423e62e 100644
--- a/templater/templater.go
+++ b/templater/templater.go
@@ -30,58 +30,79 @@ type TemplatingError struct {
 	meep.TraitCausable
 }
 
-func LoadAndPrepareTemplates(include *[]string, exclude *[]string, c *context.Context) (output []string, err error) {
+type RenderedResource struct {
+	Filename string
+	Rendered string
+}
+
+type RenderedResourceSet struct {
+	Name      string
+	Resources []RenderedResource
+}
+
+func LoadAndApplyTemplates(include *[]string, exclude *[]string, c *context.Context) ([]RenderedResourceSet, error) {
 	limitedResourceSets := applyLimits(&c.ResourceSets, include, exclude)
+	renderedResourceSets := make([]RenderedResourceSet, len(c.ResourceSets))
 
 	if len(*limitedResourceSets) == 0 {
-		fmt.Fprintln(os.Stderr, "No valid resource sets included!")
-		return
+		return renderedResourceSets, fmt.Errorf("No valid resource sets included!")
 	}
 
 	for _, rs := range *limitedResourceSets {
-		err = processResourceSet(c, &rs, &output)
+		set, err := processResourceSet(c, &rs)
 
 		if err != nil {
-			return
+			return nil, err
 		}
+
+		renderedResourceSets = append(renderedResourceSets, *set)
 	}
 
-	return
+	return renderedResourceSets, nil
 }
 
-func processResourceSet(c *context.Context, rs *context.ResourceSet, output *[]string) error {
+func processResourceSet(c *context.Context, rs *context.ResourceSet) (*RenderedResourceSet, error) {
 	fmt.Fprintf(os.Stderr, "Loading resources for %s\n", rs.Name)
 
 	rp := path.Join(c.BaseDir, rs.Name)
 	files, err := ioutil.ReadDir(rp)
 
-	err = processFiles(c, rs, rp, files, output)
+	resources, err := processFiles(c, rs, rp, files)
 
 	if err != nil {
-		return meep.New(
+		return nil, meep.New(
 			&TemplateNotFoundError{Name: rs.Name},
 			meep.Cause(err),
 		)
 	}
 
-	return nil
+	return &RenderedResourceSet{
+		Name:      rs.Name,
+		Resources: resources,
+	}, nil
 }
 
-func processFiles(c *context.Context, rs *context.ResourceSet, rp string, files []os.FileInfo, output *[]string) error {
+func processFiles(c *context.Context, rs *context.ResourceSet, rp string, files []os.FileInfo) ([]RenderedResource, error) {
+	resources := make([]RenderedResource, len(c.ResourceSets))
+
 	for _, file := range files {
 		if !file.IsDir() && isResourceFile(file) {
 			p := path.Join(rp, file.Name())
 			o, err := templateFile(c, rs, p)
 
 			if err != nil {
-				return err
+				return resources, err
 			}
 
-			*output = append(*output, o)
+			res := RenderedResource{
+				Filename: file.Name(),
+				Rendered: o,
+			}
+			resources = append(resources, res)
 		}
 	}
 
-	return nil
+	return resources, nil
 }
 
 func templateFile(c *context.Context, rs *context.ResourceSet, filename string) (string, error) {