-
Notifications
You must be signed in to change notification settings - Fork 0
/
coder_tpl.go
193 lines (174 loc) · 4.78 KB
/
coder_tpl.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package fc
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
sprig "github.com/Masterminds/sprig/v3"
"github.com/ashb/jqrepl/jq"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/juju/errors"
)
type coderTPL struct {
funcMap map[string]interface{}
conv *Recoder
importer *importer
}
func newCoderTPL(conv *Recoder, s3client s3iface.S3API) *coderTPL {
c := &coderTPL{}
c.importer = newImporter(conv, s3client)
c.conv = conv
return c
}
func (c *coderTPL) Initialize() error {
c.funcMap = sprig.TxtFuncMap()
c.funcMap["include"] = c.tplFuncInclude
c.funcMap["import"] = c.tplFuncImport
c.funcMap["jq"] = func(p string, in interface{}) (interface{}, error) {
libjq, err := jq.New()
if err != nil {
return nil, errors.Annotatef(err, "cannot initialize jq library")
}
defer libjq.Close()
chanIn, chanOut, chanErr := libjq.Start(strings.Replace(p, "'", "\"", -1), jq.JvArray())
inCopy, err := jq.JvFromInterface(in)
if err != nil {
return nil, errors.Annotatef(err, "cannot encode input data for jq")
}
var res interface{}
for chanErr != nil && chanOut != nil {
select {
case e, ok := <-chanErr:
if !ok {
chanErr = nil
} else {
err = errors.Trace(e)
}
case o, ok := <-chanOut:
if !ok {
chanOut = nil
} else if res == nil {
res = o.ToGoVal()
}
case chanIn <- inCopy:
close(chanIn)
chanIn = nil
}
}
return res, err
}
for n, f := range c.conv.Coders {
name := n
if _, ok := f.(Decoder); ok {
c.funcMap["decode_"+name] = func(in string, args ...string) (interface{}, error) {
data, _, err := c.conv.Decode(&Config{
Decoder: name,
DecoderArgs: args,
Input: bytes.NewBufferString(in),
})
if err != nil {
return nil, errors.Annotatef(err, "error while decoding %s", name)
}
return data, nil
}
}
if _, ok := f.(Encoder); ok {
c.funcMap["encode_"+name] = func(in map[string]interface{}, args ...string) (string, error) {
var buf bytes.Buffer
err := c.conv.Encode(&Config{
Encoder: name,
EncoderArgs: args,
Output: &buf,
}, in, nil)
if err != nil {
return "", errors.Annotatef(err, "error while encoding %s", name)
}
return buf.String(), nil
}
}
}
return nil
}
func (c *coderTPL) tplFuncImport(fileURL string, options ...string) (res interface{}, err error) {
var opts importOpts
for _, p := range strings.Split(strings.Join(options, ","), ",") {
p = strings.TrimSpace(p)
switch p {
case "":
case "raw":
opts.raw = true
case "nofail":
opts.nofail = true
case "pattern":
opts.pattern = true
case "metadata":
opts.metadata = true
default:
return nil, errors.Errorf("unexpected import option '%s'", p)
}
}
return c.importer.importURL(fileURL, opts)
}
func (c *coderTPL) tplFuncInclude(path string, ctx interface{}, metadata ...interface{}) (string, error) {
buf, err := c.include(path, ctx, nil)
if err != nil {
return "", errors.Trace(err)
}
return buf.String(), nil
}
func (c *coderTPL) Names() []string {
return []string{"tpl"}
}
func (c *coderTPL) Encode(out io.Writer, in interface{}, metadata interface{}, args []string) error {
if len(args) != 1 {
return errors.Trace(ArgumentError{error: "tpl: expecting one argument: template file"})
}
buf, err := c.include(args[0], in, metadata)
if err != nil {
return errors.Annotatef(err, "tpl: error while parsing template")
}
_, err = io.Copy(out, buf)
return errors.Annotatef(err, "tpl: cannot write")
}
func (c *coderTPL) newFuncMap(metadata interface{}) map[string]interface{} {
funcMap := make(map[string]interface{})
for k, v := range c.funcMap {
funcMap[k] = v
}
funcMap["metadata"] = func() interface{} {
return metadata
}
return funcMap
}
func (c *coderTPL) include(path string, ctx interface{}, metadata interface{}) (*bytes.Buffer, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, errors.Annotatef(err, "tpl: cannot get current directory")
}
defer func() {
if err := os.Chdir(cwd); err != nil {
panic(fmt.Sprintf("err: cannot chdir back to '%s', %s", cwd, err))
}
}()
dir := filepath.Dir(path)
if err = os.Chdir(dir); err != nil {
return nil, errors.Annotatef(err, "tpl: cannot chdir to template directory '%s'", dir)
}
content, err := ioutil.ReadFile(filepath.Base(path))
if err != nil {
return nil, errors.Annotatef(err, "tpl: cannot read template '%s'", path)
}
tpl, err := template.New(filepath.Join(cwd, path)).Funcs(c.newFuncMap(metadata)).Parse(string(content))
if err != nil {
return nil, errors.Annotatef(err, "tpl: cannot parse template '%s'", path)
}
var buf bytes.Buffer
if err = tpl.Execute(&buf, ctx); err != nil {
return nil, errors.Annotatef(err, "tpl: cannot render template '%s'", path)
}
return &buf, nil
}