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.
|
2017-04-10 08:25:52 +00:00
|
|
|
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, ®expList); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
2017-04-10 08:25:52 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-04-10 08:27:44 +00:00
|
|
|
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 },
|
|
|
|
}
|
|
|
|
|
2017-04-10 08:27:44 +00:00
|
|
|
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
|
|
|
|
}
|