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:
parent
e3ec3c2c76
commit
d0708fc64e
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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"
|
||||
|
@ -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())
|
||||
}
|
||||
`
|
@ -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 }}"
|
||||
)
|
||||
|
||||
`
|
Loading…
Reference in New Issue
Block a user