forked from kisielk/errcheck
/
main.go
94 lines (83 loc) · 2.04 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
package main
import (
"flag"
"fmt"
"os"
"regexp"
"strings"
"github.com/kisielk/errcheck/lib"
"github.com/kisielk/gotool"
)
// Err prints an error to Stderr
func Err(s string, args ...interface{}) {
fmt.Fprintf(os.Stderr, "error: "+s+"\n", args...)
}
// Fatal calls Err followed by os.Exit(2)
func Fatalf(s string, args ...interface{}) {
Err(s, args...)
os.Exit(2)
}
type ignoreFlag map[string]*regexp.Regexp
func (f ignoreFlag) String() string {
pairs := make([]string, 0, len(f))
for pkg, re := range f {
prefix := ""
if pkg != "" {
prefix = pkg + ":"
}
pairs = append(pairs, prefix+re.String())
}
return fmt.Sprintf("%q", strings.Join(pairs, ","))
}
func (f ignoreFlag) Set(s string) error {
if s == "" {
return nil
}
for _, pair := range strings.Split(s, ",") {
colonIndex := strings.Index(pair, ":")
var pkg, re string
if colonIndex == -1 {
pkg = ""
re = pair
} else {
pkg = pair[:colonIndex]
re = pair[colonIndex+1:]
}
regex, err := regexp.Compile(re)
if err != nil {
return err
}
f[pkg] = regex
}
return nil
}
var dotStar = regexp.MustCompile(".*")
func main() {
ignore := ignoreFlag(map[string]*regexp.Regexp{
"fmt": dotStar,
})
flag.Var(ignore, "ignore", "comma-separated list of pairs of the form pkg:regex\n"+
" the regex is used to ignore names within pkg")
ignorePkg := flag.String("ignorepkg", "", "comma-separated list of package paths to ignore")
blank := flag.Bool("blank", false, "if true, check for errors assigned to blank identifier")
flag.Parse()
for _, pkg := range strings.Split(*ignorePkg, ",") {
if pkg != "" {
ignore[pkg] = dotStar
}
}
var pkgPaths = gotool.ImportPaths(flag.Args())
if err := errcheck.CheckPackages(pkgPaths, ignore, *blank); err != nil {
if e, ok := err.(errcheck.UncheckedErrors); ok {
for _, uncheckedError := range e.Errors {
fmt.Println(uncheckedError)
}
os.Exit(1)
} else if err == errcheck.ErrNoGoFiles {
fmt.Fprintln(os.Stderr, err)
os.Exit(0)
}
Fatalf("failed to check package: %s", err)
}
os.Exit(0)
}