From a0826cd68dd352c5c5ef5782dd6c660f5a93c53e Mon Sep 17 00:00:00 2001 From: Hazel Noack Date: Mon, 30 Jun 2025 15:44:59 +0200 Subject: [PATCH] draft --- internal/gen.go | 68 +++++++++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/internal/gen.go b/internal/gen.go index 50e3bb7..66c4251 100644 --- a/internal/gen.go +++ b/internal/gen.go @@ -6,33 +6,59 @@ import ( "fmt" "log" "os" + "strings" "text/template" "time" ) -const Path = "/usr/shared/dictionary/words" +const Directory = "/usr/shared/dictionary/words" -func main() { - fmt.Println("Generate") +var wordsTemplate = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT. +// 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) defer f.Close() - packageTemplate.Execute(f, struct { - Timestamp time.Time - Path string - Words []string - }{ + wordsTemplate.Execute(f, templateData{ Timestamp: time.Now(), Path: Path, - Words: []string{ - "foo", - "bar", - "baz", - }, + Words: words, + VarName: strings.Title(name), }) +} +func main() { + fmt.Println("Generate") + generateFile("words") } 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. -// This file was generated by robots at -// {{ .Timestamp }} -// using data from -// {{ .Path }} -package dictionary - -var Words = []string{ -{{- range .Words }} - {{ printf "%q" . }}, -{{- end }} -} -`)) +var packageTemplate = template.Must(template.New("").Parse())