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

44 lines
1003 B
Go
Raw Normal View History

2017-04-06 15:31:17 +00:00
package generator
import (
"bytes"
"io"
2017-06-13 11:56:07 +00:00
"io/ioutil"
2017-05-31 10:07:46 +00:00
"text/template"
2017-04-06 15:31:17 +00:00
yaml "gopkg.in/yaml.v2"
)
// Vendor reads from fileToParse and builds source file from tmplPath. It complies with type File signature.
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, &regexpList); 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 {
2017-04-06 15:31:17 +00:00
fmap := template.FuncMap{
"getCommit": func() string { return commit },
}
2017-06-13 11:56:07 +00:00
t := template.Must(template.New(tmplName).Funcs(fmap).ParseFiles(tmplPath))
2017-04-06 15:31:17 +00:00
if err := t.Execute(out, regexpList); err != nil {
return err
}
return nil
}