about summary refs log tree commit diff
path: root/templater/templater.go
blob: 5c1db77aadfd0fd4efd0a1fdf83c9dc06ba299b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package templater

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"path"
	"strings"
	"text/template"

	"github.com/polydawn/meep"
	"github.com/tazjin/kontemplate/context"
)

// Error that is caused by non-existent template files being specified
type TemplateNotFoundError struct {
	meep.AllTraits
	Name string
}

// Error that is caused during templating, e.g. required value being absent or invalid template format
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)

			if err != nil {
				return
			}
		}
	}

	return
}

func resourceSetIncluded(limit *[]string, resourceSetName *string) bool {
	if len(*limit) == 0 {
		return true
	}

	for _, name := range *limit {
		if name == *resourceSetName {
			return true
		}
	}

	return false
}

func processResourceSet(c *context.Context, rs *context.ResourceSet, output *[]string) 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)

	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 nil
}

func templateFile(c *context.Context, rs *context.ResourceSet, filename string) (string, error) {
	tpl, err := template.ParseFiles(filename)

	if err != nil {
		return "", meep.New(
			&TemplateNotFoundError{Name: filename},
			meep.Cause(err),
		)
	}

	var b bytes.Buffer

	// Guard against empty map before merging keys
	if rs.Values == nil {
		rs.Values = make(map[string]interface{}, 0)
	}

	// Merge global and resourceset-specific values (don't override from global)
	for k, v := range c.Global {
		if _, ok := rs.Values[k]; !ok {
			rs.Values[k] = v
		}
	}

	err = tpl.Execute(&b, rs.Values)

	if err != nil {
		return "", meep.New(
			&TemplatingError{},
			meep.Cause(err),
		)
	}

	return b.String(), nil
}

// Checks whether a file is a resource file (i.e. is YAML or JSON)
func isResourceFile(f os.FileInfo) bool {
	return strings.HasSuffix(f.Name(), "yaml") ||
		strings.HasSuffix(f.Name(), "yml") ||
		strings.HasSuffix(f.Name(), "json")
}