-
Notifications
You must be signed in to change notification settings - Fork 5
/
gometalint.go
64 lines (56 loc) · 1.46 KB
/
gometalint.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
package gometalint
import (
"bufio"
"bytes"
"os/exec"
"strconv"
"strings"
log "gopkg.in/src-d/go-log.v1"
)
var (
bin = "gometalinter.v2"
defaultArgs = []string{
"--disable-all", "--enable=dupl", "--enable=gosec",
"--enable=gofmt", "--enable=goimports", "--enable=lll",
"--enable=misspell", "--enable=gocyclo",
}
)
// Comment as returned by gometalint
type Comment struct {
level string
file string
lino int32
text string
}
// RunGometalinter execs gometalint binary \w pre-configured set of linters
func RunGometalinter(args []string) []Comment {
dArgs := append([]string(nil), defaultArgs...)
args = append(dArgs, args...)
log.Debugf("Running '%s %v'\n", bin, args)
out, _ := exec.Command(bin, args...).Output() // nolint: gas
// ignoring err, as it's always not nil if anything found
var comments []Comment
s := bufio.NewScanner(bytes.NewReader(out))
for s.Scan() { //scan stdout for results
sp := strings.SplitN(s.Text(), ":", 5)
if len(sp) != 5 {
log.Warningf("failed to parse string %s\n", s.Text())
continue
}
file, line, _, severity, msg := sp[0], sp[1], sp[2], sp[3], sp[4]
c := Comment{
level: severity,
file: file,
text: msg,
}
lino, err := strconv.Atoi(line)
if err != nil {
log.Warningf("failed to parse line number from '%s' in '%s'\n", line, sp)
continue
}
c.lino = int32(lino)
comments = append(comments, c)
}
log.Debugf("Done. %d issues found\n", len(comments))
return comments
}