Refactor more things into class

This commit is contained in:
2024-09-12 15:50:02 +02:00
parent a5d6fa15a3
commit 3994b5aa74
2 changed files with 21 additions and 17 deletions

View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
_ "embed"
"fmt" "fmt"
"log" "log"
"os" "os"
@@ -12,6 +13,25 @@ import (
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
) )
//go:embed class.tmpl
var templatestr string
var classTemplate *template.Template
var fns = template.FuncMap{
"plus1": func(x int) int {
return x + 1
},
}
func init() {
var err error
classTemplate, err = template.New("class").Funcs(fns).Parse(templatestr)
if err != nil {
Error.Printf("Error parsing template: %v", err)
return
}
}
type ( type (
Class struct { Class struct {
ClassName string ClassName string

18
main.go
View File

@@ -7,16 +7,12 @@ import (
"log" "log"
"os" "os"
"sync" "sync"
"text/template"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
_ "embed" _ "embed"
) )
//go:embed class.tmpl
var templatestr string
var Error *log.Logger var Error *log.Logger
var Warning *log.Logger var Warning *log.Logger
@@ -36,12 +32,6 @@ func init() {
spew.Config.SortKeys = true spew.Config.SortKeys = true
} }
var fns = template.FuncMap{
"plus1": func(x int) int {
return x + 1
},
}
func main() { func main() {
outdir := flag.String("o", ".", "Output directory") outdir := flag.String("o", ".", "Output directory")
flag.Parse() flag.Parse()
@@ -52,12 +42,6 @@ func main() {
return return
} }
ltemplate, err := template.New("class").Funcs(fns).Parse(templatestr)
if err != nil {
Error.Printf("Error parsing template: %v", err)
return
}
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
for _, file := range files { for _, file := range files {
wg.Add(1) wg.Add(1)
@@ -68,7 +52,7 @@ func main() {
Error.Printf("Error parsing file: %v", err) Error.Printf("Error parsing file: %v", err)
return return
} }
class.Write(*outdir, ltemplate) class.Write(*outdir, classTemplate)
}(file) }(file)
} }
wg.Wait() wg.Wait()