2017-04-06 15:31:17 +00:00
|
|
|
package generator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
2017-06-13 11:56:07 +00:00
|
|
|
"io/ioutil"
|
2019-02-14 11:47:45 +00:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
2017-04-06 15:31:17 +00:00
|
|
|
)
|
|
|
|
|
2019-02-14 11:47:45 +00:00
|
|
|
// Vendor generates regex matchers in Go for vendoring files/dirs.
|
|
|
|
// It is of generator.File type.
|
2017-06-13 11:56:07 +00:00
|
|
|
func Vendor(fileToParse, samplesDir, outPath, tmplPath, tmplName, commit string) error {
|
|
|
|
data, err := ioutil.ReadFile(fileToParse)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-06 15:31:17 +00:00
|
|
|
var regexpList []string
|
|
|
|
if err := yaml.Unmarshal(data, ®expList); err != nil {
|
2017-06-13 11:56:07 +00:00
|
|
|
return nil
|
2017-04-06 15:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
2017-06-13 11:56:07 +00:00
|
|
|
if err := executeVendorTemplate(buf, regexpList, tmplPath, tmplName, commit); err != nil {
|
|
|
|
return nil
|
2017-04-06 15:31:17 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 11:56:07 +00:00
|
|
|
return formatedWrite(outPath, buf.Bytes())
|
2017-04-06 15:31:17 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 11:56:07 +00:00
|
|
|
func executeVendorTemplate(out io.Writer, regexpList []string, tmplPath, tmplName, commit string) error {
|
2018-04-28 13:12:03 +00:00
|
|
|
return executeTemplate(out, tmplName, tmplPath, commit, nil, regexpList)
|
2017-04-06 15:31:17 +00:00
|
|
|
}
|