about summary refs log tree commit diff
path: root/templater
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2017-02-08T15·44+0100
committerVincent Ambo <tazjin@gmail.com>2017-02-08T15·53+0100
commit8e08a282eb4f29618c8de8090927b521ea0c9c2b (patch)
tree7a2731061681c8ddfc22a6d3af0255e8a33d12aa /templater
parent11a5cf9e192b57eb594ce276ef7723693ff28014 (diff)
feat templater: Add ability to exclude resource sets
* renamed --limit to --include (-i)
* added --exclude (-e)

Kontemplate users can now explicitly include and exclude certain resource sets.
Excludes always override includes.

Closes #11
Diffstat (limited to 'templater')
-rw-r--r--templater/templater.go68
1 files changed, 47 insertions, 21 deletions
diff --git a/templater/templater.go b/templater/templater.go
index 6e443c14bb07..bb65cd1df058 100644
--- a/templater/templater.go
+++ b/templater/templater.go
@@ -26,32 +26,18 @@ type TemplatingError struct {
 	meep.AllTraits
 }
 
-func LoadAndPrepareTemplates(limit *[]string, c *context.Context) (output []string, err error) {
-	for _, rs := range c.ResourceSets {
-		if resourceSetIncluded(limit, &rs.Name) {
-			err = processResourceSet(c, &rs, &output)
+func LoadAndPrepareTemplates(include *[]string, exclude *[]string, c *context.Context) (output []string, err error) {
+	limitedResourceSets := applyLimits(&c.ResourceSets, include, exclude)
 
-			if err != nil {
-				return
-			}
-		}
-	}
+	for _, rs := range *limitedResourceSets {
+		err = processResourceSet(c, &rs, &output)
 
-	return
-}
-
-func resourceSetIncluded(limit *[]string, resourceSetName *string) bool {
-	if len(*limit) == 0 {
-		return true
-	}
-
-	for _, name := range *limit {
-		if name == *resourceSetName {
-			return true
+		if err != nil {
+			return
 		}
 	}
 
-	return false
+	return
 }
 
 func processResourceSet(c *context.Context, rs *context.ResourceSet, output *[]string) error {
@@ -125,6 +111,46 @@ func templateFile(c *context.Context, rs *context.ResourceSet, filename string)
 	return b.String(), nil
 }
 
+// Applies the limits of explicitly included or excluded resources and returns the updated resource set.
+// Exclude takes priority over include
+func applyLimits(rs *[]context.ResourceSet, include *[]string, exclude *[]string) *[]context.ResourceSet {
+	if len(*include) == 0 && len(*exclude) == 0 {
+		return rs
+	}
+
+	// Exclude excluded resource sets
+	excluded := make([]context.ResourceSet, 0)
+	for _, r := range *rs {
+		if !contains(exclude, &r.Name) {
+			excluded = append(excluded, r)
+		}
+	}
+
+	// Include included resource sets
+	if len(*include) == 0 {
+		return &excluded
+	}
+	included := make([]context.ResourceSet, 0)
+	for _, r := range excluded {
+		if contains(include, &r.Name) {
+			included = append(included, r)
+		}
+	}
+
+	return &included
+}
+
+// Check whether a certain string is contained in a string slice
+func contains(s *[]string, v *string) bool {
+	for _, r := range *s {
+		if r == *v {
+			return true
+		}
+	}
+
+	return false
+}
+
 func templateFuncs() template.FuncMap {
 	m := sprig.TxtFuncMap()
 	m["json"] = func(data interface{}) string {