// Render template and write generated source code
// generated code is reformatted ("linted"), which gives an
// additional level of checking. If this step fails, the generated
// code is still dumped, for template debugging purposes.
Render template and write generated source code
generated code is reformatted ("linted"), which gives an
additional level of checking. If this step fails, the generated
code is still dumped, for template debugging purposes.
// Render template and write generated source code // generated code is reformatted ( " linted " ) which gives an // additional level of checking . If this step fails the generated // code is still dumped for template debugging purposes .
func (g *GenOpts) write(t *TemplateOpts, data interface{}) error {
	dir, fname, err := g.location(t, data)
	if err != nil {
		return fmt.Errorf("failed to resolve template location for template %s: %v", t.Name, err)
	}
	if t.SkipExists && fileExists(dir, fname) {
		debugLog("skipping generation of %s because it already exists and skip_exist directive is set for %s",
			filepath.Join(dir, fname), t.Name)
		return nil
	}
	log.Printf("creating generated file %q in %q as %s", fname, dir, t.Name)
	content, err := g.render(t, data)
	if err != nil {
		return fmt.Errorf("failed rendering template data for %s: %v", t.Name, err)
	}
	if dir != "" {
		_, exists := os.Stat(dir)
		if os.IsNotExist(exists) {
			debugLog("creating directory %q for \"%s\"", dir, t.Name)
			if e := os.MkdirAll(dir, 0755); e != nil {
				return e
			}
		}
	}
	formatted := content
	var writeerr error
	if !t.SkipFormat {
		formatted, err = g.LanguageOpts.FormatContent(fname, content)
		if err != nil {
			log.Printf("source formatting failed on template-generated source (%q for %s). Check that your template produces valid code", filepath.Join(dir, fname), t.Name)
			writeerr = ioutil.WriteFile(filepath.Join(dir, fname), content, 0644)
			if writeerr != nil {
				return fmt.Errorf("failed to write (unformatted) file %q in %q: %v", fname, dir, writeerr)
			}
			log.Printf("unformatted generated source %q has been dumped for template debugging purposes. DO NOT build on this source!", fname)
			return fmt.Errorf("source formatting on generated source %q failed: %v", t.Name, err)
		}
	}
	writeerr = ioutil.WriteFile(filepath.Join(dir, fname), formatted, 0644)
	if writeerr != nil {
		return fmt.Errorf("failed to write file %q in %q: %v", fname, dir, writeerr)
	}
	return err
}
func ( g * GenOpts ) write ( t * TemplateOpts , data interface { } ) error { dir , fname , err := g . location ( t , data ) if err != nil { return fmt . Errorf ( " " , t . Name , err ) } if t . SkipExists && fileExists ( dir , fname ) { debugLog ( " " , filepath . Join ( dir , fname ) , t . Name ) return nil } log . Printf ( " " , fname , dir , t . Name ) content , err := g . render ( t , data ) if err != nil { return fmt . Errorf ( " " , t . Name , err ) } if dir != " " { _ , exists := os . Stat ( dir ) if os . IsNotExist ( exists ) { debugLog ( " \" \" " , dir , t . Name ) if e := os . MkdirAll ( dir , 0755 ) ; e != nil { return e } } } formatted := content var writeerr error if ! t . SkipFormat { formatted , err = g . LanguageOpts . FormatContent ( fname , content ) if err != nil { log . Printf ( " " , filepath . Join ( dir , fname ) , t . Name ) writeerr = ioutil . WriteFile ( filepath . Join ( dir , fname ) , content , 0644 ) if writeerr != nil { return fmt . Errorf ( " " , fname , dir , writeerr ) } log . Printf ( " " , fname ) return fmt . Errorf ( " " , t . Name , err ) } } writeerr = ioutil . WriteFile ( filepath . Join ( dir , fname ) , formatted , 0644 ) if writeerr != nil { return fmt . Errorf ( " " , fname , dir , writeerr ) } return err }
Render template and write generated source code
generated code is reformatted ("linted"), which gives an
additional level of checking.
Render template and write generated source code generated code is reformatted ( " linted " ) which gives an additional level of checking .
// Directory settings consistent with file privileges. // Environment's umask may alter this setup // Conditionally format the code, unless the user wants to skip