2017-04-06 15:31:17 +00:00
|
|
|
package generator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
2017-05-31 10:07:46 +00:00
|
|
|
"text/template"
|
2017-04-06 15:31:17 +00:00
|
|
|
|
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2017-05-29 08:05:16 +00:00
|
|
|
// Vendor reads from buf and builds source 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
|
|
|
|
}
|