-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.go
52 lines (43 loc) · 1.09 KB
/
build.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
package casper
import (
"bytes"
"io"
"io/ioutil"
"text/template"
"github.com/miracl/casper/source"
"github.com/pkg/errors"
)
var funcMap = template.FuncMap{
"last": isLast,
"notLast": isNotLast,
"quote": quote,
}
// BuildConfig represent a configuration.
type BuildConfig struct {
Template io.Reader
Source source.Getter
}
// Build creates the config based on the template and the environment files.
func (c BuildConfig) Build() ([]byte, error) {
// compile the template for the config
cfgTmplBody, err := ioutil.ReadAll(c.Template)
if err != nil {
return nil, errors.Wrap(err, "reading template failed")
}
cfgTmpl, err := template.New("config").
Funcs(funcMap).
Parse(string(cfgTmplBody))
if err != nil {
return nil, errors.Wrap(err, "template error")
}
var cfg bytes.Buffer
if err := cfgTmpl.Execute(&cfg, c.Source.Get()); err != nil {
return nil, errors.Wrap(err, "executing template failed")
}
// convert to string
cfgStr, err := ioutil.ReadAll(&cfg)
if err != nil {
return nil, errors.Wrap(err, "serializing template to string failed")
}
return cfgStr, nil
}