Move writing logic to class
This commit is contained in:
18
main.go
18
main.go
@@ -48,11 +48,11 @@ func main() {
|
||||
outdir := flag.String("o", ".", "Output directory")
|
||||
flag.Parse()
|
||||
files := flag.Args()
|
||||
// if len(files) == 0 {
|
||||
// Error.Printf("No files specified")
|
||||
// flag.Usage()
|
||||
// return
|
||||
// }
|
||||
if len(files) == 0 {
|
||||
Error.Printf("No files specified")
|
||||
flag.Usage()
|
||||
return
|
||||
}
|
||||
|
||||
ltemplate, err := template.New("class").Funcs(fns).Parse(templatestr)
|
||||
if err != nil {
|
||||
@@ -70,13 +70,7 @@ func main() {
|
||||
Error.Printf("Error parsing file: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
outfile, err := class.GetOutFile(*outdir)
|
||||
if err != nil {
|
||||
Error.Printf("Error creating output file: %v", err)
|
||||
return
|
||||
}
|
||||
ltemplate.Execute(outfile, class)
|
||||
class.Write(*outdir, ltemplate)
|
||||
}(file)
|
||||
}
|
||||
wg.Wait()
|
||||
|
13
types.go
13
types.go
@@ -5,6 +5,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -57,3 +58,15 @@ func (c *Class) GetOutFile(root string) (*os.File, error) {
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func (c *Class) Write(root string, tmpl *template.Template) error {
|
||||
outfile, err := c.GetOutFile(root)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating output file %v: %v", c.ClassName, err)
|
||||
}
|
||||
err = tmpl.Execute(outfile, c)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error writing output file %v: %v", c.ClassName, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user