1
0
mirror of https://github.com/go-acme/lego.git synced 2024-12-23 09:15:11 +02:00
lego/internal/dns/docs/generator.go

210 lines
3.9 KiB
Go
Raw Normal View History

package main
//go:generate go run .
import (
"bufio"
"bytes"
"embed"
"errors"
"fmt"
"go/format"
2024-10-26 03:49:06 +02:00
html "html/template"
"log"
"os"
"path/filepath"
2024-10-26 03:49:06 +02:00
"slices"
"strings"
"text/template"
"github.com/go-acme/lego/v4/internal/dns/descriptors"
)
//go:embed templates
var templateFS embed.FS
const (
root = "../../../"
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 (
startLine = "<!-- START DNS PROVIDERS LIST -->"
endLine = "<!-- END DNS PROVIDERS LIST -->"
)
func main() {
models, err := descriptors.GetProviderInformation(root)
if err != nil {
log.Fatal(err)
}
for _, m := range models.Providers {
// generate documentation
err = generateDocumentation(m)
if err != nil {
log.Fatal(err)
}
}
// generate CLI help
err = generateCLIHelp(models)
if err != nil {
log.Fatal(err)
}
// generate README.md
err = generateReadMe(models)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Documentation for %d DNS providers has been generated.\n", len(models.Providers)+1)
}
func generateDocumentation(m descriptors.Provider) error {
filename := filepath.Join(docOutput, "zz_gen_"+m.Code+".md")
file, err := os.Create(filename)
if err != nil {
return err
}
defer func() { _ = file.Close() }()
return template.Must(template.ParseFS(templateFS, mdTemplate)).Execute(file, m)
}
func generateCLIHelp(models *descriptors.Providers) error {
2021-11-03 21:39:12 +02:00
filename := filepath.Clean(cliOutput)
file, err := os.Create(filename)
if err != nil {
return err
}
defer func() { _ = file.Close() }()
b := &bytes.Buffer{}
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
}
// gofmt
source, err := format.Source(b.Bytes())
if err != nil {
return err
}
_, err = file.Write(source)
return err
}
func generateReadMe(models *descriptors.Providers) error {
tpl := html.Must(html.New(filepath.Base(readmeTemplate)).ParseFS(templateFS, readmeTemplate))
2024-10-26 03:49:06 +02:00
providers := orderProviders(models)
file, err := os.Open(readmePath)
if err != nil {
return err
}
defer func() { _ = file.Close() }()
var skip bool
buffer := bytes.NewBufferString("")
fileScanner := bufio.NewScanner(file)
for fileScanner.Scan() {
text := fileScanner.Text()
if text == startLine {
_, _ = fmt.Fprintln(buffer, text)
2024-10-26 03:49:06 +02:00
if err = tpl.Execute(buffer, providers); err != nil {
return err
}
skip = true
}
if text == endLine {
skip = false
}
if skip {
continue
}
_, _ = fmt.Fprintln(buffer, text)
}
if fileScanner.Err() != nil {
return fileScanner.Err()
}
if skip {
return errors.New("missing end tag")
}
2021-08-25 11:44:11 +02:00
return os.WriteFile(readmePath, buffer.Bytes(), 0o666)
}
2024-10-26 03:49:06 +02:00
func orderProviders(models *descriptors.Providers) [][]descriptors.Provider {
providers := append(models.Providers, descriptors.Provider{
Name: "Manual",
Code: "manual",
})
const nbCol = 4
2024-10-26 03:49:06 +02:00
slices.SortFunc(providers, func(a, b descriptors.Provider) int {
return strings.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name))
})
2024-10-26 03:49:06 +02:00
var matrix [][]descriptors.Provider
var row []descriptors.Provider
2024-10-26 03:49:06 +02:00
for i, p := range providers {
switch {
2024-10-26 03:49:06 +02:00
case len(row) == nbCol:
matrix = append(matrix, row)
row = []descriptors.Provider{p}
case i == len(providers)-1:
row = append(row, p)
for j := len(row); j < nbCol; j++ {
row = append(row, descriptors.Provider{})
}
2024-10-26 03:49:06 +02:00
matrix = append(matrix, row)
default:
2024-10-26 03:49:06 +02:00
row = append(row, p)
}
}
2024-10-26 03:49:06 +02:00
if len(row) < nbCol {
for j := len(row); j < nbCol; j++ {
row = append(row, descriptors.Provider{})
}
2024-10-26 03:49:06 +02:00
matrix = append(matrix, row)
}
2024-10-26 03:49:06 +02:00
return matrix
}