From d0708fc64e36ecb679f55a6ce982db1e261aad94 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Mon, 28 Oct 2024 14:14:35 +0100 Subject: [PATCH] chore: embed templates for internal commands (#2314) --- internal/dns/docs/generator.go | 37 +++++++++++-------- internal/dns/docs/{ => templates}/dns.go.tmpl | 0 internal/dns/docs/{ => templates}/dns.md.tmpl | 0 .../dns/docs/{ => templates}/readme.md.tmpl | 0 internal/dns/providers/generator.go | 23 ++++++------ internal/useragent/generator.go | 13 +++++-- internal/useragent/main.go | 12 ++++++ .../{data_dns.go => templates/dns.go.tmpl} | 9 +---- .../sender.go.tmpl} | 10 +---- 9 files changed, 57 insertions(+), 47 deletions(-) rename internal/dns/docs/{ => templates}/dns.go.tmpl (100%) rename internal/dns/docs/{ => templates}/dns.md.tmpl (100%) rename internal/dns/docs/{ => templates}/readme.md.tmpl (100%) rename internal/useragent/{data_dns.go => templates/dns.go.tmpl} (77%) rename internal/useragent/{data_sender.go => templates/sender.go.tmpl} (64%) diff --git a/internal/dns/docs/generator.go b/internal/dns/docs/generator.go index 3ffdcc92c..a6b91b45d 100644 --- a/internal/dns/docs/generator.go +++ b/internal/dns/docs/generator.go @@ -5,6 +5,7 @@ package main import ( "bufio" "bytes" + "embed" "errors" "fmt" "go/format" @@ -19,15 +20,21 @@ import ( "github.com/go-acme/lego/v4/internal/dns/descriptors" ) +//go:embed templates +var templateFS embed.FS + const ( root = "../../../" - mdTemplate = root + "internal/dns/docs/dns.md.tmpl" - cliTemplate = root + "internal/dns/docs/dns.go.tmpl" - cliOutput = root + "cmd/zz_gen_cmd_dnshelp.go" - docOutput = root + "docs/content/dns" - readmeTemplate = root + "internal/dns/docs/readme.md.tmpl" - readmePath = root + "README.md" + cliOutput = root + "cmd/zz_gen_cmd_dnshelp.go" + docOutput = root + "docs/content/dns" + readmePath = root + "README.md" +) + +const ( + mdTemplate = "templates/dns.md.tmpl" + cliTemplate = "templates/dns.go.tmpl" + readmeTemplate = "templates/readme.md.tmpl" ) const ( @@ -74,7 +81,7 @@ func generateDocumentation(m descriptors.Provider) error { defer func() { _ = file.Close() }() - return template.Must(template.ParseFiles(mdTemplate)).Execute(file, m) + return template.Must(template.ParseFS(templateFS, mdTemplate)).Execute(file, m) } func generateCLIHelp(models *descriptors.Providers) error { @@ -87,14 +94,14 @@ func generateCLIHelp(models *descriptors.Providers) error { defer func() { _ = file.Close() }() - tlt := template.New(filepath.Base(cliTemplate)).Funcs(map[string]interface{}{ - "safe": func(src string) string { - return strings.ReplaceAll(src, "`", "'") - }, - }) - b := &bytes.Buffer{} - err = template.Must(tlt.ParseFiles(cliTemplate)).Execute(b, models) + err = template.Must( + template.New(filepath.Base(cliTemplate)).Funcs(map[string]interface{}{ + "safe": func(src string) string { + return strings.ReplaceAll(src, "`", "'") + }, + }).ParseFS(templateFS, cliTemplate), + ).Execute(b, models) if err != nil { return err } @@ -110,7 +117,7 @@ func generateCLIHelp(models *descriptors.Providers) error { } func generateReadMe(models *descriptors.Providers) error { - tpl := html.Must(html.New(filepath.Base(readmeTemplate)).ParseFiles(readmeTemplate)) + tpl := html.Must(html.New(filepath.Base(readmeTemplate)).ParseFS(templateFS, readmeTemplate)) providers := orderProviders(models) file, err := os.Open(readmePath) diff --git a/internal/dns/docs/dns.go.tmpl b/internal/dns/docs/templates/dns.go.tmpl similarity index 100% rename from internal/dns/docs/dns.go.tmpl rename to internal/dns/docs/templates/dns.go.tmpl diff --git a/internal/dns/docs/dns.md.tmpl b/internal/dns/docs/templates/dns.md.tmpl similarity index 100% rename from internal/dns/docs/dns.md.tmpl rename to internal/dns/docs/templates/dns.md.tmpl diff --git a/internal/dns/docs/readme.md.tmpl b/internal/dns/docs/templates/readme.md.tmpl similarity index 100% rename from internal/dns/docs/readme.md.tmpl rename to internal/dns/docs/templates/readme.md.tmpl diff --git a/internal/dns/providers/generator.go b/internal/dns/providers/generator.go index 98749db47..bab31072d 100644 --- a/internal/dns/providers/generator.go +++ b/internal/dns/providers/generator.go @@ -4,6 +4,7 @@ package main import ( "bytes" + _ "embed" "fmt" "go/format" "log" @@ -18,10 +19,12 @@ import ( const ( root = "../../../" - srcTemplate = "internal/dns/providers/dns_providers.go.tmpl" - outputPath = "providers/dns/zz_gen_dns_providers.go" + outputPath = "providers/dns/zz_gen_dns_providers.go" ) +//go:embed dns_providers.go.tmpl +var srcTemplate string + func main() { err := generate() if err != nil { @@ -42,16 +45,14 @@ func generate() error { defer func() { _ = file.Close() }() - tmplFile := filepath.Join(root, srcTemplate) - - tlt := template.New(filepath.Base(tmplFile)).Funcs(map[string]interface{}{ - "cleanName": func(src string) string { - return strings.ReplaceAll(src, "-", "") - }, - }) - b := &bytes.Buffer{} - err = template.Must(tlt.ParseFiles(tmplFile)).Execute(b, info) + err = template.Must( + template.New("").Funcs(map[string]interface{}{ + "cleanName": func(src string) string { + return strings.ReplaceAll(src, "-", "") + }, + }).Parse(srcTemplate), + ).Execute(b, info) if err != nil { return err } diff --git a/internal/useragent/generator.go b/internal/useragent/generator.go index bfa2e406b..bd41d420a 100644 --- a/internal/useragent/generator.go +++ b/internal/useragent/generator.go @@ -2,26 +2,31 @@ package main import ( "bytes" + "embed" "fmt" "go/ast" "go/format" "go/parser" "go/token" "os" + "path/filepath" "regexp" "strconv" "strings" "text/template" ) +//go:embed templates +var templateFS embed.FS + type Generator struct { baseUserAgent string - template string + templatePath string sourcePath string } -func NewGenerator(baseUserAgent string, tmpl string, sourcePath string) *Generator { - return &Generator{baseUserAgent: baseUserAgent, template: tmpl, sourcePath: sourcePath} +func NewGenerator(baseUserAgent string, templatePath string, sourcePath string) *Generator { + return &Generator{baseUserAgent: baseUserAgent, templatePath: templatePath, sourcePath: sourcePath} } func (g *Generator) Release(mode string) error { @@ -58,7 +63,7 @@ func (g *Generator) Detach() error { } func (g *Generator) writeUserAgentFile(filename, version, comment string) error { - tmpl, err := template.New("ua").Parse(g.template) + tmpl, err := template.New(filepath.Base(g.templatePath)).ParseFS(templateFS, g.templatePath) if err != nil { return err } diff --git a/internal/useragent/main.go b/internal/useragent/main.go index 9add82aa4..c4de3fdaa 100644 --- a/internal/useragent/main.go +++ b/internal/useragent/main.go @@ -8,6 +8,18 @@ import ( "github.com/urfave/cli/v2" ) +const ( + dnsBaseUserAgent = "goacme-lego/" + dnsSourceFile = "./providers/dns/internal/useragent/useragent.go" + dnsTemplate = "templates/dns.go.tmpl" +) + +const ( + senderBaseUserAgent = "xenolf-acme/" + senderSourceFile = "./acme/api/internal/sender/useragent.go" + senderTemplate = "templates/sender.go.tmpl" +) + func main() { app := cli.NewApp() app.Name = "lego-releaser" diff --git a/internal/useragent/data_dns.go b/internal/useragent/templates/dns.go.tmpl similarity index 77% rename from internal/useragent/data_dns.go rename to internal/useragent/templates/dns.go.tmpl index 2ce8a3223..3419b045e 100644 --- a/internal/useragent/data_dns.go +++ b/internal/useragent/templates/dns.go.tmpl @@ -1,10 +1,4 @@ -package main - -const dnsBaseUserAgent = "goacme-lego/" - -const dnsSourceFile = "./providers/dns/internal/useragent/useragent.go" - -const dnsTemplate = `// Code generated by 'internal/useragent'; DO NOT EDIT. +// Code generated by 'internal/useragent'; DO NOT EDIT. package useragent @@ -33,4 +27,3 @@ func Get() string { func SetHeader(h http.Header) { h.Set("User-Agent", Get()) } -` diff --git a/internal/useragent/data_sender.go b/internal/useragent/templates/sender.go.tmpl similarity index 64% rename from internal/useragent/data_sender.go rename to internal/useragent/templates/sender.go.tmpl index 5da2d8538..5fcd58b87 100644 --- a/internal/useragent/data_sender.go +++ b/internal/useragent/templates/sender.go.tmpl @@ -1,10 +1,4 @@ -package main - -const senderBaseUserAgent = "xenolf-acme/" - -const senderSourceFile = "./acme/api/internal/sender/useragent.go" - -const senderTemplate = `// Code generated by 'internal/useragent'; DO NOT EDIT. +// Code generated by 'internal/useragent'; DO NOT EDIT. package sender @@ -17,5 +11,3 @@ const ( // NOTE: Update this with each tagged release. ourUserAgentComment = "{{ .comment }}" ) - -`