Skip to content

Commit

Permalink
codegen/templates: allow templates to be passed in options instead of…
Browse files Browse the repository at this point in the history
… os files
  • Loading branch information
marwan-at-work committed Apr 13, 2019
1 parent 5ff6092 commit a0ee717
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions codegen/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var CurrentImports *Imports

type Options struct {
PackageName string
Template string
Filename string
RegionTags bool
GeneratedHeader bool
Expand All @@ -48,31 +49,40 @@ func Render(cfg Options) error {
t := template.New("").Funcs(funcs)

var roots []string
// load all the templates in the directory
err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
if cfg.Template != "" {
var err error
t, err = t.New("template.gotpl").Parse(cfg.Template)
if err != nil {
return err
}
name := filepath.ToSlash(strings.TrimPrefix(path, rootDir+string(os.PathSeparator)))
if !strings.HasSuffix(info.Name(), ".gotpl") {
return nil
}
b, err := ioutil.ReadFile(path)
if err != nil {
return err
return errors.Wrap(err, "error with provided template")
}
roots = append(roots, "template.gotpl")
} else {
// load all the templates in the directory
err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
name := filepath.ToSlash(strings.TrimPrefix(path, rootDir+string(os.PathSeparator)))
if !strings.HasSuffix(info.Name(), ".gotpl") {
return nil
}
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}

t, err = t.New(name).Parse(string(b))
if err != nil {
return errors.Wrap(err, cfg.Filename)
}
t, err = t.New(name).Parse(string(b))
if err != nil {
return errors.Wrap(err, cfg.Filename)
}

roots = append(roots, name)
roots = append(roots, name)

return nil
})
if err != nil {
return errors.Wrap(err, "locating templates")
return nil
})
if err != nil {
return errors.Wrap(err, "locating templates")
}
}

// then execute all the important looking ones in order, adding them to the same file
Expand All @@ -91,7 +101,7 @@ func Render(cfg Options) error {
if cfg.RegionTags {
buf.WriteString("\n// region " + center(70, "*", " "+root+" ") + "\n")
}
err = t.Lookup(root).Execute(&buf, cfg.Data)
err := t.Lookup(root).Execute(&buf, cfg.Data)
if err != nil {
return errors.Wrap(err, root)
}
Expand All @@ -110,7 +120,7 @@ func Render(cfg Options) error {
result.WriteString("import (\n")
result.WriteString(CurrentImports.String())
result.WriteString(")\n")
_, err = buf.WriteTo(&result)
_, err := buf.WriteTo(&result)
if err != nil {
return err
}
Expand Down

0 comments on commit a0ee717

Please sign in to comment.