Move writing logic to class

This commit is contained in:
2024-09-12 15:44:35 +02:00
parent b9306ce53d
commit 3577ecfaaf
2 changed files with 19 additions and 12 deletions

18
main.go
View File

@@ -48,11 +48,11 @@ func main() {
outdir := flag.String("o", ".", "Output directory") outdir := flag.String("o", ".", "Output directory")
flag.Parse() flag.Parse()
files := flag.Args() files := flag.Args()
// if len(files) == 0 { if len(files) == 0 {
// Error.Printf("No files specified") Error.Printf("No files specified")
// flag.Usage() flag.Usage()
// return return
// } }
ltemplate, err := template.New("class").Funcs(fns).Parse(templatestr) ltemplate, err := template.New("class").Funcs(fns).Parse(templatestr)
if err != nil { if err != nil {
@@ -70,13 +70,7 @@ func main() {
Error.Printf("Error parsing file: %v", err) Error.Printf("Error parsing file: %v", err)
return return
} }
class.Write(*outdir, ltemplate)
outfile, err := class.GetOutFile(*outdir)
if err != nil {
Error.Printf("Error creating output file: %v", err)
return
}
ltemplate.Execute(outfile, class)
}(file) }(file)
} }
wg.Wait() wg.Wait()

View File

@@ -5,6 +5,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"text/template"
) )
type ( type (
@@ -57,3 +58,15 @@ func (c *Class) GetOutFile(root string) (*os.File, error) {
} }
return f, nil 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
}