2017-02-08 11:50:49 +01:00
|
|
|
package templater
|
|
|
|
|
|
|
|
import (
|
2017-02-08 11:55:59 +01:00
|
|
|
"bytes"
|
2017-02-08 14:37:36 +01:00
|
|
|
"encoding/json"
|
2017-02-08 11:50:49 +01:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2017-02-08 11:55:59 +01:00
|
|
|
"strings"
|
2017-02-08 11:50:49 +01:00
|
|
|
"text/template"
|
|
|
|
|
2017-02-08 14:37:36 +01:00
|
|
|
"github.com/Masterminds/sprig"
|
2017-02-08 11:50:49 +01:00
|
|
|
"github.com/polydawn/meep"
|
2017-02-08 11:55:59 +01:00
|
|
|
"github.com/tazjin/kontemplate/context"
|
2017-02-10 20:53:59 +01:00
|
|
|
"github.com/tazjin/kontemplate/util"
|
2017-02-08 11:50:49 +01:00
|
|
|
)
|
|
|
|
|
2017-04-04 11:02:34 +02:00
|
|
|
const failOnMissingKeys string = "missingkey=error"
|
|
|
|
|
2017-02-08 11:50:49 +01:00
|
|
|
// 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 {
|
2017-02-09 15:32:14 +01:00
|
|
|
meep.TraitAutodescribing
|
|
|
|
meep.TraitCausable
|
2017-02-08 11:50:49 +01:00
|
|
|
}
|
|
|
|
|
2017-02-08 16:44:55 +01:00
|
|
|
func LoadAndPrepareTemplates(include *[]string, exclude *[]string, c *context.Context) (output []string, err error) {
|
|
|
|
limitedResourceSets := applyLimits(&c.ResourceSets, include, exclude)
|
2017-02-08 12:58:53 +01:00
|
|
|
|
2017-05-04 17:24:36 +02:00
|
|
|
if len(*limitedResourceSets) == 0 {
|
|
|
|
fmt.Fprintln(os.Stderr, "No valid resource sets included!")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-08 16:44:55 +01:00
|
|
|
for _, rs := range *limitedResourceSets {
|
|
|
|
err = processResourceSet(c, &rs, &output)
|
2017-02-08 11:50:49 +01:00
|
|
|
|
2017-02-08 16:44:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
2017-02-08 11:50:49 +01:00
|
|
|
}
|
2017-02-08 12:58:53 +01:00
|
|
|
}
|
|
|
|
|
2017-02-08 16:44:55 +01:00
|
|
|
return
|
2017-02-08 12:58:53 +01:00
|
|
|
}
|
2017-02-08 11:50:49 +01:00
|
|
|
|
2017-02-08 12:58:53 +01:00
|
|
|
func processResourceSet(c *context.Context, rs *context.ResourceSet, output *[]string) error {
|
|
|
|
fmt.Fprintf(os.Stderr, "Loading resources for %s\n", rs.Name)
|
2017-02-08 11:50:49 +01:00
|
|
|
|
2017-02-08 12:58:53 +01:00
|
|
|
rp := path.Join(c.BaseDir, rs.Name)
|
|
|
|
files, err := ioutil.ReadDir(rp)
|
2017-02-08 11:50:49 +01:00
|
|
|
|
2017-02-08 12:58:53 +01:00
|
|
|
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
|
2017-02-08 11:50:49 +01:00
|
|
|
}
|
2017-02-08 12:58:53 +01:00
|
|
|
|
|
|
|
*output = append(*output, o)
|
2017-02-08 11:50:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 12:58:53 +01:00
|
|
|
return nil
|
2017-02-08 11:50:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func templateFile(c *context.Context, rs *context.ResourceSet, filename string) (string, error) {
|
2017-04-04 11:02:34 +02:00
|
|
|
tpl, err := template.New(path.Base(filename)).Funcs(templateFuncs()).Option(failOnMissingKeys).ParseFiles(filename)
|
2017-02-08 11:50:49 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", meep.New(
|
|
|
|
&TemplateNotFoundError{Name: filename},
|
|
|
|
meep.Cause(err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
2017-02-10 20:53:59 +01:00
|
|
|
rs.Values = *util.Merge(&c.Global, &rs.Values)
|
2017-02-08 11:50:49 +01:00
|
|
|
|
|
|
|
err = tpl.Execute(&b, rs.Values)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", meep.New(
|
|
|
|
&TemplatingError{},
|
|
|
|
meep.Cause(err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.String(), nil
|
|
|
|
}
|
|
|
|
|
2017-02-08 16:44:55 +01:00
|
|
|
// 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 {
|
2017-02-08 17:14:40 +01:00
|
|
|
if !matchesResourceSet(exclude, &r) {
|
2017-02-08 16:44:55 +01:00
|
|
|
excluded = append(excluded, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Include included resource sets
|
|
|
|
if len(*include) == 0 {
|
|
|
|
return &excluded
|
|
|
|
}
|
|
|
|
included := make([]context.ResourceSet, 0)
|
|
|
|
for _, r := range excluded {
|
2017-02-08 17:14:40 +01:00
|
|
|
if matchesResourceSet(include, &r) {
|
2017-02-08 16:44:55 +01:00
|
|
|
included = append(included, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &included
|
|
|
|
}
|
|
|
|
|
2017-02-08 17:14:40 +01:00
|
|
|
// Check whether an include/exclude string slice matches a resource set
|
|
|
|
func matchesResourceSet(s *[]string, rs *context.ResourceSet) bool {
|
2017-02-08 16:44:55 +01:00
|
|
|
for _, r := range *s {
|
2017-02-08 17:32:19 +01:00
|
|
|
if r == rs.Name || r == rs.Parent {
|
2017-02-08 16:44:55 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-02-08 14:37:36 +01:00
|
|
|
func templateFuncs() template.FuncMap {
|
|
|
|
m := sprig.TxtFuncMap()
|
|
|
|
m["json"] = func(data interface{}) string {
|
|
|
|
b, _ := json.Marshal(data)
|
|
|
|
return string(b)
|
|
|
|
}
|
2017-02-09 15:33:03 +01:00
|
|
|
m["passLookup"] = GetFromPass
|
2017-02-08 14:37:36 +01:00
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2017-04-04 14:36:27 +02:00
|
|
|
// Checks whether a file is a resource file (i.e. is YAML or JSON) and not a default values file.
|
2017-02-08 11:50:49 +01:00
|
|
|
func isResourceFile(f os.FileInfo) bool {
|
2017-04-04 14:36:27 +02:00
|
|
|
if f.Name() == "default.json" || f.Name() == "default.yaml" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-02-08 11:50:49 +01:00
|
|
|
return strings.HasSuffix(f.Name(), "yaml") ||
|
|
|
|
strings.HasSuffix(f.Name(), "yml") ||
|
|
|
|
strings.HasSuffix(f.Name(), "json")
|
|
|
|
}
|