in generator/app.go [431:473]
func writeTemplate(templatePath, outputPath string, fileFormat string, data interface{}) error {
// set up template
t, err := template.New(filepath.Base(templatePath)).
Funcs(funcMap).
ParseFiles(templatePath, filepath.Join(baseGeneratorDir, templateDir, headerTemplate))
if err != nil {
return err
}
// create if not exists
if !pathExists(outputPath) {
_, err = os.Create(outputPath)
if err != nil {
return err
}
}
// open file and truncate
f, err := os.OpenFile(outputPath, os.O_RDWR, 0644)
if err != nil {
return err
}
defer f.Close()
// get any existing content
content, err := getExistingContent(outputPath, fileFormat)
if err != nil {
return err
}
// ensure file is empty
f.Truncate(0)
// generated content
err = t.Execute(f, data)
if err != nil {
return err
}
writeCustomContentBlock(f, content, fileFormat)
return nil
}