forked from gopasspw/gopass
/
template.go
44 lines (36 loc) · 807 Bytes
/
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
package tpl
import (
"bytes"
"context"
"path/filepath"
"text/template"
"github.com/gopasspw/gopass/pkg/store"
)
type kvstore interface {
Get(context.Context, string) (store.Secret, error)
}
type payload struct {
Dir string
Path string
Name string
Content string
}
// Execute executes the given template
func Execute(ctx context.Context, tpl, name string, content []byte, s kvstore) ([]byte, error) {
funcs := funcMap(ctx, s)
pl := payload{
Dir: filepath.Dir(name),
Path: name,
Name: filepath.Base(name),
Content: string(content),
}
tmpl, err := template.New(tpl).Funcs(funcs).Parse(tpl)
if err != nil {
return []byte{}, err
}
buff := &bytes.Buffer{}
if err := tmpl.Execute(buff, pl); err != nil {
return []byte{}, err
}
return buff.Bytes(), nil
}