-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathtemplate.go
84 lines (67 loc) · 2.01 KB
/
template.go
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
package templates
//go:generate packr
import (
"bufio"
"bytes"
"io"
"text/template"
"github.com/gobuffalo/packr"
)
// GetAsset reads an asset from "disk" into a string
func GetAsset(assetName string, options ...AssetOption) (string, error) {
box := packr.NewBox("./assets")
asset, err := box.MustString(assetName)
if err != nil {
return "", err
}
for _, option := range options {
if asset, err = option(assetName, asset); err != nil {
return "", err
}
}
return asset, nil
}
// ExecuteTemplate executes the data parameter on a text/template
func ExecuteTemplate(data interface{}) AssetOption {
return func(assetName string, asset string) (string, error) {
tmpl, err := template.New(assetName).Parse(asset)
if err != nil {
return asset, err
}
buf := new(bytes.Buffer)
bufWriter := bufio.NewWriter(buf)
err = tmpl.Execute(bufWriter, data)
if err != nil {
return asset, err
}
bufWriter.Flush()
templateBodyBytes := new(bytes.Buffer)
_, err = templateBodyBytes.ReadFrom(buf)
if err != nil {
return asset, err
}
return templateBodyBytes.String(), nil
}
}
// DecorateTemplate uses an ExtensionImpl to inject data into an extension
func DecorateTemplate(extMgr StackTemplateDecorator,
stackName string) AssetOption {
return func(assetName string, asset string) (string, error) {
assetBuf := bytes.NewBufferString(asset)
newBuf, err := extMgr.DecorateStackTemplate(assetName, stackName, assetBuf)
if err != nil {
return "", err
}
templateBodyBytes := new(bytes.Buffer)
if _, err := templateBodyBytes.ReadFrom(newBuf); err != nil {
return "", err
}
return templateBodyBytes.String(), nil
}
}
// AssetOption describes the method signature for manipulating loaded assets
type AssetOption func(string, string) (string, error)
// StackTemplateDecorator is a stub for decorate template struct to avoid circular dependencies
type StackTemplateDecorator interface {
DecorateStackTemplate(assetName string, stackName string, templateBody io.Reader) (io.Reader, error)
}