2021-05-11 14:14:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/build"
|
|
|
|
"go/types"
|
|
|
|
"log"
|
2021-09-28 13:30:14 +00:00
|
|
|
"os"
|
2021-05-11 14:14:10 +00:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-02-03 14:24:05 +00:00
|
|
|
const rootPkg = "github.com/traefik/traefik/v3/pkg/config/dynamic"
|
2021-05-11 14:14:10 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
destModuleName = "github.com/traefik/genconf"
|
|
|
|
destPkg = "dynamic"
|
|
|
|
)
|
|
|
|
|
|
|
|
const marsh = `package %s
|
|
|
|
|
|
|
|
import "encoding/json"
|
|
|
|
|
|
|
|
type JSONPayload struct {
|
|
|
|
*Configuration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c JSONPayload) MarshalJSON() ([]byte, error) {
|
|
|
|
if c.Configuration == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(c.Configuration)
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
// main generate Go Structures from Go structures.
|
|
|
|
// Allows to create an external module (destModuleName) used by the plugin's providers
|
|
|
|
// that contains Go structs of the dynamic configuration and nothing else.
|
|
|
|
// These Go structs do not have any non-exported fields and do not rely on any external dependencies.
|
|
|
|
func main() {
|
|
|
|
dest := filepath.Join(path.Join(build.Default.GOPATH, "src"), destModuleName, destPkg)
|
|
|
|
|
|
|
|
log.Println("Output:", dest)
|
|
|
|
|
|
|
|
err := run(dest)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(dest string) error {
|
|
|
|
centrifuge, err := NewCentrifuge(rootPkg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
centrifuge.IncludedImports = []string{
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/tls",
|
|
|
|
"github.com/traefik/traefik/v3/pkg/types",
|
2021-05-11 14:14:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
centrifuge.ExcludedTypes = []string{
|
|
|
|
// tls
|
|
|
|
"CertificateStore", "Manager",
|
|
|
|
// dynamic
|
|
|
|
"Message", "Configurations",
|
|
|
|
// types
|
|
|
|
"HTTPCodeRanges", "HostResolverConfig",
|
|
|
|
}
|
|
|
|
|
|
|
|
centrifuge.ExcludedFiles = []string{
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/types/logs.go",
|
|
|
|
"github.com/traefik/traefik/v3/pkg/types/metrics.go",
|
2021-05-11 14:14:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
centrifuge.TypeCleaner = cleanType
|
|
|
|
centrifuge.PackageCleaner = cleanPackage
|
|
|
|
|
|
|
|
err = centrifuge.Run(dest, destPkg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-28 13:30:14 +00:00
|
|
|
return os.WriteFile(filepath.Join(dest, "marshaler.go"), []byte(fmt.Sprintf(marsh, destPkg)), 0o666)
|
2021-05-11 14:14:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func cleanType(typ types.Type, base string) string {
|
2023-02-03 14:24:05 +00:00
|
|
|
if typ.String() == "github.com/traefik/traefik/v3/pkg/tls.FileOrContent" {
|
2021-05-11 14:14:10 +00:00
|
|
|
return "string"
|
|
|
|
}
|
|
|
|
|
2023-02-03 14:24:05 +00:00
|
|
|
if typ.String() == "[]github.com/traefik/traefik/v3/pkg/tls.FileOrContent" {
|
2021-05-11 14:14:10 +00:00
|
|
|
return "[]string"
|
|
|
|
}
|
|
|
|
|
|
|
|
if typ.String() == "github.com/traefik/paerser/types.Duration" {
|
|
|
|
return "string"
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(typ.String(), base) {
|
|
|
|
return strings.ReplaceAll(typ.String(), base+".", "")
|
|
|
|
}
|
|
|
|
|
2023-02-03 14:24:05 +00:00
|
|
|
if strings.Contains(typ.String(), "github.com/traefik/traefik/v3/pkg/") {
|
|
|
|
return strings.ReplaceAll(typ.String(), "github.com/traefik/traefik/v3/pkg/", "")
|
2021-05-11 14:14:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return typ.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanPackage(src string) string {
|
|
|
|
switch src {
|
|
|
|
case "github.com/traefik/paerser/types":
|
|
|
|
return ""
|
2023-02-03 14:24:05 +00:00
|
|
|
case "github.com/traefik/traefik/v3/pkg/tls":
|
2021-05-11 14:14:10 +00:00
|
|
|
return path.Join(destModuleName, destPkg, "tls")
|
2023-02-03 14:24:05 +00:00
|
|
|
case "github.com/traefik/traefik/v3/pkg/types":
|
2021-05-11 14:14:10 +00:00
|
|
|
return path.Join(destModuleName, destPkg, "types")
|
|
|
|
default:
|
|
|
|
return src
|
|
|
|
}
|
|
|
|
}
|