-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
85 lines (71 loc) · 1.47 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
package main
import (
"flag"
"fmt"
"go/build"
"io"
"log"
"os"
"github.com/kisielk/gotool"
"golang.org/x/tools/go/loader"
)
const usage = `Usage of %s:
durcheck [flags] [package] ...
durcheck [flags] [directory] ...
durcheck [flags] ./..
Available flags:
-t Also check test packages
`
var (
// definitions for testing purposes
stderr io.Writer = os.Stderr
exit = os.Exit
)
func main() {
lintTests := flag.Bool("t", false, "Also check test packages")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, usage, os.Args[0])
}
flag.Parse()
importPaths := gotool.ImportPaths(flag.Args())
if len(importPaths) == 0 {
importPaths = []string{"."}
}
var wasProblem bool
for _, p := range importPaths {
loadcfg := loader.Config{Build: &build.Default}
if *lintTests {
loadcfg.ImportWithTests(p)
} else {
loadcfg.Import(p)
}
program, err := loadcfg.Load()
if err != nil {
log.Printf("failed to load packages: %s", err)
continue
}
ntf := notifier{
fset: program.Fset,
out: stderr,
}
for _, pkgInfo := range program.InitialPackages() {
if len(pkgInfo.Files) == 0 {
fmt.Fprintf(os.Stderr, "WARNING: no go files in package %q\n", pkgInfo.Pkg.Path())
continue
}
insp := &inspector{tinf: pkgInfo.Info}
for _, f := range pkgInfo.Files {
problems := insp.inspect(f)
for _, p := range problems {
wasProblem = true
ntf.notify(p)
}
}
}
}
code := 0
if wasProblem {
code = 1
}
exit(code)
}