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{
{{- range .Words }}
{{ printf "%q" . }},
{{- end }}
}
`))
type templateData struct {
Timestamp time.Time
Path string
Words []string
VarName string
}
func generateFile(name string) {
lowerName := strings.ToLower(name)
words := []string{
"foo",
"bar",
"baz",
}
Path := Directory + lowerName
f, err := os.Create("internal/dictionary/" + lowerName + ".go")
die(err) die(err)
defer f.Close() defer f.Close()
packageTemplate.Execute(f, struct { wordsTemplate.Execute(f, templateData{
Timestamp time.Time
Path string
Words []string
}{
Timestamp: time.Now(), Timestamp: time.Now(),
Path: Path, Path: Path,
Words: []string{ Words: words,
"foo", VarName: strings.Title(name),
"bar",
"baz",
},
}) })
}
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 }}
}
`))