summaryrefslogtreecommitdiff
path: root/gomplate.go
blob: 78317634c225719b87c2e8b44012cb8e13037961 (plain)
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
package main

import (
	"io"
	"log"
	"net/url"
	"strings"
	"text/template"

	"github.com/hairyhenderson/gomplate/aws"
)

func (g *Gomplate) createTemplate() *template.Template {
	return template.New("template").Funcs(g.funcMap).Option("missingkey=error")
}

// Gomplate -
type Gomplate struct {
	funcMap    template.FuncMap
	leftDelim  string
	rightDelim string
}

// RunTemplate -
func (g *Gomplate) RunTemplate(text string, out io.Writer) {
	context := &Context{}
	tmpl, err := g.createTemplate().Delims(g.leftDelim, g.rightDelim).Parse(text)
	if err != nil {
		log.Fatalf("Line %q: %v\n", text, err)
	}

	if err := tmpl.Execute(out, context); err != nil {
		panic(err)
	}
}

// NewGomplate -
func NewGomplate(data *Data, leftDelim, rightDelim string) *Gomplate {
	env := &Env{}
	typeconv := &TypeConv{}
	stringfunc := &stringFunc{}
	ec2meta := aws.NewEc2Meta()
	ec2info := aws.NewEc2Info()
	return &Gomplate{
		leftDelim:  leftDelim,
		rightDelim: rightDelim,
		funcMap: template.FuncMap{
			"getenv":           env.Getenv,
			"bool":             typeconv.Bool,
			"has":              typeconv.Has,
			"json":             typeconv.JSON,
			"jsonArray":        typeconv.JSONArray,
			"yaml":             typeconv.YAML,
			"yamlArray":        typeconv.YAMLArray,
			"csv":              typeconv.CSV,
			"csvByRow":         typeconv.CSVByRow,
			"csvByColumn":      typeconv.CSVByColumn,
			"slice":            typeconv.Slice,
			"indent":           typeconv.indent,
			"join":             typeconv.Join,
			"toJSON":           typeconv.ToJSON,
			"toJSONPretty":     typeconv.toJSONPretty,
			"toYAML":           typeconv.ToYAML,
			"toCSV":            typeconv.ToCSV,
			"ec2meta":          ec2meta.Meta,
			"ec2dynamic":       ec2meta.Dynamic,
			"ec2tag":           ec2info.Tag,
			"ec2region":        ec2meta.Region,
			"contains":         strings.Contains,
			"hasPrefix":        strings.HasPrefix,
			"hasSuffix":        strings.HasSuffix,
			"replaceAll":       stringfunc.replaceAll,
			"split":            strings.Split,
			"splitN":           strings.SplitN,
			"title":            strings.Title,
			"toUpper":          strings.ToUpper,
			"toLower":          strings.ToLower,
			"trim":             strings.Trim,
			"trimSpace":        strings.TrimSpace,
			"urlParse":         url.Parse,
			"datasource":       data.Datasource,
			"ds":               data.Datasource,
			"datasourceExists": data.DatasourceExists,
			"include":          data.include,
		},
	}
}

func runTemplate(o *GomplateOpts) error {
	defer runCleanupHooks()
	data := NewData(o.dataSources, o.dataSourceHeaders)

	g := NewGomplate(data, o.lDelim, o.rDelim)

	if o.inputDir != "" {
		return processInputDir(o.inputDir, o.outputDir, g)
	}

	return processInputFiles(o.input, o.inputFiles, o.outputFiles, g)
}

// Called from process.go ...
func renderTemplate(g *Gomplate, inString string, outPath string) error {
	outFile, err := openOutFile(outPath)
	if err != nil {
		return err
	}
	// nolint: errcheck
	defer outFile.Close()
	g.RunTemplate(inString, outFile)
	return nil
}