1
0
mirror of https://github.com/go-acme/lego.git synced 2025-01-03 07:19:39 +02:00

chore: embed templates for internal commands (#2314)

This commit is contained in:
Ludovic Fernandez 2024-10-28 14:14:35 +01:00 committed by GitHub
parent e3ec3c2c76
commit d0708fc64e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 57 additions and 47 deletions

View File

@ -5,6 +5,7 @@ package main
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"embed"
"errors" "errors"
"fmt" "fmt"
"go/format" "go/format"
@ -19,15 +20,21 @@ import (
"github.com/go-acme/lego/v4/internal/dns/descriptors" "github.com/go-acme/lego/v4/internal/dns/descriptors"
) )
//go:embed templates
var templateFS embed.FS
const ( const (
root = "../../../" root = "../../../"
mdTemplate = root + "internal/dns/docs/dns.md.tmpl" cliOutput = root + "cmd/zz_gen_cmd_dnshelp.go"
cliTemplate = root + "internal/dns/docs/dns.go.tmpl" docOutput = root + "docs/content/dns"
cliOutput = root + "cmd/zz_gen_cmd_dnshelp.go" readmePath = root + "README.md"
docOutput = root + "docs/content/dns" )
readmeTemplate = root + "internal/dns/docs/readme.md.tmpl"
readmePath = root + "README.md" const (
mdTemplate = "templates/dns.md.tmpl"
cliTemplate = "templates/dns.go.tmpl"
readmeTemplate = "templates/readme.md.tmpl"
) )
const ( const (
@ -74,7 +81,7 @@ func generateDocumentation(m descriptors.Provider) error {
defer func() { _ = file.Close() }() 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 { func generateCLIHelp(models *descriptors.Providers) error {
@ -87,14 +94,14 @@ func generateCLIHelp(models *descriptors.Providers) error {
defer func() { _ = file.Close() }() 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{} 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 { if err != nil {
return err return err
} }
@ -110,7 +117,7 @@ func generateCLIHelp(models *descriptors.Providers) error {
} }
func generateReadMe(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) providers := orderProviders(models)
file, err := os.Open(readmePath) file, err := os.Open(readmePath)

View File

@ -4,6 +4,7 @@ package main
import ( import (
"bytes" "bytes"
_ "embed"
"fmt" "fmt"
"go/format" "go/format"
"log" "log"
@ -18,10 +19,12 @@ import (
const ( const (
root = "../../../" 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() { func main() {
err := generate() err := generate()
if err != nil { if err != nil {
@ -42,16 +45,14 @@ func generate() error {
defer func() { _ = file.Close() }() 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{} 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 { if err != nil {
return err return err
} }

View File

@ -2,26 +2,31 @@ package main
import ( import (
"bytes" "bytes"
"embed"
"fmt" "fmt"
"go/ast" "go/ast"
"go/format" "go/format"
"go/parser" "go/parser"
"go/token" "go/token"
"os" "os"
"path/filepath"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"text/template" "text/template"
) )
//go:embed templates
var templateFS embed.FS
type Generator struct { type Generator struct {
baseUserAgent string baseUserAgent string
template string templatePath string
sourcePath string sourcePath string
} }
func NewGenerator(baseUserAgent string, tmpl string, sourcePath string) *Generator { func NewGenerator(baseUserAgent string, templatePath string, sourcePath string) *Generator {
return &Generator{baseUserAgent: baseUserAgent, template: tmpl, sourcePath: sourcePath} return &Generator{baseUserAgent: baseUserAgent, templatePath: templatePath, sourcePath: sourcePath}
} }
func (g *Generator) Release(mode string) error { 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 { 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 { if err != nil {
return err return err
} }

View File

@ -8,6 +8,18 @@ import (
"github.com/urfave/cli/v2" "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() { func main() {
app := cli.NewApp() app := cli.NewApp()
app.Name = "lego-releaser" app.Name = "lego-releaser"

View File

@ -1,10 +1,4 @@
package main // Code generated by 'internal/useragent'; DO NOT EDIT.
const dnsBaseUserAgent = "goacme-lego/"
const dnsSourceFile = "./providers/dns/internal/useragent/useragent.go"
const dnsTemplate = `// Code generated by 'internal/useragent'; DO NOT EDIT.
package useragent package useragent
@ -33,4 +27,3 @@ func Get() string {
func SetHeader(h http.Header) { func SetHeader(h http.Header) {
h.Set("User-Agent", Get()) h.Set("User-Agent", Get())
} }
`

View File

@ -1,10 +1,4 @@
package main // Code generated by 'internal/useragent'; DO NOT EDIT.
const senderBaseUserAgent = "xenolf-acme/"
const senderSourceFile = "./acme/api/internal/sender/useragent.go"
const senderTemplate = `// Code generated by 'internal/useragent'; DO NOT EDIT.
package sender package sender
@ -17,5 +11,3 @@ const (
// NOTE: Update this with each tagged release. // NOTE: Update this with each tagged release.
ourUserAgentComment = "{{ .comment }}" ourUserAgentComment = "{{ .comment }}"
) )
`