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

75 lines
1.2 KiB
Go

package main
//go:generate go run .
import (
"bytes"
_ "embed"
"fmt"
"go/format"
"log"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/go-acme/lego/v4/internal/dns/descriptors"
)
const (
root = "../../../"
outputPath = "providers/dns/zz_gen_dns_providers.go"
)
//go:embed dns_providers.go.tmpl
var srcTemplate string
func main() {
err := generate()
if err != nil {
log.Fatal(err)
}
}
func generate() error {
info, err := descriptors.GetProviderInformation(root)
if err != nil {
return err
}
file, err := os.Create(filepath.Join(root, outputPath))
if err != nil {
return err
}
defer func() { _ = file.Close() }()
b := &bytes.Buffer{}
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
}
// gofmt
source, err := format.Source(b.Bytes())
if err != nil {
return err
}
_, err = file.Write(source)
if err != nil {
return err
}
fmt.Printf("Switch mapping for %d DNS providers has been generated.\n", len(info.Providers)+1)
return nil
}