-
Notifications
You must be signed in to change notification settings - Fork 4
/
zblint.go
197 lines (161 loc) · 4.11 KB
/
zblint.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
package zblint
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/urfave/cli"
"jrubin.io/zb/lib/lintflags"
"jrubin.io/zb/lib/project"
"jrubin.io/zb/lib/zbcontext"
)
// ZBLint provides methods for working with cached lint result files
type ZBLint struct {
lintflags.Data
NoMissingComment bool
IgnoreSuffixes cli.StringSlice
ignoreSuffixMap map[string]struct{}
}
// DefaultIgnoreSuffixes lists the file suffixes for which lint results will be
// filtered out
var DefaultIgnoreSuffixes = []string{
".pb.go",
".pb.gw.go",
"_string.go",
"bindata.go",
"bindata_assetfs.go",
"static.go",
}
// LintSetup must be called before other methods to complete the configuration
// from the context
func (l *ZBLint) LintSetup(ctx zbcontext.Context) zbcontext.Context {
if len(l.IgnoreSuffixes) == 0 {
l.IgnoreSuffixes = DefaultIgnoreSuffixes
}
l.ignoreSuffixMap = map[string]struct{}{}
for _, is := range l.IgnoreSuffixes {
if is == "" {
continue
}
l.ignoreSuffixMap[is] = struct{}{}
}
if filepath.Base(ctx.CacheDir) != "lint" {
ctx.CacheDir = filepath.Join(ctx.CacheDir, "lint")
}
return ctx
}
// CacheFile returns the location of the lint cache file for a given package
func (l *ZBLint) CacheFile(ctx zbcontext.Context, p *project.Package) (string, error) {
lintHash, err := p.LintHash(&l.Data)
if err != nil {
return "", err
}
return filepath.Join(
ctx.CacheDir,
lintHash[:3],
fmt.Sprintf("%s.lint", lintHash[3:]),
), nil
}
// HaveResult checks to see if a lint result is available for a given package
func (l *ZBLint) HaveResult(ctx zbcontext.Context, p *project.Package) (bool, error) {
if l.Data.Force {
return false, nil
}
file, err := l.CacheFile(ctx, p)
if err != nil {
return false, err
}
fi, err := os.Stat(file)
return err == nil && fi.Mode().IsRegular(), nil
}
// ReadResult reads lint results from the Reader and writes the unfiltered data
// to the file and the filtered data to the Writer
func (l *ZBLint) ReadResult(w io.Writer, pr io.Reader, file string) error {
if err := os.MkdirAll(filepath.Dir(file), 0700); err != nil {
return err
}
fd, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer func() { _ = fd.Close() }() // nosec
_, err = l.readCommon(w, pr, fd)
return err
}
var (
levelRE = regexp.MustCompile(`\A([^:]*):(\d*):(\d*):(\w+): (.*?) \((\w+)\)( \(cached\))?\n\z`)
commentRE = regexp.MustCompile(` should have comment.* or be unexported`)
)
// Part enum representing each field in a gometalinter line
type Part int
// The different fields of the gometalinter line
const (
LintFile Part = 1 + iota
LintLine
LintColumn
LintLevel
LintMessage
LintLinter
)
func (l *ZBLint) readCommon(w io.Writer, pr io.Reader, fd io.Writer) (bool, error) {
r := bufio.NewReader(pr)
defer func() { _, _ = io.Copy(w, r) }() // nosec
var buf bytes.Buffer
var foundLines bool
LOOP:
for eof := false; !eof; {
line, err := r.ReadString('\n')
if err == io.EOF {
eof = true
} else if err != nil {
return foundLines, err
}
m := levelRE.FindStringSubmatch(line)
if m == nil {
if fd != nil {
fmt.Fprintf(&buf, "%s", line)
}
if _, err := w.Write([]byte(line)); err != nil {
return foundLines, err
}
continue
}
foundLines = true
if fd != nil {
fmt.Fprintf(&buf, "%s (cached)\n", strings.TrimSuffix(line, "\n"))
}
if l.NoMissingComment &&
m[LintLinter] == "golint" &&
commentRE.MatchString(m[LintMessage]) {
continue
}
for is := range l.ignoreSuffixMap {
if strings.HasSuffix(m[LintFile], is) {
continue LOOP
}
}
if _, err := w.Write([]byte(line)); err != nil {
return foundLines, err
}
}
if fd != nil {
if _, err := buf.WriteTo(fd); err != nil {
return foundLines, err
}
}
return foundLines, nil
}
// ShowResult reads data from cacheFile and writes the filtered data to the
// Writer
func (l *ZBLint) ShowResult(w io.Writer, cacheFile string) (bool, error) {
fd, err := os.Open(cacheFile)
if err != nil {
return false, err
}
defer func() { _ = fd.Close() }() // nosec
return l.readCommon(w, fd, nil)
}