Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: embed templates for internal commands #2314

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions internal/dns/docs/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main
import (
"bufio"
"bytes"
"embed"
"errors"
"fmt"
"go/format"
Expand All @@ -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 (
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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)
Expand Down
File renamed without changes.
File renamed without changes.
23 changes: 12 additions & 11 deletions internal/dns/providers/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package main

import (
"bytes"
_ "embed"
"fmt"
"go/format"
"log"
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down
13 changes: 9 additions & 4 deletions internal/useragent/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 12 additions & 0 deletions internal/useragent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -33,4 +27,3 @@ func Get() string {
func SetHeader(h http.Header) {
h.Set("User-Agent", Get())
}
`
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -17,5 +11,3 @@ const (
// NOTE: Update this with each tagged release.
ourUserAgentComment = "{{ .comment }}"
)

`