tartrazine/internal/code-generator/generator/vendor.go

38 lines
905 B
Go
Raw Normal View History

2017-04-06 15:31:17 +00:00
package generator
import (
"bytes"
"html/template"
"io"
yaml "gopkg.in/yaml.v2"
)
2017-04-10 08:32:54 +00:00
// Vendor reads from buf and builds vendor_matchers.go file from vendorTmplPath.
func Vendor(data []byte, vendorTmplPath, vendorTmplName, commit string) ([]byte, error) {
2017-04-06 15:31:17 +00:00
var regexpList []string
if err := yaml.Unmarshal(data, &regexpList); err != nil {
return nil, err
}
buf := &bytes.Buffer{}
if err := executeVendorTemplate(buf, regexpList, vendorTmplPath, vendorTmplName, commit); err != nil {
2017-04-06 15:31:17 +00:00
return nil, err
}
return buf.Bytes(), nil
}
func executeVendorTemplate(out io.Writer, regexpList []string, vendorTmplPath, vendorTmpl, commit string) error {
2017-04-06 15:31:17 +00:00
fmap := template.FuncMap{
"getCommit": func() string { return commit },
}
t := template.Must(template.New(vendorTmpl).Funcs(fmap).ParseFiles(vendorTmplPath))
2017-04-06 15:31:17 +00:00
if err := t.Execute(out, regexpList); err != nil {
return err
}
return nil
}