This commit is contained in:
Hazel Noack 2025-06-30 15:44:59 +02:00
parent 5acfd28ccc
commit a0826cd68d

View File

@ -6,33 +6,59 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"strings"
"text/template" "text/template"
"time" "time"
) )
const Path = "/usr/shared/dictionary/words" const Directory = "/usr/shared/dictionary/words"
func main() { var wordsTemplate = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT.
fmt.Println("Generate") // This file was generated by robots at
// {{ .Timestamp }}
// using data from
// {{ .Path }}
package dictionary
f, err := os.Create("internal/dictionary/words.go") var {{ .VarName }} = []string{
die(err) {{- range .Words }}
defer f.Close() {{ printf "%q" . }},
{{- end }}
}
`))
packageTemplate.Execute(f, struct { type templateData struct {
Timestamp time.Time Timestamp time.Time
Path string Path string
Words []string Words []string
}{ VarName string
Timestamp: time.Now(), }
Path: Path,
Words: []string{ func generateFile(name string) {
lowerName := strings.ToLower(name)
words := []string{
"foo", "foo",
"bar", "bar",
"baz", "baz",
}, }
}) Path := Directory + lowerName
f, err := os.Create("internal/dictionary/" + lowerName + ".go")
die(err)
defer f.Close()
wordsTemplate.Execute(f, templateData{
Timestamp: time.Now(),
Path: Path,
Words: words,
VarName: strings.Title(name),
})
}
func main() {
fmt.Println("Generate")
generateFile("words")
} }
func die(err error) { func die(err error) {
@ -41,16 +67,4 @@ func die(err error) {
} }
} }
var packageTemplate = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT. var packageTemplate = template.Must(template.New("").Parse())
// This file was generated by robots at
// {{ .Timestamp }}
// using data from
// {{ .Path }}
package dictionary
var Words = []string{
{{- range .Words }}
{{ printf "%q" . }},
{{- end }}
}
`))