about summary refs log tree commit diff
path: root/templater
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2017-02-08T11·58+0100
committerVincent Ambo <tazjin@gmail.com>2017-02-08T11·58+0100
commit7ac63613fb64c6cec3119afc51ba4bac91b81a94 (patch)
tree58927d534621fe94ea882628df372de7901007a5 /templater
parent8fac7c1a415d6ea5e41c28f4e7ab5c1ef0883997 (diff)
feat main: Add proper CLI support
Adds a basic CLI structure with a single "run" command that takes a --file (-f)
and --limit (-l) flag.

--limit can be used to only output certain resource sets.

Closes #4
Diffstat (limited to 'templater')
-rw-r--r--templater/templater.go70
1 files changed, 50 insertions, 20 deletions
diff --git a/templater/templater.go b/templater/templater.go
index 27beff371e5c..a2c860c60d3d 100644
--- a/templater/templater.go
+++ b/templater/templater.go
@@ -24,37 +24,67 @@ type TemplatingError struct {
 	meep.AllTraits
 }
 
-func LoadAndPrepareTemplates(c *context.Context) ([]string, error) {
-	output := make([]string, 0)
-
+func LoadAndPrepareTemplates(limit *[]string, c *context.Context) (output []string, err error) {
 	for _, rs := range c.ResourceSets {
-		fmt.Fprintf(os.Stderr, "Loading resources for %s\n", rs.Name)
+		if resourceSetIncluded(limit, &rs.Name) {
+			err = processResourceSet(c, &rs, &output)
+
+			if err != nil {
+				return
+			}
+		}
+	}
 
-		rp := path.Join(c.BaseDir, rs.Name)
-		files, err := ioutil.ReadDir(rp)
+	return
+}
 
-		if err != nil {
-			return nil, meep.New(
-				&TemplateNotFoundError{Name: rs.Name},
-				meep.Cause(err),
-			)
+func resourceSetIncluded(limit *[]string, resourceSetName *string) bool {
+	if len(*limit) == 0 {
+		return true
+	}
+
+	for _, name := range *limit {
+		if name == *resourceSetName {
+			return true
 		}
+	}
+
+	return false
+}
 
-		for _, file := range files {
-			if !file.IsDir() && isResourceFile(file) {
-				p := path.Join(rp, file.Name())
-				o, err := templateFile(c, &rs, p)
+func processResourceSet(c *context.Context, rs *context.ResourceSet, output *[]string) error {
+	fmt.Fprintf(os.Stderr, "Loading resources for %s\n", rs.Name)
 
-				if err != nil {
-					return nil, err
-				}
+	rp := path.Join(c.BaseDir, rs.Name)
+	files, err := ioutil.ReadDir(rp)
 
-				output = append(output, o)
+	err = processFiles(c, rs, rp, files, output)
+
+	if err != nil {
+		return meep.New(
+			&TemplateNotFoundError{Name: rs.Name},
+			meep.Cause(err),
+		)
+	}
+
+	return nil
+}
+
+func processFiles(c *context.Context, rs *context.ResourceSet, rp string, files []os.FileInfo, output *[]string) error {
+	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
 			}
+
+			*output = append(*output, o)
 		}
 	}
 
-	return output, nil
+	return nil
 }
 
 func templateFile(c *context.Context, rs *context.ResourceSet, filename string) (string, error) {