github.com/dennwc/enry@v1.6.4-0.20180424151738-42391b8e105b/internal/code-generator/generator/documentation.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 // Documentation reads from fileToParse and builds source file from tmplPath. It complies with type File signature. 13 func Documentation(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, ®expList); err != nil { 21 return err 22 } 23 24 buf := &bytes.Buffer{} 25 if err := executeDocumentationTemplate(buf, regexpList, tmplPath, tmplName, commit); err != nil { 26 return err 27 } 28 29 return formatedWrite(outPath, buf.Bytes()) 30 } 31 32 func executeDocumentationTemplate(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 }