-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
204 lines (189 loc) · 4.8 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright 2016 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"go/ast"
"go/printer"
"regexp"
"strings"
"golang.org/x/tools/go/packages"
"gopkg.in/yaml.v2"
)
var (
searchFlag = flag.String("search", "", "return handlers matching search regexp")
noSearchFlag = flag.String("no-search", "", "return handlers NOT matching search regexp")
methodFlag = flag.String("method", "", "return handlers with method")
noMethodFlag = flag.String("no-method", "", "return handlers EXCEPT with method")
)
func isListMode() bool {
return *searchFlag == "" && *methodFlag == "" && *noSearchFlag == "" && *noMethodFlag == ""
}
func apiLoader() ([]*packages.Package, error) {
cfg := &packages.Config{
Mode: packages.NeedFiles | packages.NeedSyntax | packages.NeedTypes | packages.NeedImports | packages.NeedDeps | packages.NeedCompiledGoFiles,
}
pkgsStr := []string{
"github.com/tsuru/tsuru/api",
}
pkgs, err := packages.Load(cfg, pkgsStr...)
if err != nil {
return nil, err
}
if packages.PrintErrors(pkgs) > 0 {
return nil, errors.New("errors reading pkgs")
}
return pkgs, err
}
func isHandler(object *ast.Object) bool {
params := object.Decl.(*ast.FuncDecl).Type.Params.List
if len(params) < 2 {
return false
}
paramType, ok := params[0].Type.(*ast.SelectorExpr)
if !ok || paramType.Sel.Name != "ResponseWriter" {
return false
}
if len(params) == 3 {
paramType, ok = params[2].Type.(*ast.SelectorExpr)
if !ok || paramType.Sel.Name != "Token" {
return false
}
}
return true
}
func shouldBeIgnored(objectName string) bool {
ignoreList := []string{
"writeEnvVars", "fullHealthcheck", "setVersionHeadersMiddleware",
"authTokenMiddleware", "runDelayedHandler", "errorHandlingMiddleware",
"contextClearerMiddleware", "flushingWriterMiddleware", "setRequestIDHeaderMiddleware",
"bsEnvSetHandler", "bsConfigGetHandler", "bsUpgradeHandler", "contentHijacker",
"deprecatedHandler",
}
for _, name := range ignoreList {
if name == objectName {
return true
}
}
return false
}
func parse(pkgs []*packages.Package) error {
if isListMode() {
fmt.Println("handlers:")
}
for _, pkg := range pkgs {
err := parsePkg(pkg)
if err != nil {
return err
}
}
return nil
}
func parsePkg(pkg *packages.Package) error {
for i, _ := range pkg.CompiledGoFiles {
fileAst := pkg.Syntax[i]
for _, object := range fileAst.Scope.Objects {
if object.Kind != ast.Fun {
continue
}
ok := isHandler(object)
if !ok {
continue
}
if shouldBeIgnored(object.Name) {
continue
}
commentGroup := object.Decl.(*ast.FuncDecl).Doc
if commentGroup == nil {
fmt.Printf("missing docs for %s\n", object.Name)
continue
}
err := handleComments(object, commentGroup, pkg)
if err != nil {
fmt.Printf("error handling comments for %s: %s\n", object.Name, err)
}
}
}
return nil
}
func handleComments(object *ast.Object, commentGroup *ast.CommentGroup, pkg *packages.Package) error {
if isListMode() {
for _, comment := range commentGroup.List {
if strings.Contains(comment.Text, "title:") {
fmt.Println(strings.Replace(comment.Text, "// ", " - ", -1))
} else {
fmt.Println(strings.Replace(comment.Text, "// ", " ", -1))
}
}
return nil
}
var buf bytes.Buffer
for _, comment := range commentGroup.List {
text := comment.Text
text = strings.Replace(text, "// ", "", -1)
text = strings.Replace(text, "//\t", "\t", -1)
text = strings.Replace(text, "//", "", -1)
text = strings.Replace(text, "\t", " ", -1)
buf.WriteString(text)
buf.WriteString("\n")
}
var parsed map[string]interface{}
err := yaml.Unmarshal(buf.Bytes(), &parsed)
if err != nil {
return err
}
method, ok := parsed["method"].(string)
if !ok {
return fmt.Errorf("invalid method declaration for %s: %v", object.Name, parsed["method"])
}
if *methodFlag != "" {
if strings.ToLower(*methodFlag) != strings.ToLower(method) {
return nil
}
}
if *noMethodFlag != "" {
if strings.ToLower(*noMethodFlag) == strings.ToLower(method) {
return nil
}
}
if *searchFlag != "" || *noSearchFlag != "" {
value := *searchFlag
negate := true
if value == "" {
value = *noSearchFlag
negate = false
}
var funcBuf bytes.Buffer
printer.Fprint(&funcBuf, pkg.Fset, object.Decl)
re, err := regexp.Compile(value)
if err != nil {
return err
}
isMatch := re.Match(funcBuf.Bytes())
if negate {
isMatch = !isMatch
}
if isMatch {
return nil
}
}
fmt.Printf("%s.%s\n", pkg.String(), object.Name)
return nil
}
func main() {
flag.Parse()
pkgs, err := apiLoader()
if err != nil {
fmt.Println("error loading code ", err)
return
}
err = parse(pkgs)
if err != nil {
fmt.Println("error parsing api ", err)
return
}
}