-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
384 lines (342 loc) · 9.24 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//nolint:forbidigo // tool is supposed to send to stdout
package main
import (
"context"
"encoding/json"
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"os/signal"
"path"
"path/filepath"
"sort"
"strings"
"golang.org/x/sync/errgroup"
"golang.org/x/tools/go/packages"
)
// https://go.dev/ref/spec#Selectors
var exists = struct{}{}
const fsep = string(filepath.Separator)
const fromArg = "--from"
const excludeFromArg = "--exclude-from"
const toArg = "--to"
const excludeToArg = "--exclude-to"
type Report struct {
Exported []string
Imported []string
UnusedExports []string
}
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
// parse input
from := []string{}
excludeFrom := []string{}
to := []string{}
excludeTo := []string{}
addArg := func(arg string) {}
for _, a := range os.Args[1:] {
switch a {
case fromArg:
addArg = func(arg string) { from = append(from, expandPath(arg)) }
case excludeFromArg:
addArg = func(arg string) { excludeFrom = append(excludeFrom, expandPath(arg)) }
case toArg:
addArg = func(arg string) { to = append(to, expandPath(arg)) }
case excludeToArg:
addArg = func(arg string) { excludeTo = append(excludeTo, expandPath(arg)) }
default:
addArg(a)
}
}
// validate input
if len(from) == 0 && len(to) == 0 {
fmt.Println("Find potentially unused exports in go code. Works across repos. There will be false positives.")
fmt.Printf("Usage:\n\trefaudit %s [files] %s [files]\n", fromArg, toArg)
fmt.Printf("%s: Directories that contain exports.\n", fromArg)
fmt.Printf("%s: Directories that contain imports.\n", toArg)
fmt.Printf("%s: Directories that contain imports that you want to exclude. Optional.\n", excludeToArg)
fmt.Printf("%s: Directories that contain exports that you want to exclude. Optional.\n", excludeFromArg)
fmt.Println("Examples:")
fmt.Printf("\trefaudit %s /path/to/library/ %s /path/to/app1 /path/to/app2 | tee ~/unused1.json\n", fromArg, toArg)
fmt.Printf("\trefaudit %s /path/to/library/ %s /path/to/app1 %s /path/to/app1/exclude | tee ~/unused2.json\n", fromArg, toArg, excludeToArg)
os.Exit(1)
}
// print input so user knows what's going on
fmt.Fprintf(os.Stderr, "%s: %s\n", fromArg, strings.Join(from, ", "))
fmt.Fprintf(os.Stderr, "%s: %s\n", toArg, strings.Join(to, ", "))
fmt.Fprintf(os.Stderr, "%s: %s\n", excludeToArg, strings.Join(excludeTo, ", "))
fmt.Fprintf(os.Stderr, "%s: %s\n", excludeFromArg, strings.Join(excludeFrom, ", "))
globals, err := findExports(ctx, from, excludeFrom)
if err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(2)
}
refs, err := findImports(ctx, to, excludeTo)
if err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(2)
}
// print potentially unused globals
rpt := Report{
Exported: []string{},
Imported: []string{},
UnusedExports: []string{},
}
for k := range globals {
rpt.Exported = sortedInsert(rpt.Exported, k)
//rpt.Exported = append(rpt.Exported, k)
if _, ok := refs[k]; !ok {
rpt.UnusedExports = sortedInsert(rpt.UnusedExports, k)
}
}
for k := range refs {
rpt.Imported = sortedInsert(rpt.Imported, k)
}
outB, err := json.MarshalIndent(rpt, "", " ")
if err != nil {
fmt.Fprintf(os.Stderr, "failed to marshal output: %v", err)
os.Exit(2)
}
fmt.Println(string(outB))
}
// sortedInsert
func sortedInsert(list []string, elem string) []string {
// find spot to insert element
i := sort.Search(len(list), func(i int) bool { return list[i] >= elem })
// handle not found case
if i == len(list) {
return append(list, elem)
}
// shift over and set
list = append(list[:i+1], list[i:]...)
list[i] = elem
return list
}
func expandPath(path string) string {
exp, err := filepath.Abs(os.ExpandEnv(path))
if err != nil {
fmt.Fprintf(os.Stderr, "bad path input %s: %v", path, err)
os.Exit(1)
}
return exp
}
// runOnFiles runs fn on every file/dir specified, recursively.
func runOnFiles(ctx context.Context, files []string, excluding []string, fn func(file string) error) error {
g, ctx := errgroup.WithContext(ctx)
filesChan := make(chan string, 4) // buffered chan since walking can take a while
// set up file consumer
g.Go(func() error {
for {
select {
case <-ctx.Done():
return ctx.Err()
case f, ok := <-filesChan:
if ok {
if err := fn(f); err != nil {
return err
}
} else {
return nil
}
}
}
})
// walk the dir tree, producing files
g.Go(func() error {
defer close(filesChan)
vendor := fmt.Sprintf("%svendor%s", fsep, fsep)
for _, file := range files {
err := filepath.Walk(file,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if ctx.Err() != nil {
return ctx.Err()
}
// don't run on vendor sub-directories
if strings.Contains(path, vendor) {
return filepath.SkipDir
}
// exclude any top-level paths as needed
for _, ex := range excluding {
if strings.TrimSuffix(path, fsep) == strings.TrimSuffix(ex, fsep) {
return filepath.SkipDir
}
}
// don't run on dirs
if info.IsDir() {
return nil
}
// don't run on non-go files
if !strings.HasSuffix(path, ".go") {
return nil
}
// send to consumer
filesChan <- path
return nil
})
if err != nil {
return fmt.Errorf("could not walk %s: %w", file, err)
}
}
return nil
})
return g.Wait()
}
func findExports(ctx context.Context, from []string, excludeFrom []string) (map[string]interface{}, error) {
globals := make(map[string]interface{})
fs := token.NewFileSet()
err := runOnFiles(ctx, from, excludeFrom, func(file string) error {
f, err := parser.ParseFile(fs, file, nil, parser.AllErrors)
if err != nil {
return fmt.Errorf("could not parse %s: %w", file, err)
}
// find the public-facing full package path for the file
cfg := &packages.Config{Mode: packages.NeedName, Tests: false, Dir: path.Dir(file)}
pkgs, err := packages.Load(cfg, fmt.Sprintf("file=%s", file))
if err != nil {
return fmt.Errorf("could not parse package in %s: %w", file, err)
}
pkgPath := ""
for _, pkg := range pkgs {
if pkg.Name != "" {
pkgPath = pkg.PkgPath
}
}
if pkgPath == "" {
// probably a test
return nil
}
pkgPath = strings.Trim(pkgPath, "\"")
// scan the file for exports
v := newExportVisitor(f, globals, pkgPath)
ast.Walk(v, f)
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to find exports: %w", err)
}
return globals, nil
}
// exportVisitor tracks public exports.
type exportVisitor struct {
f *ast.File
pkgPath string
exports map[string]interface{}
}
func newExportVisitor(f *ast.File, exports map[string]interface{}, pkgPath string) exportVisitor {
return exportVisitor{f, pkgPath, exports}
}
func (v exportVisitor) Visit(n ast.Node) ast.Visitor {
if n == nil {
return nil
}
switch d := n.(type) {
case *ast.AssignStmt:
if d.Tok != token.DEFINE {
return v
}
for _, name := range d.Lhs {
v.add(name)
}
case *ast.FuncDecl:
v.add(d.Name)
case *ast.GenDecl:
if d.Tok == token.VAR {
for _, spec := range d.Specs {
if value, ok := spec.(*ast.ValueSpec); ok {
for _, name := range value.Names {
v.add(name)
}
}
}
} else if d.Tok == token.TYPE {
for _, spec := range d.Specs {
if value, ok := spec.(*ast.TypeSpec); ok {
v.add(value.Name)
}
}
}
}
return v
}
func (v exportVisitor) add(n ast.Node) {
ident, ok := n.(*ast.Ident)
if !ok {
return
}
if ident.Name == "_" || ident.Name == "" {
return
}
if ident.Obj != nil && ident.Obj.Pos() == ident.Pos() {
if ident.IsExported() {
v.exports[v.pkgPath+"."+ident.Name] = exists
}
}
}
func findImports(ctx context.Context, to []string, excludeTo []string) (map[string]interface{}, error) {
refs := make(map[string]interface{})
fs := token.NewFileSet()
err := runOnFiles(ctx, to, excludeTo, func(file string) error {
f, err := parser.ParseFile(fs, file, nil, parser.AllErrors)
if err != nil {
return fmt.Errorf("could not parse %s: %w", file, err)
} else {
v := newRefVisitor(f, refs)
ast.Walk(v, f)
}
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to find exports: %v", err)
}
return refs, nil
}
// refVisitor tracks import references.
type refVisitor struct {
f *ast.File
refs map[string]interface{}
// alias -> real pkg
importedPkgs map[string]string
}
func newRefVisitor(f *ast.File, refs map[string]interface{}) refVisitor {
ip := make(map[string]string)
for _, decl := range f.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if !ok || genDecl.Tok != token.IMPORT {
continue
}
for _, spec := range genDecl.Specs {
importSpec, ok := spec.(*ast.ImportSpec)
if ok {
impName := strings.Trim(importSpec.Path.Value, "\"")
splits := strings.Split(impName, "/")
alias := splits[len(splits)-1]
if importSpec.Name != nil {
alias = importSpec.Name.Name
}
ip[alias] = impName
}
}
}
return refVisitor{f, refs, ip}
}
func (v refVisitor) Visit(n ast.Node) ast.Visitor {
if n == nil {
return nil
}
if d, ok := n.(*ast.SelectorExpr); ok {
xIdent, ok := d.X.(*ast.Ident)
if !ok {
return v
}
if imp, ok := v.importedPkgs[xIdent.Name]; ok {
v.refs[imp+"."+d.Sel.Name] = exists
}
}
return v
}