|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +// config-fields emits source-backed middleware config fields for the website. |
| 4 | +// It does not claim to determine runtime defaults or behavior. |
| 5 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "encoding/json" |
| 10 | + "flag" |
| 11 | + "fmt" |
| 12 | + "go/ast" |
| 13 | + "go/format" |
| 14 | + "go/parser" |
| 15 | + "go/token" |
| 16 | + "os" |
| 17 | + "path/filepath" |
| 18 | + "sort" |
| 19 | + "strings" |
| 20 | +) |
| 21 | + |
| 22 | +type field struct { |
| 23 | + Name string `json:"name"` |
| 24 | + Type string `json:"type"` |
| 25 | + Deprecated bool `json:"deprecated,omitempty"` |
| 26 | + Line int `json:"line"` |
| 27 | +} |
| 28 | + |
| 29 | +type config struct { |
| 30 | + Name string `json:"name"` |
| 31 | + File string `json:"file"` |
| 32 | + Line int `json:"line"` |
| 33 | + Fields []field `json:"fields"` |
| 34 | +} |
| 35 | + |
| 36 | +type manifest struct { |
| 37 | + Module string `json:"module"` |
| 38 | + Revision string `json:"revision,omitempty"` |
| 39 | + Configs []config `json:"configs"` |
| 40 | +} |
| 41 | + |
| 42 | +func main() { |
| 43 | + root := flag.String("root", "../..", "Echo repository root, relative to the current directory") |
| 44 | + revision := flag.String("revision", "", "exact Echo commit or tag used for this build") |
| 45 | + flag.Parse() |
| 46 | + |
| 47 | + result, err := extract(*root, *revision) |
| 48 | + if err != nil { |
| 49 | + fmt.Fprintln(os.Stderr, err) |
| 50 | + os.Exit(1) |
| 51 | + } |
| 52 | + encoder := json.NewEncoder(os.Stdout) |
| 53 | + encoder.SetIndent("", " ") |
| 54 | + if err := encoder.Encode(result); err != nil { |
| 55 | + fmt.Fprintln(os.Stderr, err) |
| 56 | + os.Exit(1) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func extract(root, revision string) (manifest, error) { |
| 61 | + root, err := filepath.Abs(root) |
| 62 | + if err != nil { |
| 63 | + return manifest{}, err |
| 64 | + } |
| 65 | + fs := token.NewFileSet() |
| 66 | + packageDir := filepath.Join(root, "middleware") |
| 67 | + packages, err := parser.ParseDir(fs, packageDir, func(info os.FileInfo) bool { |
| 68 | + return strings.HasSuffix(info.Name(), ".go") && !strings.HasSuffix(info.Name(), "_test.go") |
| 69 | + }, parser.ParseComments) |
| 70 | + if err != nil { |
| 71 | + return manifest{}, err |
| 72 | + } |
| 73 | + pkg, ok := packages["middleware"] |
| 74 | + if !ok { |
| 75 | + return manifest{}, fmt.Errorf("middleware package not found in %s", packageDir) |
| 76 | + } |
| 77 | + |
| 78 | + result := manifest{Module: "github.com/labstack/echo/v5", Revision: revision, Configs: []config{}} |
| 79 | + for filename, source := range pkg.Files { |
| 80 | + for _, declaration := range source.Decls { |
| 81 | + group, ok := declaration.(*ast.GenDecl) |
| 82 | + if !ok || group.Tok != token.TYPE { |
| 83 | + continue |
| 84 | + } |
| 85 | + for _, item := range group.Specs { |
| 86 | + typeSpec := item.(*ast.TypeSpec) |
| 87 | + structure, ok := typeSpec.Type.(*ast.StructType) |
| 88 | + if !ok || !ast.IsExported(typeSpec.Name.Name) || !strings.HasSuffix(typeSpec.Name.Name, "Config") { |
| 89 | + continue |
| 90 | + } |
| 91 | + entry := config{ |
| 92 | + Name: typeSpec.Name.Name, |
| 93 | + File: filepath.ToSlash(strings.TrimPrefix(filename, root+string(filepath.Separator))), |
| 94 | + Line: fs.Position(typeSpec.Pos()).Line, |
| 95 | + Fields: []field{}, |
| 96 | + } |
| 97 | + for _, sourceField := range structure.Fields.List { |
| 98 | + var rendered bytes.Buffer |
| 99 | + if err := format.Node(&rendered, fs, sourceField.Type); err != nil { |
| 100 | + return manifest{}, err |
| 101 | + } |
| 102 | + fieldDoc := comment(sourceField.Doc) |
| 103 | + for _, name := range sourceField.Names { |
| 104 | + if !ast.IsExported(name.Name) { |
| 105 | + continue |
| 106 | + } |
| 107 | + entry.Fields = append(entry.Fields, field{ |
| 108 | + Name: name.Name, |
| 109 | + Type: rendered.String(), |
| 110 | + Deprecated: strings.Contains(fieldDoc, "Deprecated:"), |
| 111 | + Line: fs.Position(name.Pos()).Line, |
| 112 | + }) |
| 113 | + } |
| 114 | + } |
| 115 | + result.Configs = append(result.Configs, entry) |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + sort.Slice(result.Configs, func(i, j int) bool { return result.Configs[i].Name < result.Configs[j].Name }) |
| 120 | + return result, nil |
| 121 | +} |
| 122 | + |
| 123 | +func comment(group *ast.CommentGroup) string { |
| 124 | + if group == nil { |
| 125 | + return "" |
| 126 | + } |
| 127 | + return strings.TrimSpace(group.Text()) |
| 128 | +} |
0 commit comments