github.com/zkry/enry@v1.6.3/internal/code-generator/generator/vendor.go (about)

     1  package generator
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"io/ioutil"
     7  	"text/template"
     8  
     9  	yaml "gopkg.in/yaml.v2"
    10  )
    11  
    12  // Vendor reads from fileToParse and builds source file from tmplPath. It complies with type File signature.
    13  func Vendor(fileToParse, samplesDir, outPath, tmplPath, tmplName, commit string) error {
    14  	data, err := ioutil.ReadFile(fileToParse)
    15  	if err != nil {
    16  		return err
    17  	}
    18  
    19  	var regexpList []string
    20  	if err := yaml.Unmarshal(data, &regexpList); err != nil {
    21  		return nil
    22  	}
    23  
    24  	buf := &bytes.Buffer{}
    25  	if err := executeVendorTemplate(buf, regexpList, tmplPath, tmplName, commit); err != nil {
    26  		return nil
    27  	}
    28  
    29  	return formatedWrite(outPath, buf.Bytes())
    30  }
    31  
    32  func executeVendorTemplate(out io.Writer, regexpList []string, tmplPath, tmplName, commit string) error {
    33  	fmap := template.FuncMap{
    34  		"getCommit": func() string { return commit },
    35  	}
    36  
    37  	t := template.Must(template.New(tmplName).Funcs(fmap).ParseFiles(tmplPath))
    38  	if err := t.Execute(out, regexpList); err != nil {
    39  		return err
    40  	}
    41  
    42  	return nil
    43  }