1
0
mirror of https://github.com/prometheus-community/postgres_exporter.git synced 2025-08-05 06:21:12 +03:00

Update vendored tools/.

This commit is contained in:
Will Rouesnel
2017-08-03 21:19:17 +10:00
parent 1afbd62ab1
commit a7ff84a674
22 changed files with 1452 additions and 1109 deletions

View File

@@ -18,6 +18,7 @@
- [1. Update to the latest build of gometalinter and all linters](#1-update-to-the-latest-build-of-gometalinter-and-all-linters)
- [2. Analyse the debug output](#2-analyse-the-debug-output)
- [3. Report an issue.](#3-report-an-issue)
- [How do I filter issues between two git refs?](#how-do-i-filter-issues-between-two-git-refs)
- [Details](#details)
- [Checkstyle XML format](#checkstyle-xml-format)
@@ -54,7 +55,7 @@ It is intended for use with editor/IDE integration.
## Supported linters
- [go vet](https://golang.org/cmd/vet/) - Reports potential errors that otherwise compile.
- [go vet --shadow](https://golang.org/cmd/vet/#hdr-Shadowed_variables) - Reports variables that may have been unintentionally shadowed.
- [go tool vet --shadow](https://golang.org/cmd/vet/#hdr-Shadowed_variables) - Reports variables that may have been unintentionally shadowed.
- [gotype](https://golang.org/x/tools/cmd/gotype) - Syntactic and semantic analysis similar to the Go compiler.
- [deadcode](https://github.com/tsenart/deadcode) - Finds unused code.
- [gocyclo](https://github.com/alecthomas/gocyclo) - Computes the cyclomatic complexity of functions.
@@ -63,13 +64,12 @@ It is intended for use with editor/IDE integration.
- [structcheck](https://github.com/opennota/check) - Find unused struct fields.
- [aligncheck](https://github.com/opennota/check) - Warn about un-optimally aligned structures.
- [errcheck](https://github.com/kisielk/errcheck) - Check that error return values are used.
- [megacheck](https://github.com/dominikh/go-tools/tree/master/cmd/megacheck) - Run staticcheck, gosimple and unused, sharing work.
- [dupl](https://github.com/mibk/dupl) - Reports potentially duplicated code.
- [ineffassign](https://github.com/gordonklaus/ineffassign/blob/master/list) - Detect when assignments to *existing* variables are not used.
- [interfacer](https://github.com/mvdan/interfacer) - Suggest narrower interfaces that can be used.
- [unconvert](https://github.com/mdempsky/unconvert) - Detect redundant type conversions.
- [goconst](https://github.com/jgautheron/goconst) - Finds repeated strings that could be replaced by a constant.
- [gosimple](https://github.com/dominikh/go-tools/tree/master/cmd/gosimple) - Report simplifications in code.
- [staticcheck](https://github.com/dominikh/go-tools/tree/master/cmd/staticcheck) - Statically detect bugs, both obvious and subtle ones.
- [gas](https://github.com/GoASTScanner/gas) - Inspects source code for security problems by scanning the Go AST.
Disabled by default (enable with `--enable=<linter>`):
@@ -78,11 +78,13 @@ Disabled by default (enable with `--enable=<linter>`):
- [test](http://golang.org/pkg/testing/) - Show location of test failures from the stdlib testing module.
- [gofmt -s](https://golang.org/cmd/gofmt/) - Checks if the code is properly formatted and could not be further simplified.
- [goimports](https://godoc.org/golang.org/x/tools/cmd/goimports) - Checks missing or unreferenced package imports.
- [gosimple](https://github.com/dominikh/go-tools/tree/master/cmd/gosimple) - Report simplifications in code.
- [lll](https://github.com/walle/lll) - Report long lines (see `--line-length=N`).
- [misspell](https://github.com/client9/misspell) - Finds commonly misspelled English words.
- [unparam](https://github.com/mvdan/unparam) - Find unused function parameters.
- [unused](https://github.com/dominikh/go-tools/tree/master/cmd/unused) - Find unused variables.
- [safesql](https://github.com/stripe/safesql) - Finds potential SQL injection vulnerabilities.
- [staticcheck](https://github.com/dominikh/go-tools/tree/master/cmd/staticcheck) - Statically detect bugs, both obvious and subtle ones.
Additional linters can be added through the command line with `--linter=NAME:COMMAND:PATTERN` (see [below](#details)).
@@ -95,13 +97,13 @@ in `config.go`.
The configuration file mostly corresponds to command-line flags, with the following exceptions:
- Linters defined in the configuration file will overlay existing definitions, not replace them.
- "Enable" defines the exact set of linters that will be enabled.
- "Enable" defines the exact set of linters that will be enabled (default
linters are disabled).
Here is an example configuration file:
```json
{
"DisableAll": true,
"Enable": ["deadcode", "unconvert"]
}
```
@@ -291,6 +293,21 @@ failing.
Failing all else, if the problem looks like a bug please file an issue and
include the output of `gometalinter --debug`.
### How do I filter issues between two git refs?
[revgrep](https://github.com/bradleyfalzon/revgrep) can be used to filter the output of `gometalinter`
to show issues on lines that have changed between two git refs, such as unstaged changes, changes in
`HEAD` vs `master` and between `master` and `origin/master`. See the project's documentation and `-help`
usage for more information.
```
go get -u github.com/bradleyfalzon/revgrep/...
gometalinter |& revgrep # If unstaged changes or untracked files, those issues are shown.
gometalinter |& revgrep # Else show issues in the last commit.
gometalinter |& revgrep master # Show issues between master and HEAD (or any other reference).
gometalinter |& revgrep origin/master # Show issues that haven't been pushed.
```
## Details
Additional linters can be configured via the command line:

View File

@@ -18,6 +18,13 @@ type (
}
)
func maybeAggregateIssues(issues chan *Issue) chan *Issue {
if !config.Aggregate {
return issues
}
return aggregateIssues(issues)
}
func aggregateIssues(issues chan *Issue) chan *Issue {
out := make(chan *Issue, 1000000)
issueMap := make(map[issueKey]*multiIssue)
@@ -30,18 +37,18 @@ func aggregateIssues(issues chan *Issue) chan *Issue {
message: issue.Message,
}
if existing, ok := issueMap[key]; ok {
existing.linterNames = append(existing.linterNames, issue.Linter.Name)
existing.linterNames = append(existing.linterNames, issue.Linter)
} else {
issueMap[key] = &multiIssue{
Issue: issue,
linterNames: []string{issue.Linter.Name},
linterNames: []string{issue.Linter},
}
}
}
for _, multi := range issueMap {
issue := multi.Issue
sort.Strings(multi.linterNames)
issue.Linter.Name = strings.Join(multi.linterNames, ", ")
issue.Linter = strings.Join(multi.linterNames, ", ")
out <- issue
}
close(out)

View File

@@ -52,7 +52,7 @@ func outputToCheckstyle(issues chan *Issue) int {
Line: issue.Line,
Message: issue.Message,
Severity: string(issue.Severity),
Source: issue.Linter.Name,
Source: issue.Linter,
})
status = 1
}

View File

@@ -1,11 +1,10 @@
package main
import (
"encoding/json"
"runtime"
"text/template"
"time"
"gopkg.in/alecthomas/kingpin.v3-unstable"
)
// Config for gometalinter. This can be loaded from a JSON file with --config.
@@ -45,144 +44,66 @@ type Config struct { // nolint: aligncheck
DuplThreshold int
Sort []string
Test bool
Deadline time.Duration `json:"-"`
Deadline jsonDuration
Errors bool
JSON bool
Checkstyle bool
EnableGC bool
Aggregate bool
DeadlineJSONCrutch string `json:"Deadline"`
EnableAll bool
}
type jsonDuration time.Duration
func (td *jsonDuration) UnmarshalJSON(raw []byte) error {
var durationAsString string
if err := json.Unmarshal(raw, &durationAsString); err != nil {
return err
}
duration, err := time.ParseDuration(durationAsString)
*td = jsonDuration(duration)
return err
}
// Duration returns the value as a time.Duration
func (td *jsonDuration) Duration() time.Duration {
return time.Duration(*td)
}
// TODO: should be a field on Config struct
var formatTemplate = &template.Template{}
var sortKeys = []string{"none", "path", "line", "column", "severity", "message", "linter"}
// Configuration defaults.
var (
vetRe = `^(?:vet:.*?\.go:\s+(?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.*))|(?:(?P<path>.*?\.go):(?P<line>\d+):\s*(?P<message>.*))$`
var config = &Config{
Format: "{{.Path}}:{{.Line}}:{{if .Col}}{{.Col}}{{end}}:{{.Severity}}: {{.Message}} ({{.Linter}})",
predefinedPatterns = map[string]string{
"PATH:LINE:COL:MESSAGE": `^(?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.*)$`,
"PATH:LINE:MESSAGE": `^(?P<path>.*?\.go):(?P<line>\d+):\s*(?P<message>.*)$`,
}
formatTemplate = &template.Template{}
installMap = map[string]string{
"aligncheck": "github.com/opennota/check/cmd/aligncheck",
"deadcode": "github.com/tsenart/deadcode",
"dupl": "github.com/mibk/dupl",
"errcheck": "github.com/kisielk/errcheck",
"gas": "github.com/GoASTScanner/gas",
"goconst": "github.com/jgautheron/goconst/cmd/goconst",
"gocyclo": "github.com/alecthomas/gocyclo",
"goimports": "golang.org/x/tools/cmd/goimports",
"golint": "github.com/golang/lint/golint",
"gosimple": "honnef.co/go/tools/cmd/gosimple",
"gotype": "golang.org/x/tools/cmd/gotype",
"ineffassign": "github.com/gordonklaus/ineffassign",
"interfacer": "github.com/mvdan/interfacer/cmd/interfacer",
"lll": "github.com/walle/lll/cmd/lll",
"misspell": "github.com/client9/misspell/cmd/misspell",
"safesql": "github.com/stripe/safesql",
"staticcheck": "honnef.co/go/tools/cmd/staticcheck",
"structcheck": "github.com/opennota/check/cmd/structcheck",
"unconvert": "github.com/mdempsky/unconvert",
"unparam": "github.com/mvdan/unparam",
"unused": "honnef.co/go/tools/cmd/unused",
"varcheck": "github.com/opennota/check/cmd/varcheck",
}
acceptsEllipsis = map[string]bool{
"aligncheck": true,
"errcheck": true,
"golint": true,
"gosimple": true,
"interfacer": true,
"staticcheck": true,
"structcheck": true,
"test": true,
"varcheck": true,
"unconvert": true,
}
slowLinters = []string{"structcheck", "varcheck", "errcheck", "aligncheck", "testify", "test", "interfacer", "unconvert", "deadcode", "safesql", "staticcheck", "unparam", "unused", "gosimple"}
sortKeys = []string{"none", "path", "line", "column", "severity", "message", "linter"}
// Linter definitions.
linterDefinitions = map[string]string{
"aligncheck": `aligncheck {path}:^(?:[^:]+: )?(?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.+)$`,
"deadcode": `deadcode {path}:^deadcode: (?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.*)$`,
"dupl": `dupl -plumbing -threshold {duplthreshold} {path}/*.go:^(?P<path>.*?\.go):(?P<line>\d+)-\d+:\s*(?P<message>.*)$`,
"errcheck": `errcheck -abspath {path}:PATH:LINE:COL:MESSAGE`,
"gas": `gas -fmt=csv {path}/*.go:^(?P<path>.*?\.go),(?P<line>\d+),(?P<message>[^,]+,[^,]+,[^,]+)`,
"goconst": `goconst -min-occurrences {min_occurrences} -min-length {min_const_length} {path}:PATH:LINE:COL:MESSAGE`,
"gocyclo": `gocyclo -over {mincyclo} {path}:^(?P<cyclo>\d+)\s+\S+\s(?P<function>\S+)\s+(?P<path>.*?\.go):(?P<line>\d+):(\d+)$`,
"gofmt": `gofmt -l -s {path}/*.go:^(?P<path>.*?\.go)$`,
"goimports": `goimports -l {path}/*.go:^(?P<path>.*?\.go)$`,
"golint": "golint -min_confidence {min_confidence} {path}:PATH:LINE:COL:MESSAGE",
"gosimple": "gosimple {path}:PATH:LINE:COL:MESSAGE",
"gotype": "gotype -e {tests=-a} {path}:PATH:LINE:COL:MESSAGE",
"ineffassign": `ineffassign -n {path}:PATH:LINE:COL:MESSAGE`,
"interfacer": `interfacer {path}:PATH:LINE:COL:MESSAGE`,
"lll": `lll -g -l {maxlinelength} {path}/*.go:PATH:LINE:MESSAGE`,
"misspell": "misspell -j 1 {path}/*.go:PATH:LINE:COL:MESSAGE",
"safesql": `safesql {path}:^- (?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+)$`,
"staticcheck": "staticcheck {path}:PATH:LINE:COL:MESSAGE",
"structcheck": `structcheck {tests=-t} {path}:^(?:[^:]+: )?(?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.+)$`,
"test": `go test {path}:^--- FAIL: .*$\s+(?P<path>.*?\.go):(?P<line>\d+): (?P<message>.*)$`,
"testify": `go test {path}:Location:\s+(?P<path>.*?\.go):(?P<line>\d+)$\s+Error:\s+(?P<message>[^\n]+)`,
"unconvert": "unconvert {path}:PATH:LINE:COL:MESSAGE",
"unparam": `unparam {path}:PATH:LINE:COL:MESSAGE`,
"unused": `unused {path}:PATH:LINE:COL:MESSAGE`,
"varcheck": `varcheck {path}:^(?:[^:]+: )?(?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.*)$`,
"vet": `go tool vet {path}/*.go:` + vetRe,
"vetshadow": `go tool vet --shadow {path}/*.go:` + vetRe,
}
pathsArg = kingpin.Arg("path", "Directories to lint. Defaults to \".\". <path>/... will recurse.").Strings()
config = &Config{
Format: "{{.Path}}:{{.Line}}:{{if .Col}}{{.Col}}{{end}}:{{.Severity}}: {{.Message}} ({{.Linter}})",
Severity: map[string]string{
"gotype": "error",
"test": "error",
"testify": "error",
"vet": "error",
},
MessageOverride: map[string]string{
"errcheck": "error return value not checked ({message})",
"gocyclo": "cyclomatic complexity {cyclo} of function {function}() is high (> {mincyclo})",
"gofmt": "file is not gofmted with -s",
"goimports": "file is not goimported",
"safesql": "potentially unsafe SQL statement",
"structcheck": "unused struct field {message}",
"unparam": "parameter {message}",
"varcheck": "unused variable or constant {message}",
},
Enable: []string{
"aligncheck",
"deadcode",
"errcheck",
"gas",
"goconst",
"gocyclo",
"golint",
"gosimple",
"gotype",
"ineffassign",
"interfacer",
"staticcheck",
"structcheck",
"unconvert",
"varcheck",
"vet",
"vetshadow",
},
VendoredLinters: true,
Concurrency: runtime.NumCPU(),
Cyclo: 10,
LineLength: 80,
MinConfidence: 0.8,
MinOccurrences: 3,
MinConstLength: 3,
DuplThreshold: 50,
Sort: []string{"none"},
Deadline: time.Second * 30,
}
)
Severity: map[string]string{
"gotype": "error",
"test": "error",
"testify": "error",
"vet": "error",
},
MessageOverride: map[string]string{
"errcheck": "error return value not checked ({message})",
"gocyclo": "cyclomatic complexity {cyclo} of function {function}() is high (> {mincyclo})",
"gofmt": "file is not gofmted with -s",
"goimports": "file is not goimported",
"safesql": "potentially unsafe SQL statement",
"structcheck": "unused struct field {message}",
"unparam": "parameter {message}",
"varcheck": "unused variable or constant {message}",
},
Enable: defaultEnabled(),
VendoredLinters: true,
Concurrency: runtime.NumCPU(),
Cyclo: 10,
LineLength: 80,
MinConfidence: 0.8,
MinOccurrences: 3,
MinConstLength: 3,
DuplThreshold: 50,
Sort: []string{"none"},
Deadline: jsonDuration(time.Second * 30),
}

View File

@@ -24,14 +24,14 @@ func (i *ignoredRange) matches(issue *Issue) bool {
return true
}
for _, l := range i.linters {
if l == issue.Linter.Name {
if l == issue.Linter {
return true
}
}
return false
}
func (i *ignoredRange) near(col, start, end int) bool {
func (i *ignoredRange) near(col, start int) bool {
return col == i.col && i.end == start-1
}
@@ -42,15 +42,13 @@ func (ir ignoredRanges) Swap(i, j int) { ir[i], ir[j] = ir[j], ir[i] }
func (ir ignoredRanges) Less(i, j int) bool { return ir[i].end < ir[j].end }
type directiveParser struct {
paths []string
lock sync.Mutex
files map[string]ignoredRanges
fset *token.FileSet
}
func newDirectiveParser(paths []string) *directiveParser {
func newDirectiveParser() *directiveParser {
return &directiveParser{
paths: paths,
files: map[string]ignoredRanges{},
fset: token.NewFileSet(),
}
@@ -92,7 +90,7 @@ func (a *rangeExpander) Visit(node ast.Node) ast.Visitor {
found := sort.Search(len(a.ranges), func(i int) bool {
return a.ranges[i].end+1 >= start
})
if found < len(a.ranges) && a.ranges[found].near(startPos.Column, start, end) {
if found < len(a.ranges) && a.ranges[found].near(startPos.Column, start) {
r := a.ranges[found]
if r.start > start {
r.start = start
@@ -144,12 +142,6 @@ func extractCommentGroupRange(fset *token.FileSet, comments ...*ast.CommentGroup
return
}
func (d *directiveParser) in(n ast.Node, issue *Issue) bool {
start := d.fset.Position(n.Pos())
end := d.fset.Position(n.End())
return issue.Line >= start.Line && issue.Line <= end.Line
}
func filterIssuesViaDirectives(directives *directiveParser, issues chan *Issue) chan *Issue {
out := make(chan *Issue, 1000000)
go func() {

View File

@@ -0,0 +1,384 @@
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/google/shlex"
"gopkg.in/alecthomas/kingpin.v3-unstable"
)
type Vars map[string]string
func (v Vars) Copy() Vars {
out := Vars{}
for k, v := range v {
out[k] = v
}
return out
}
func (v Vars) Replace(s string) string {
for k, v := range v {
prefix := regexp.MustCompile(fmt.Sprintf("{%s=([^}]*)}", k))
if v != "" {
s = prefix.ReplaceAllString(s, "$1")
} else {
s = prefix.ReplaceAllString(s, "")
}
s = strings.Replace(s, fmt.Sprintf("{%s}", k), v, -1)
}
return s
}
// Severity of linter message.
type Severity string
// Linter message severity levels.
const ( // nolint: deadcode
Error Severity = "error"
Warning Severity = "warning"
)
type Issue struct {
Linter string `json:"linter"`
Severity Severity `json:"severity"`
Path string `json:"path"`
Line int `json:"line"`
Col int `json:"col"`
Message string `json:"message"`
}
func (i *Issue) String() string {
buf := new(bytes.Buffer)
err := formatTemplate.Execute(buf, i)
kingpin.FatalIfError(err, "Invalid output format")
return buf.String()
}
type linterState struct {
*Linter
paths []string
issues chan *Issue
vars Vars
exclude *regexp.Regexp
include *regexp.Regexp
deadline <-chan time.Time
}
func (l *linterState) Partitions() ([][]string, error) {
command := l.vars.Replace(l.Command)
cmdArgs, err := parseCommand(command)
if err != nil {
return nil, err
}
parts, err := l.Linter.PartitionStrategy(cmdArgs, l.paths)
if err != nil {
return nil, err
}
return parts, nil
}
func runLinters(linters map[string]*Linter, paths []string, concurrency int, exclude, include *regexp.Regexp) (chan *Issue, chan error) {
errch := make(chan error, len(linters))
concurrencych := make(chan bool, concurrency)
incomingIssues := make(chan *Issue, 1000000)
processedIssues := filterIssuesViaDirectives(
newDirectiveParser(),
maybeSortIssues(maybeAggregateIssues(incomingIssues)))
vars := Vars{
"duplthreshold": fmt.Sprintf("%d", config.DuplThreshold),
"mincyclo": fmt.Sprintf("%d", config.Cyclo),
"maxlinelength": fmt.Sprintf("%d", config.LineLength),
"min_confidence": fmt.Sprintf("%f", config.MinConfidence),
"min_occurrences": fmt.Sprintf("%d", config.MinOccurrences),
"min_const_length": fmt.Sprintf("%d", config.MinConstLength),
"tests": "",
}
if config.Test {
vars["tests"] = "-t"
}
wg := &sync.WaitGroup{}
for _, linter := range linters {
deadline := time.After(config.Deadline.Duration())
state := &linterState{
Linter: linter,
issues: incomingIssues,
paths: paths,
vars: vars,
exclude: exclude,
include: include,
deadline: deadline,
}
partitions, err := state.Partitions()
if err != nil {
errch <- err
continue
}
for _, args := range partitions {
wg.Add(1)
// Call the goroutine with a copy of the args array so that the
// contents of the array are not modified by the next iteration of
// the above for loop
go func(args []string) {
concurrencych <- true
err := executeLinter(state, args)
if err != nil {
errch <- err
}
<-concurrencych
wg.Done()
}(append(args))
}
}
go func() {
wg.Wait()
close(incomingIssues)
close(errch)
}()
return processedIssues, errch
}
func executeLinter(state *linterState, args []string) error {
if len(args) == 0 {
return fmt.Errorf("missing linter command")
}
start := time.Now()
debug("executing %s", strings.Join(args, " "))
buf := bytes.NewBuffer(nil)
command := args[0]
cmd := exec.Command(command, args[1:]...) // nolint: gas
cmd.Stdout = buf
cmd.Stderr = buf
err := cmd.Start()
if err != nil {
return fmt.Errorf("failed to execute linter %s: %s", command, err)
}
done := make(chan bool)
go func() {
err = cmd.Wait()
done <- true
}()
// Wait for process to complete or deadline to expire.
select {
case <-done:
case <-state.deadline:
err = fmt.Errorf("deadline exceeded by linter %s (try increasing --deadline)",
state.Name)
kerr := cmd.Process.Kill()
if kerr != nil {
warning("failed to kill %s: %s", state.Name, kerr)
}
return err
}
if err != nil {
debug("warning: %s returned %s: %s", command, err, buf.String())
}
processOutput(state, buf.Bytes())
elapsed := time.Since(start)
debug("%s linter took %s", state.Name, elapsed)
return nil
}
func parseCommand(command string) ([]string, error) {
args, err := shlex.Split(command)
if err != nil {
return nil, err
}
if len(args) == 0 {
return nil, fmt.Errorf("invalid command %q", command)
}
exe, err := exec.LookPath(args[0])
if err != nil {
return nil, err
}
return append([]string{exe}, args[1:]...), nil
}
// nolint: gocyclo
func processOutput(state *linterState, out []byte) {
re := state.regex
all := re.FindAllSubmatchIndex(out, -1)
debug("%s hits %d: %s", state.Name, len(all), state.Pattern)
cwd, err := os.Getwd()
if err != nil {
warning("failed to get working directory %s", err)
}
// Create a local copy of vars so they can be modified by the linter output
vars := state.vars.Copy()
for _, indices := range all {
group := [][]byte{}
for i := 0; i < len(indices); i += 2 {
var fragment []byte
if indices[i] != -1 {
fragment = out[indices[i]:indices[i+1]]
}
group = append(group, fragment)
}
issue := &Issue{Line: 1, Linter: state.Linter.Name}
for i, name := range re.SubexpNames() {
if group[i] == nil {
continue
}
part := string(group[i])
if name != "" {
vars[name] = part
}
switch name {
case "path":
issue.Path = relativePath(cwd, part)
case "line":
n, err := strconv.ParseInt(part, 10, 32)
kingpin.FatalIfError(err, "line matched invalid integer")
issue.Line = int(n)
case "col":
n, err := strconv.ParseInt(part, 10, 32)
kingpin.FatalIfError(err, "col matched invalid integer")
issue.Col = int(n)
case "message":
issue.Message = part
case "":
}
}
// TODO: set messageOveride and severity on the Linter instead of reading
// them directly from the static config
if m, ok := config.MessageOverride[state.Name]; ok {
issue.Message = vars.Replace(m)
}
if sev, ok := config.Severity[state.Name]; ok {
issue.Severity = Severity(sev)
} else {
issue.Severity = Warning
}
if state.exclude != nil && state.exclude.MatchString(issue.String()) {
continue
}
if state.include != nil && !state.include.MatchString(issue.String()) {
continue
}
state.issues <- issue
}
}
func relativePath(root, path string) string {
fallback := path
root = resolvePath(root)
path = resolvePath(path)
var err error
path, err = filepath.Rel(root, path)
if err != nil {
warning("failed to make %s a relative path: %s", fallback, err)
return fallback
}
return path
}
func resolvePath(path string) string {
var err error
fallback := path
if !filepath.IsAbs(path) {
path, err = filepath.Abs(path)
if err != nil {
warning("failed to make %s an absolute path: %s", fallback, err)
return fallback
}
}
path, err = filepath.EvalSymlinks(path)
if err != nil {
warning("failed to resolve symlinks in %s: %s", fallback, err)
return fallback
}
return path
}
type sortedIssues struct {
issues []*Issue
order []string
}
func (s *sortedIssues) Len() int { return len(s.issues) }
func (s *sortedIssues) Swap(i, j int) { s.issues[i], s.issues[j] = s.issues[j], s.issues[i] }
// nolint: gocyclo
func (s *sortedIssues) Less(i, j int) bool {
l, r := s.issues[i], s.issues[j]
for _, key := range s.order {
switch key {
case "path":
if l.Path > r.Path {
return false
}
case "line":
if l.Line > r.Line {
return false
}
case "column":
if l.Col > r.Col {
return false
}
case "severity":
if l.Severity > r.Severity {
return false
}
case "message":
if l.Message > r.Message {
return false
}
case "linter":
if l.Linter > r.Linter {
return false
}
}
}
return true
}
func maybeSortIssues(issues chan *Issue) chan *Issue {
if reflect.DeepEqual([]string{"none"}, config.Sort) {
return issues
}
out := make(chan *Issue, 1000000)
sorted := &sortedIssues{
issues: []*Issue{},
order: config.Sort,
}
go func() {
for issue := range issues {
sorted.issues = append(sorted.issues, issue)
}
sort.Sort(sorted)
for _, issue := range sorted.issues {
out <- issue
}
close(out)
}()
return out
}

View File

@@ -0,0 +1,392 @@
package main
import (
"fmt"
"os"
"os/exec"
"regexp"
"sort"
"strings"
"gopkg.in/alecthomas/kingpin.v3-unstable"
)
type LinterConfig struct {
Name string
Command string
Pattern string
InstallFrom string
PartitionStrategy partitionStrategy
IsFast bool
defaultEnabled bool
}
type Linter struct {
LinterConfig
regex *regexp.Regexp
}
// NewLinter returns a new linter from a config
func NewLinter(config LinterConfig) (*Linter, error) {
if p, ok := predefinedPatterns[config.Pattern]; ok {
config.Pattern = p
}
regex, err := regexp.Compile("(?m:" + config.Pattern + ")")
if err != nil {
return nil, err
}
return &Linter{
LinterConfig: config,
regex: regex,
}, nil
}
func (l *Linter) String() string {
return l.Name
}
var predefinedPatterns = map[string]string{
"PATH:LINE:COL:MESSAGE": `^(?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.*)$`,
"PATH:LINE:MESSAGE": `^(?P<path>.*?\.go):(?P<line>\d+):\s*(?P<message>.*)$`,
}
func getLinterByName(name string, customSpec string) *Linter {
if customSpec != "" {
return parseLinterSpec(name, customSpec)
}
linter, _ := NewLinter(defaultLinters[name])
return linter
}
func parseLinterSpec(name string, spec string) *Linter {
parts := strings.SplitN(spec, ":", 2)
if len(parts) < 2 {
kingpin.Fatalf("invalid linter: %q", spec)
}
config := defaultLinters[name]
config.Command, config.Pattern = parts[0], parts[1]
linter, err := NewLinter(config)
kingpin.FatalIfError(err, "invalid linter %q", name)
return linter
}
func makeInstallCommand(linters ...string) []string {
cmd := []string{"get"}
if config.VendoredLinters {
cmd = []string{"install"}
} else {
if config.Update {
cmd = append(cmd, "-u")
}
if config.Force {
cmd = append(cmd, "-f")
}
if config.DownloadOnly {
cmd = append(cmd, "-d")
}
}
if config.Debug {
cmd = append(cmd, "-v")
}
cmd = append(cmd, linters...)
return cmd
}
func installLintersWithOneCommand(targets []string) error {
cmd := makeInstallCommand(targets...)
debug("go %s", strings.Join(cmd, " "))
c := exec.Command("go", cmd...) // nolint: gas
c.Stdout = os.Stdout
c.Stderr = os.Stderr
return c.Run()
}
func installLintersIndividually(targets []string) {
failed := []string{}
for _, target := range targets {
cmd := makeInstallCommand(target)
debug("go %s", strings.Join(cmd, " "))
c := exec.Command("go", cmd...) // nolint: gas
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if err := c.Run(); err != nil {
warning("failed to install %s: %s", target, err)
failed = append(failed, target)
}
}
if len(failed) > 0 {
kingpin.Fatalf("failed to install the following linters: %s", strings.Join(failed, ", "))
}
}
func installLinters() {
names := make([]string, 0, len(defaultLinters))
targets := make([]string, 0, len(defaultLinters))
for name, config := range defaultLinters {
if config.InstallFrom == "" {
continue
}
names = append(names, name)
targets = append(targets, config.InstallFrom)
}
sort.Strings(names)
namesStr := strings.Join(names, "\n ")
if config.DownloadOnly {
fmt.Printf("Downloading:\n %s\n", namesStr)
} else {
fmt.Printf("Installing:\n %s\n", namesStr)
}
err := installLintersWithOneCommand(targets)
if err == nil {
return
}
warning("failed to install one or more linters: %s (installing individually)", err)
installLintersIndividually(targets)
}
func getDefaultLinters() []*Linter {
out := []*Linter{}
for _, config := range defaultLinters {
linter, err := NewLinter(config)
kingpin.FatalIfError(err, "invalid linter %q", config.Name)
out = append(out, linter)
}
return out
}
func defaultEnabled() []string {
enabled := []string{}
for name, config := range defaultLinters {
if config.defaultEnabled {
enabled = append(enabled, name)
}
}
return enabled
}
const vetPattern = `^(?:vet:.*?\.go:\s+(?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.*))|(?:(?P<path>.*?\.go):(?P<line>\d+):\s*(?P<message>.*))$`
var defaultLinters = map[string]LinterConfig{
"aligncheck": {
Name: "aligncheck",
Command: "aligncheck",
Pattern: `^(?:[^:]+: )?(?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.+)$`,
InstallFrom: "github.com/opennota/check/cmd/aligncheck",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
defaultEnabled: true,
},
"deadcode": {
Name: "deadcode",
Command: "deadcode",
Pattern: `^deadcode: (?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.*)$`,
InstallFrom: "github.com/tsenart/deadcode",
PartitionStrategy: partitionToMaxArgSize,
defaultEnabled: true,
},
"dupl": {
Name: "dupl",
Command: `dupl -plumbing -threshold {duplthreshold}`,
Pattern: `^(?P<path>.*?\.go):(?P<line>\d+)-\d+:\s*(?P<message>.*)$`,
InstallFrom: "github.com/mibk/dupl",
PartitionStrategy: partitionToMaxArgSizeWithFileGlobs,
IsFast: true,
},
"errcheck": {
Name: "errcheck",
Command: `errcheck -abspath`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "github.com/kisielk/errcheck",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
defaultEnabled: true,
},
"gas": {
Name: "gas",
Command: `gas -fmt=csv`,
Pattern: `^(?P<path>.*?\.go),(?P<line>\d+),(?P<message>[^,]+,[^,]+,[^,]+)`,
InstallFrom: "github.com/GoASTScanner/gas",
PartitionStrategy: partitionToMaxArgSize,
defaultEnabled: true,
IsFast: true,
},
"goconst": {
Name: "goconst",
Command: `goconst -min-occurrences {min_occurrences} -min-length {min_const_length}`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "github.com/jgautheron/goconst/cmd/goconst",
PartitionStrategy: partitionToMaxArgSize,
defaultEnabled: true,
IsFast: true,
},
"gocyclo": {
Name: "gocyclo",
Command: `gocyclo -over {mincyclo}`,
Pattern: `^(?P<cyclo>\d+)\s+\S+\s(?P<function>\S+)\s+(?P<path>.*?\.go):(?P<line>\d+):(\d+)$`,
InstallFrom: "github.com/alecthomas/gocyclo",
PartitionStrategy: partitionToMaxArgSize,
defaultEnabled: true,
IsFast: true,
},
"gofmt": {
Name: "gofmt",
Command: `gofmt -l -s`,
Pattern: `^(?P<path>.*?\.go)$`,
PartitionStrategy: partitionToMaxArgSizeWithFileGlobs,
IsFast: true,
},
"goimports": {
Name: "goimports",
Command: `goimports -l`,
Pattern: `^(?P<path>.*?\.go)$`,
InstallFrom: "golang.org/x/tools/cmd/goimports",
PartitionStrategy: partitionToMaxArgSizeWithFileGlobs,
IsFast: true,
},
"golint": {
Name: "golint",
Command: `golint -min_confidence {min_confidence}`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "github.com/golang/lint/golint",
PartitionStrategy: partitionToMaxArgSize,
defaultEnabled: true,
IsFast: true,
},
"gosimple": {
Name: "gosimple",
Command: `gosimple`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "honnef.co/go/tools/cmd/gosimple",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
},
"gotype": {
Name: "gotype",
Command: `gotype -e {tests=-t}`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "golang.org/x/tools/cmd/gotype",
PartitionStrategy: partitionToMaxArgSize,
defaultEnabled: true,
IsFast: true,
},
"ineffassign": {
Name: "ineffassign",
Command: `ineffassign -n`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "github.com/gordonklaus/ineffassign",
PartitionStrategy: partitionToMaxArgSize,
defaultEnabled: true,
IsFast: true,
},
"interfacer": {
Name: "interfacer",
Command: `interfacer`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "github.com/mvdan/interfacer/cmd/interfacer",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
defaultEnabled: true,
},
"lll": {
Name: "lll",
Command: `lll -g -l {maxlinelength}`,
Pattern: `PATH:LINE:MESSAGE`,
InstallFrom: "github.com/walle/lll/cmd/lll",
PartitionStrategy: partitionToMaxArgSizeWithFileGlobs,
IsFast: true,
},
"megacheck": {
Name: "megacheck",
Command: `megacheck`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "honnef.co/go/tools/cmd/megacheck",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
defaultEnabled: true,
},
"misspell": {
Name: "misspell",
Command: `misspell -j 1`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "github.com/client9/misspell/cmd/misspell",
PartitionStrategy: partitionToMaxArgSizeWithFileGlobs,
IsFast: true,
},
"safesql": {
Name: "safesql",
Command: `safesql`,
Pattern: `^- (?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+)$`,
InstallFrom: "github.com/stripe/safesql",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
},
"staticcheck": {
Name: "staticcheck",
Command: `staticcheck`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "honnef.co/go/tools/cmd/staticcheck",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
},
"structcheck": {
Name: "structcheck",
Command: `structcheck {tests=-t}`,
Pattern: `^(?:[^:]+: )?(?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.+)$`,
InstallFrom: "github.com/opennota/check/cmd/structcheck",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
defaultEnabled: true,
},
"test": {
Name: "test",
Command: `go test`,
Pattern: `^--- FAIL: .*$\s+(?P<path>.*?\.go):(?P<line>\d+): (?P<message>.*)$`,
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
},
"testify": {
Name: "testify",
Command: `go test`,
Pattern: `Location:\s+(?P<path>.*?\.go):(?P<line>\d+)$\s+Error:\s+(?P<message>[^\n]+)`,
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
},
"unconvert": {
Name: "unconvert",
Command: `unconvert`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "github.com/mdempsky/unconvert",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
defaultEnabled: true,
},
"unparam": {
Name: "unparam",
Command: `unparam`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "github.com/mvdan/unparam",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
},
"unused": {
Name: "unused",
Command: `unused`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "honnef.co/go/tools/cmd/unused",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
},
"varcheck": {
Name: "varcheck",
Command: `varcheck`,
Pattern: `^(?:[^:]+: )?(?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.*)$`,
InstallFrom: "github.com/opennota/check/cmd/varcheck",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
defaultEnabled: true,
},
"vet": {
Name: "vet",
Command: `go tool vet`,
Pattern: vetPattern,
PartitionStrategy: partitionToPackageFileGlobs,
defaultEnabled: true,
IsFast: true,
},
"vetshadow": {
Name: "vetshadow",
Command: `go tool vet --shadow`,
Pattern: vetPattern,
PartitionStrategy: partitionToPackageFileGlobs,
defaultEnabled: true,
IsFast: true,
},
}

View File

@@ -5,32 +5,18 @@ import (
"encoding/json"
"fmt"
"os"
"os/exec"
"os/user"
"path/filepath"
"reflect"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"text/template"
"time"
"github.com/google/shlex"
"gopkg.in/alecthomas/kingpin.v3-unstable"
)
// Severity of linter message.
type Severity string
// Linter message severity levels.
const ( // nolint
Warning Severity = "warning"
Error Severity = "error"
)
var (
// Locations to look for vendored linters.
vendoredSearchPaths = [][]string{
@@ -39,129 +25,43 @@ var (
}
)
type Linter struct {
Name string `json:"name"`
Command string `json:"command"`
CompositeCommand string `json:"composite_command,omitempty"`
Pattern string `json:"pattern"`
InstallFrom string `json:"install_from"`
SeverityOverride Severity `json:"severity,omitempty"`
MessageOverride string `json:"message_override,omitempty"`
regex *regexp.Regexp
}
func (l *Linter) MarshalJSON() ([]byte, error) {
return json.Marshal(l.Name)
}
func (l *Linter) String() string {
return l.Name
}
func LinterFromName(name string) *Linter {
s := linterDefinitions[name]
parts := strings.SplitN(s, ":", 2)
if len(parts) < 2 {
kingpin.Fatalf("invalid linter: %q", name)
}
pattern := parts[1]
if p, ok := predefinedPatterns[pattern]; ok {
pattern = p
}
re, err := regexp.Compile("(?m:" + pattern + ")")
kingpin.FatalIfError(err, "invalid regex for %q", name)
return &Linter{
Name: name,
Command: s[0:strings.Index(s, ":")],
Pattern: pattern,
InstallFrom: installMap[name],
SeverityOverride: Severity(config.Severity[name]),
MessageOverride: config.MessageOverride[name],
regex: re,
}
}
type sortedIssues struct {
issues []*Issue
order []string
}
func (s *sortedIssues) Len() int { return len(s.issues) }
func (s *sortedIssues) Swap(i, j int) { s.issues[i], s.issues[j] = s.issues[j], s.issues[i] }
// nolint: gocyclo
func (s *sortedIssues) Less(i, j int) bool {
l, r := s.issues[i], s.issues[j]
for _, key := range s.order {
switch key {
case "path":
if l.Path > r.Path {
return false
}
case "line":
if l.Line > r.Line {
return false
}
case "column":
if l.Col > r.Col {
return false
}
case "severity":
if l.Severity > r.Severity {
return false
}
case "message":
if l.Message > r.Message {
return false
}
case "linter":
if l.Linter.Name > r.Linter.Name {
return false
}
}
}
return true
}
func init() {
kingpin.Flag("config", "Load JSON configuration from file.").Action(loadConfig).String()
kingpin.Flag("disable", "Disable previously enabled linters.").PlaceHolder("LINTER").Short('D').Action(disableAction).Strings()
kingpin.Flag("enable", "Enable previously disabled linters.").PlaceHolder("LINTER").Short('E').Action(enableAction).Strings()
kingpin.Flag("linter", "Define a linter.").PlaceHolder("NAME:COMMAND:PATTERN").StringMapVar(&config.Linters)
kingpin.Flag("message-overrides", "Override message from linter. {message} will be expanded to the original message.").PlaceHolder("LINTER:MESSAGE").StringMapVar(&config.MessageOverride)
kingpin.Flag("severity", "Map of linter severities.").PlaceHolder("LINTER:SEVERITY").StringMapVar(&config.Severity)
kingpin.Flag("disable-all", "Disable all linters.").Action(disableAllAction).Bool()
kingpin.Flag("enable-all", "Enable all linters.").Action(enableAllAction).Bool()
kingpin.Flag("format", "Output format.").PlaceHolder(config.Format).StringVar(&config.Format)
kingpin.Flag("vendored-linters", "Use vendored linters (recommended).").BoolVar(&config.VendoredLinters)
kingpin.Flag("fast", "Only run fast linters.").BoolVar(&config.Fast)
kingpin.Flag("install", "Attempt to install all known linters.").Short('i').BoolVar(&config.Install)
kingpin.Flag("update", "Pass -u to go tool when installing.").Short('u').BoolVar(&config.Update)
kingpin.Flag("force", "Pass -f to go tool when installing.").Short('f').BoolVar(&config.Force)
kingpin.Flag("download-only", "Pass -d to go tool when installing.").BoolVar(&config.DownloadOnly)
kingpin.Flag("debug", "Display messages for failed linters, etc.").Short('d').BoolVar(&config.Debug)
kingpin.Flag("concurrency", "Number of concurrent linters to run.").PlaceHolder(fmt.Sprintf("%d", runtime.NumCPU())).Short('j').IntVar(&config.Concurrency)
kingpin.Flag("exclude", "Exclude messages matching these regular expressions.").Short('e').PlaceHolder("REGEXP").StringsVar(&config.Exclude)
kingpin.Flag("include", "Include messages matching these regular expressions.").Short('I').PlaceHolder("REGEXP").StringsVar(&config.Include)
kingpin.Flag("skip", "Skip directories with this name when expanding '...'.").Short('s').PlaceHolder("DIR...").StringsVar(&config.Skip)
kingpin.Flag("vendor", "Enable vendoring support (skips 'vendor' directories and sets GO15VENDOREXPERIMENT=1).").BoolVar(&config.Vendor)
kingpin.Flag("cyclo-over", "Report functions with cyclomatic complexity over N (using gocyclo).").PlaceHolder("10").IntVar(&config.Cyclo)
kingpin.Flag("line-length", "Report lines longer than N (using lll).").PlaceHolder("80").IntVar(&config.LineLength)
kingpin.Flag("min-confidence", "Minimum confidence interval to pass to golint.").PlaceHolder(".80").FloatVar(&config.MinConfidence)
kingpin.Flag("min-occurrences", "Minimum occurrences to pass to goconst.").PlaceHolder("3").IntVar(&config.MinOccurrences)
kingpin.Flag("min-const-length", "Minimumum constant length.").PlaceHolder("3").IntVar(&config.MinConstLength)
kingpin.Flag("dupl-threshold", "Minimum token sequence as a clone for dupl.").PlaceHolder("50").IntVar(&config.DuplThreshold)
kingpin.Flag("sort", fmt.Sprintf("Sort output by any of %s.", strings.Join(sortKeys, ", "))).PlaceHolder("none").EnumsVar(&config.Sort, sortKeys...)
kingpin.Flag("tests", "Include test files for linters that support this option").Short('t').BoolVar(&config.Test)
kingpin.Flag("deadline", "Cancel linters if they have not completed within this duration.").PlaceHolder("30s").DurationVar(&config.Deadline)
kingpin.Flag("errors", "Only show errors.").BoolVar(&config.Errors)
kingpin.Flag("json", "Generate structured JSON rather than standard line-based output.").BoolVar(&config.JSON)
kingpin.Flag("checkstyle", "Generate checkstyle XML rather than standard line-based output.").BoolVar(&config.Checkstyle)
kingpin.Flag("enable-gc", "Enable GC for linters (useful on large repositories).").BoolVar(&config.EnableGC)
kingpin.Flag("aggregate", "Aggregate issues reported by several linters.").BoolVar(&config.Aggregate)
kingpin.CommandLine.GetFlag("help").Short('h')
func setupFlags(app *kingpin.Application) {
app.Flag("config", "Load JSON configuration from file.").Action(loadConfig).String()
app.Flag("disable", "Disable previously enabled linters.").PlaceHolder("LINTER").Short('D').Action(disableAction).Strings()
app.Flag("enable", "Enable previously disabled linters.").PlaceHolder("LINTER").Short('E').Action(enableAction).Strings()
app.Flag("linter", "Define a linter.").PlaceHolder("NAME:COMMAND:PATTERN").StringMapVar(&config.Linters)
app.Flag("message-overrides", "Override message from linter. {message} will be expanded to the original message.").PlaceHolder("LINTER:MESSAGE").StringMapVar(&config.MessageOverride)
app.Flag("severity", "Map of linter severities.").PlaceHolder("LINTER:SEVERITY").StringMapVar(&config.Severity)
app.Flag("disable-all", "Disable all linters.").Action(disableAllAction).Bool()
app.Flag("enable-all", "Enable all linters.").Action(enableAllAction).Bool()
app.Flag("format", "Output format.").PlaceHolder(config.Format).StringVar(&config.Format)
app.Flag("vendored-linters", "Use vendored linters (recommended).").BoolVar(&config.VendoredLinters)
app.Flag("fast", "Only run fast linters.").BoolVar(&config.Fast)
app.Flag("install", "Attempt to install all known linters.").Short('i').BoolVar(&config.Install)
app.Flag("update", "Pass -u to go tool when installing.").Short('u').BoolVar(&config.Update)
app.Flag("force", "Pass -f to go tool when installing.").Short('f').BoolVar(&config.Force)
app.Flag("download-only", "Pass -d to go tool when installing.").BoolVar(&config.DownloadOnly)
app.Flag("debug", "Display messages for failed linters, etc.").Short('d').BoolVar(&config.Debug)
app.Flag("concurrency", "Number of concurrent linters to run.").PlaceHolder(fmt.Sprintf("%d", runtime.NumCPU())).Short('j').IntVar(&config.Concurrency)
app.Flag("exclude", "Exclude messages matching these regular expressions.").Short('e').PlaceHolder("REGEXP").StringsVar(&config.Exclude)
app.Flag("include", "Include messages matching these regular expressions.").Short('I').PlaceHolder("REGEXP").StringsVar(&config.Include)
app.Flag("skip", "Skip directories with this name when expanding '...'.").Short('s').PlaceHolder("DIR...").StringsVar(&config.Skip)
app.Flag("vendor", "Enable vendoring support (skips 'vendor' directories and sets GO15VENDOREXPERIMENT=1).").BoolVar(&config.Vendor)
app.Flag("cyclo-over", "Report functions with cyclomatic complexity over N (using gocyclo).").PlaceHolder("10").IntVar(&config.Cyclo)
app.Flag("line-length", "Report lines longer than N (using lll).").PlaceHolder("80").IntVar(&config.LineLength)
app.Flag("min-confidence", "Minimum confidence interval to pass to golint.").PlaceHolder(".80").FloatVar(&config.MinConfidence)
app.Flag("min-occurrences", "Minimum occurrences to pass to goconst.").PlaceHolder("3").IntVar(&config.MinOccurrences)
app.Flag("min-const-length", "Minimumum constant length.").PlaceHolder("3").IntVar(&config.MinConstLength)
app.Flag("dupl-threshold", "Minimum token sequence as a clone for dupl.").PlaceHolder("50").IntVar(&config.DuplThreshold)
app.Flag("sort", fmt.Sprintf("Sort output by any of %s.", strings.Join(sortKeys, ", "))).PlaceHolder("none").EnumsVar(&config.Sort, sortKeys...)
app.Flag("tests", "Include test files for linters that support this option").Short('t').BoolVar(&config.Test)
app.Flag("deadline", "Cancel linters if they have not completed within this duration.").PlaceHolder("30s").DurationVar((*time.Duration)(&config.Deadline))
app.Flag("errors", "Only show errors.").BoolVar(&config.Errors)
app.Flag("json", "Generate structured JSON rather than standard line-based output.").BoolVar(&config.JSON)
app.Flag("checkstyle", "Generate checkstyle XML rather than standard line-based output.").BoolVar(&config.Checkstyle)
app.Flag("enable-gc", "Enable GC for linters (useful on large repositories).").BoolVar(&config.EnableGC)
app.Flag("aggregate", "Aggregate issues reported by several linters.").BoolVar(&config.Aggregate)
app.GetFlag("help").Short('h')
}
func loadConfig(app *kingpin.Application, element *kingpin.ParseElement, ctx *kingpin.ParseContext) error {
@@ -174,9 +74,6 @@ func loadConfig(app *kingpin.Application, element *kingpin.ParseElement, ctx *ki
if err != nil {
return err
}
if config.DeadlineJSONCrutch != "" {
config.Deadline, err = time.ParseDuration(config.DeadlineJSONCrutch)
}
for _, disable := range config.Disable {
for i, enable := range config.Enable {
if enable == disable {
@@ -210,28 +107,13 @@ func disableAllAction(app *kingpin.Application, element *kingpin.ParseElement, c
}
func enableAllAction(app *kingpin.Application, element *kingpin.ParseElement, ctx *kingpin.ParseContext) error {
for linter := range linterDefinitions {
for linter := range defaultLinters {
config.Enable = append(config.Enable, linter)
}
config.EnableAll = true
return nil
}
type Issue struct {
Linter *Linter `json:"linter"`
Severity Severity `json:"severity"`
Path string `json:"path"`
Line int `json:"line"`
Col int `json:"col"`
Message string `json:"message"`
}
func (i *Issue) String() string {
buf := new(bytes.Buffer)
err := formatTemplate.Execute(buf, i)
kingpin.FatalIfError(err, "Invalid output format")
return buf.String()
}
func debug(format string, args ...interface{}) {
if config.Debug {
fmt.Fprintf(os.Stderr, "DEBUG: "+format+"\n", args...)
@@ -244,13 +126,13 @@ func warning(format string, args ...interface{}) {
func formatLinters() string {
w := bytes.NewBuffer(nil)
for name := range linterDefinitions {
linter := LinterFromName(name)
for _, linter := range getDefaultLinters() {
install := "(" + linter.InstallFrom + ")"
if install == "()" {
install = ""
}
fmt.Fprintf(w, " %s %s\n %s\n %s\n", name, install, linter.Command, linter.Pattern)
fmt.Fprintf(w, " %s %s\n %s\n %s\n",
linter.Name, install, linter.Command, linter.Pattern)
}
return w.String()
}
@@ -263,33 +145,11 @@ func formatSeverity() string {
return w.String()
}
type Vars map[string]string
func (v Vars) Copy() Vars {
out := Vars{}
for k, v := range v {
out[k] = v
}
return out
}
func (v Vars) Replace(s string) string {
for k, v := range v {
prefix := regexp.MustCompile(fmt.Sprintf("{%s=([^}]*)}", k))
if v != "" {
s = prefix.ReplaceAllString(s, "$1")
} else {
s = prefix.ReplaceAllString(s, "")
}
s = strings.Replace(s, fmt.Sprintf("{%s}", k), v, -1)
}
return s
}
func main() {
// Linters are by their very nature, short lived, so disable GC.
// Reduced (user) linting time on kingpin from 0.97s to 0.64s.
kingpin.CommandLine.Help = fmt.Sprintf(`Aggregate and normalise the output of a whole bunch of Go linters.
pathsArg := kingpin.Arg("path", "Directories to lint. Defaults to \".\". <path>/... will recurse.").Strings()
app := kingpin.CommandLine
setupFlags(app)
app.Help = fmt.Sprintf(`Aggregate and normalise the output of a whole bunch of Go linters.
PlaceHolder linters:
@@ -301,21 +161,23 @@ Severity override map (default is "warning"):
`, formatLinters(), formatSeverity())
kingpin.Parse()
configureEnvironment()
if config.Install {
if config.VendoredLinters {
configureEnvironmentForInstall()
}
installLinters()
return
}
configureEnvironment()
include, exclude := processConfig(config)
start := time.Now()
paths := expandPaths(*pathsArg, config.Skip)
paths := resolvePaths(*pathsArg, config.Skip)
linters := lintersFromFlags()
linters := lintersFromConfig(config)
issues, errch := runLinters(linters, paths, config.Concurrency, exclude, include)
status := 0
issues, errch := runLinters(linters, paths, *pathsArg, config.Concurrency, exclude, include)
if config.JSON {
status |= outputToJSON(issues)
} else if config.Checkstyle {
@@ -334,14 +196,12 @@ Severity override map (default is "warning"):
// nolint: gocyclo
func processConfig(config *Config) (include *regexp.Regexp, exclude *regexp.Regexp) {
// Move configured linters into linterDefinitions.
for name, definition := range config.Linters {
linterDefinitions[name] = definition
}
tmpl, err := template.New("output").Parse(config.Format)
kingpin.FatalIfError(err, "invalid format %q", config.Format)
formatTemplate = tmpl
// Linters are by their very nature, short lived, so disable GC.
// Reduced (user) linting time on kingpin from 0.97s to 0.64s.
if !config.EnableGC {
_ = os.Setenv("GOGC", "off")
}
@@ -413,74 +273,13 @@ func outputToJSON(issues chan *Issue) int {
return status
}
func runLinters(linters map[string]*Linter, paths, ellipsisPaths []string, concurrency int, exclude *regexp.Regexp, include *regexp.Regexp) (chan *Issue, chan error) {
errch := make(chan error, len(linters)*(len(paths)+len(ellipsisPaths)))
concurrencych := make(chan bool, config.Concurrency)
incomingIssues := make(chan *Issue, 1000000)
directives := newDirectiveParser(paths)
processedIssues := filterIssuesViaDirectives(directives, maybeSortIssues(maybeAggregateIssues(incomingIssues)))
wg := &sync.WaitGroup{}
for _, linter := range linters {
// Recreated in each loop because it is mutated by executeLinter().
vars := Vars{
"duplthreshold": fmt.Sprintf("%d", config.DuplThreshold),
"mincyclo": fmt.Sprintf("%d", config.Cyclo),
"maxlinelength": fmt.Sprintf("%d", config.LineLength),
"min_confidence": fmt.Sprintf("%f", config.MinConfidence),
"min_occurrences": fmt.Sprintf("%d", config.MinOccurrences),
"min_const_length": fmt.Sprintf("%d", config.MinConstLength),
"tests": "",
}
if config.Test {
vars["tests"] = "-t"
}
linterPaths := paths
// Most linters don't exclude vendor paths when recursing, so we don't use ... paths.
if acceptsEllipsis[linter.Name] && !config.Vendor && len(ellipsisPaths) > 0 {
linterPaths = ellipsisPaths
}
for _, path := range linterPaths {
wg.Add(1)
deadline := time.After(config.Deadline)
state := &linterState{
Linter: linter,
issues: incomingIssues,
path: path,
vars: vars.Copy(),
exclude: exclude,
include: include,
deadline: deadline,
}
go func() {
concurrencych <- true
err := executeLinter(state)
if err != nil {
errch <- err
}
<-concurrencych
wg.Done()
}()
}
}
go func() {
wg.Wait()
close(incomingIssues)
close(errch)
}()
return processedIssues, errch
}
// nolint: gocyclo
func expandPaths(paths, skip []string) []string {
func resolvePaths(paths, skip []string) []string {
if len(paths) == 0 {
paths = []string{"."}
return []string{"."}
}
skipMap := map[string]bool{}
for _, name := range skip {
skipMap[name] = true
}
dirs := map[string]bool{}
skipPath := newPathFilter(skip)
dirs := newStringSet()
for _, path := range paths {
if strings.HasSuffix(path, "/...") {
root := filepath.Dir(path)
@@ -490,24 +289,22 @@ func expandPaths(paths, skip []string) []string {
return err
}
base := filepath.Base(p)
skip := skipMap[base] || skipMap[p] || (strings.ContainsAny(base[0:1], "_.") && base != "." && base != "..")
if i.IsDir() {
if skip {
return filepath.SkipDir
}
} else if !skip && strings.HasSuffix(p, ".go") {
dirs[filepath.Clean(filepath.Dir(p))] = true
skip := skipPath(p)
switch {
case i.IsDir() && skip:
return filepath.SkipDir
case !i.IsDir() && !skip && strings.HasSuffix(p, ".go"):
dirs.add(filepath.Clean(filepath.Dir(p)))
}
return nil
})
} else {
dirs[filepath.Clean(path)] = true
dirs.add(filepath.Clean(path))
}
}
out := make([]string, 0, len(dirs))
for d := range dirs {
out = append(out, d)
out := make([]string, 0, dirs.size())
for _, d := range dirs.asSlice() {
out = append(out, relativePackagePath(d))
}
sort.Strings(out)
for _, d := range out {
@@ -516,313 +313,82 @@ func expandPaths(paths, skip []string) []string {
return out
}
func makeInstallCommand(linters ...string) []string {
cmd := []string{"get"}
if config.VendoredLinters {
cmd = []string{"install"}
} else {
if config.Update {
cmd = append(cmd, "-u")
func newPathFilter(skip []string) func(string) bool {
filter := map[string]bool{}
for _, name := range skip {
filter[name] = true
}
return func(path string) bool {
base := filepath.Base(path)
if filter[base] || filter[path] {
return true
}
if config.Force {
cmd = append(cmd, "-f")
}
if config.DownloadOnly {
cmd = append(cmd, "-d")
}
}
if config.Debug {
cmd = append(cmd, "-v")
}
cmd = append(cmd, linters...)
return cmd
}
func installLintersWithOneCommand(targets []string) error {
cmd := makeInstallCommand(targets...)
debug("go %s", strings.Join(cmd, " "))
c := exec.Command("go", cmd...) // nolint: gas
c.Stdout = os.Stdout
c.Stderr = os.Stderr
return c.Run()
}
func installLintersIndividually(targets []string) {
failed := []string{}
for _, target := range targets {
cmd := makeInstallCommand(target)
debug("go %s", strings.Join(cmd, " "))
c := exec.Command("go", cmd...) // nolint: gas
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if err := c.Run(); err != nil {
warning("failed to install %s: %s", target, err)
failed = append(failed, target)
}
}
if len(failed) > 0 {
kingpin.Fatalf("failed to install the following linters: %s", strings.Join(failed, ", "))
return base != "." && base != ".." && strings.ContainsAny(base[0:1], "_.")
}
}
func installLinters() {
names := make([]string, 0, len(installMap))
targets := make([]string, 0, len(installMap))
for name, target := range installMap {
names = append(names, name)
targets = append(targets, target)
func relativePackagePath(dir string) string {
if filepath.IsAbs(dir) || strings.HasPrefix(dir, ".") {
return dir
}
namesStr := strings.Join(names, "\n ")
if config.DownloadOnly {
fmt.Printf("Downloading:\n %s\n", namesStr)
} else {
fmt.Printf("Installing:\n %s\n", namesStr)
}
err := installLintersWithOneCommand(targets)
if err == nil {
return
}
warning("failed to install one or more linters: %s (installing individually)", err)
installLintersIndividually(targets)
// package names must start with a ./
return "./" + dir
}
func maybeAggregateIssues(issues chan *Issue) chan *Issue {
if !config.Aggregate {
return issues
}
return aggregateIssues(issues)
}
func maybeSortIssues(issues chan *Issue) chan *Issue {
if reflect.DeepEqual([]string{"none"}, config.Sort) {
return issues
}
out := make(chan *Issue, 1000000)
sorted := &sortedIssues{
issues: []*Issue{},
order: config.Sort,
}
go func() {
for issue := range issues {
sorted.issues = append(sorted.issues, issue)
}
sort.Sort(sorted)
for _, issue := range sorted.issues {
out <- issue
}
close(out)
}()
return out
}
type linterState struct {
*Linter
path string
issues chan *Issue
vars Vars
exclude *regexp.Regexp
include *regexp.Regexp
deadline <-chan time.Time
}
func (l *linterState) InterpolatedCommand() string {
vars := l.vars.Copy()
if l.ShouldChdir() {
vars["path"] = "."
} else {
vars["path"] = l.path
}
return vars.Replace(l.Command)
}
func (l *linterState) ShouldChdir() bool {
return config.Vendor || !strings.HasSuffix(l.path, "/...") || !strings.Contains(l.Command, "{path}")
}
func parseCommand(dir, command string) (string, []string, error) {
args, err := shlex.Split(command)
if err != nil {
return "", nil, err
}
if len(args) == 0 {
return "", nil, fmt.Errorf("invalid command %q", command)
}
exe, err := exec.LookPath(args[0])
if err != nil {
return "", nil, err
}
out := []string{}
for _, arg := range args[1:] {
if strings.Contains(arg, "*") {
pattern := filepath.Join(dir, arg)
globbed, err := filepath.Glob(pattern)
if err != nil {
return "", nil, err
}
for i, g := range globbed {
if strings.HasPrefix(g, dir+string(filepath.Separator)) {
globbed[i] = g[len(dir)+1:]
}
}
out = append(out, globbed...)
} else {
out = append(out, arg)
}
}
return exe, out, nil
}
func executeLinter(state *linterState) error {
debug("linting with %s: %s (on %s)", state.Name, state.Command, state.path)
start := time.Now()
command := state.InterpolatedCommand()
exe, args, err := parseCommand(state.path, command)
if err != nil {
return err
}
debug("executing %s %q", exe, args)
buf := bytes.NewBuffer(nil)
cmd := exec.Command(exe, args...) // nolint: gas
if state.ShouldChdir() {
cmd.Dir = state.path
}
cmd.Stdout = buf
cmd.Stderr = buf
err = cmd.Start()
if err != nil {
return fmt.Errorf("failed to execute linter %s: %s", command, err)
}
done := make(chan bool)
go func() {
err = cmd.Wait()
done <- true
}()
// Wait for process to complete or deadline to expire.
select {
case <-done:
case <-state.deadline:
err = fmt.Errorf("deadline exceeded by linter %s on %s (try increasing --deadline)",
state.Name, state.path)
kerr := cmd.Process.Kill()
if kerr != nil {
warning("failed to kill %s: %s", state.Name, kerr)
}
return err
}
if err != nil {
debug("warning: %s returned %s", command, err)
}
processOutput(state, buf.Bytes())
elapsed := time.Since(start)
debug("%s linter took %s", state.Name, elapsed)
return nil
}
func (l *linterState) fixPath(path string) string {
lpath := strings.TrimSuffix(l.path, "...")
labspath, _ := filepath.Abs(lpath)
if !l.ShouldChdir() {
path = strings.TrimPrefix(path, lpath)
}
if !filepath.IsAbs(path) {
path, _ = filepath.Abs(filepath.Join(labspath, path))
}
if strings.HasPrefix(path, labspath) {
return filepath.Join(lpath, strings.TrimPrefix(path, labspath))
}
return path
}
func lintersFromFlags() map[string]*Linter {
func lintersFromConfig(config *Config) map[string]*Linter {
out := map[string]*Linter{}
for _, linter := range config.Enable {
out[linter] = LinterFromName(linter)
config.Enable = replaceWithMegacheck(config.Enable, config.EnableAll)
for _, name := range config.Enable {
linter := getLinterByName(name, config.Linters[name])
if config.Fast && !linter.IsFast {
continue
}
out[name] = linter
}
for _, linter := range config.Disable {
delete(out, linter)
}
if config.Fast {
for _, linter := range slowLinters {
delete(out, linter)
}
}
return out
}
// nolint: gocyclo
func processOutput(state *linterState, out []byte) {
re := state.regex
all := re.FindAllSubmatchIndex(out, -1)
debug("%s hits %d: %s", state.Name, len(all), state.Pattern)
for _, indices := range all {
group := [][]byte{}
for i := 0; i < len(indices); i += 2 {
var fragment []byte
if indices[i] != -1 {
fragment = out[indices[i]:indices[i+1]]
}
group = append(group, fragment)
// replaceWithMegacheck checks enabled linters if they duplicate megacheck and
// returns a either a revised list removing those and adding megacheck or an
// unchanged slice. Emits a warning if linters were removed and swapped with
// megacheck.
func replaceWithMegacheck(enabled []string, enableAll bool) []string {
var (
staticcheck,
gosimple,
unused bool
revised []string
)
for _, linter := range enabled {
switch linter {
case "staticcheck":
staticcheck = true
case "gosimple":
gosimple = true
case "unused":
unused = true
case "megacheck":
// Don't add to revised slice, we'll add it later
default:
revised = append(revised, linter)
}
issue := &Issue{Line: 1}
issue.Linter = LinterFromName(state.Name)
for i, name := range re.SubexpNames() {
if group[i] == nil {
continue
}
part := string(group[i])
if name != "" {
state.vars[name] = part
}
switch name {
case "path":
issue.Path = state.fixPath(part)
case "line":
n, err := strconv.ParseInt(part, 10, 32)
kingpin.FatalIfError(err, "line matched invalid integer")
issue.Line = int(n)
case "col":
n, err := strconv.ParseInt(part, 10, 32)
kingpin.FatalIfError(err, "col matched invalid integer")
issue.Col = int(n)
case "message":
issue.Message = part
case "":
}
}
if m, ok := config.MessageOverride[state.Name]; ok {
issue.Message = state.vars.Replace(m)
}
if sev, ok := config.Severity[state.Name]; ok {
issue.Severity = Severity(sev)
} else {
issue.Severity = "warning"
}
if state.exclude != nil && state.exclude.MatchString(issue.String()) {
continue
}
if state.include != nil && !state.include.MatchString(issue.String()) {
continue
}
state.issues <- issue
}
return
if staticcheck && gosimple && unused {
if !enableAll {
warning("staticcheck, gosimple and unused are all set, using megacheck instead")
}
return append(revised, "megacheck")
}
return enabled
}
func findVendoredLinters() string {
gopaths := strings.Split(getGoPath(), string(os.PathListSeparator))
gopaths := getGoPathList()
for _, home := range vendoredSearchPaths {
for _, p := range gopaths {
joined := append([]string{p, "src"}, home...)
@@ -833,7 +399,6 @@ func findVendoredLinters() string {
}
}
return ""
}
// Go 1.8 compatible GOPATH.
@@ -847,62 +412,70 @@ func getGoPath() string {
return path
}
// addPath appends p to paths and returns it if:
// 1. p is not a blank string
// 2. p doesn't already exist in paths
// Otherwise paths is returned unchanged.
func addPath(p string, paths []string) []string {
if p == "" {
return paths
}
for _, path := range paths {
if p == path {
func getGoPathList() []string {
return strings.Split(getGoPath(), string(os.PathListSeparator))
}
// addPath appends path to paths if path does not already exist in paths. Returns
// the new paths.
func addPath(paths []string, path string) []string {
for _, existingpath := range paths {
if path == existingpath {
return paths
}
}
return append(paths, p)
return append(paths, path)
}
// Ensure all "bin" directories from GOPATH exists in PATH, as well as GOBIN if set.
// configureEnvironment adds all `bin/` directories from $GOPATH to $PATH
func configureEnvironment() {
gopaths := strings.Split(getGoPath(), string(os.PathListSeparator))
paths := addGoBinsToPath(getGoPathList())
setEnv("PATH", strings.Join(paths, string(os.PathListSeparator)))
debugPrintEnv()
}
func addGoBinsToPath(gopaths []string) []string {
paths := strings.Split(os.Getenv("PATH"), string(os.PathListSeparator))
gobin := os.Getenv("GOBIN")
if config.VendoredLinters && config.Install {
vendorRoot := findVendoredLinters()
if vendorRoot == "" {
kingpin.Fatalf("could not find vendored linters in GOPATH=%q", getGoPath())
}
debug("found vendored linters at %s, updating environment", vendorRoot)
if gobin == "" {
gobin = filepath.Join(gopaths[0], "bin")
}
// "go install" panics when one GOPATH element is beneath another, so we just set
// our vendor root instead.
gopaths = []string{vendorRoot}
}
for _, p := range gopaths {
paths = addPath(filepath.Join(p, "bin"), paths)
paths = addPath(paths, filepath.Join(p, "bin"))
}
paths = addPath(gobin, paths)
path := strings.Join(paths, string(os.PathListSeparator))
gopath := strings.Join(gopaths, string(os.PathListSeparator))
if err := os.Setenv("PATH", path); err != nil {
warning("setenv PATH: %s", err)
gobin := os.Getenv("GOBIN")
if gobin != "" {
paths = addPath(paths, gobin)
}
return paths
}
// configureEnvironmentForInstall sets GOPATH and GOBIN so that vendored linters
// can be installed
func configureEnvironmentForInstall() {
gopaths := getGoPathList()
vendorRoot := findVendoredLinters()
if vendorRoot == "" {
kingpin.Fatalf("could not find vendored linters in GOPATH=%q", getGoPath())
}
debug("found vendored linters at %s, updating environment", vendorRoot)
gobin := os.Getenv("GOBIN")
if gobin == "" {
gobin = filepath.Join(gopaths[0], "bin")
}
setEnv("GOBIN", gobin)
// "go install" panics when one GOPATH element is beneath another, so set
// GOPATH to the vendor root
setEnv("GOPATH", vendorRoot)
debugPrintEnv()
}
func setEnv(key string, value string) {
if err := os.Setenv(key, value); err != nil {
warning("setenv %s: %s", key, err)
}
}
func debugPrintEnv() {
debug("PATH=%s", os.Getenv("PATH"))
if err := os.Setenv("GOPATH", gopath); err != nil {
warning("setenv GOPATH: %s", err)
}
debug("GOPATH=%s", os.Getenv("GOPATH"))
if err := os.Setenv("GOBIN", gobin); err != nil {
warning("setenv GOBIN: %s", err)
}
debug("GOBIN=%s", os.Getenv("GOBIN"))
}

View File

@@ -0,0 +1,131 @@
package main
import (
"fmt"
"path/filepath"
)
// MaxCommandBytes is the maximum number of bytes used when executing a command
const MaxCommandBytes = 32000
type partitionStrategy func([]string, []string) ([][]string, error)
func pathsToFileGlobs(paths []string) ([]string, error) {
filePaths := []string{}
for _, dir := range paths {
paths, err := filepath.Glob(filepath.Join(dir, "*.go"))
if err != nil {
return nil, err
}
filePaths = append(filePaths, paths...)
}
return filePaths, nil
}
func partitionToMaxArgSize(cmdArgs []string, paths []string) ([][]string, error) {
return partitionToMaxSize(cmdArgs, paths, MaxCommandBytes), nil
}
func partitionToMaxSize(cmdArgs []string, paths []string, maxSize int) [][]string {
partitions := newSizePartitioner(cmdArgs, maxSize)
for _, path := range paths {
partitions.add(path)
}
return partitions.end()
}
type sizePartitioner struct {
base []string
parts [][]string
current []string
size int
max int
}
func newSizePartitioner(base []string, max int) *sizePartitioner {
p := &sizePartitioner{base: base, max: max}
p.new()
return p
}
func (p *sizePartitioner) add(arg string) {
if p.size+len(arg)+1 > p.max {
p.new()
}
p.current = append(p.current, arg)
p.size += len(arg) + 1
}
func (p *sizePartitioner) new() {
p.end()
p.size = 0
p.current = []string{}
for _, arg := range p.base {
p.add(arg)
}
}
func (p *sizePartitioner) end() [][]string {
if len(p.current) > 0 {
p.parts = append(p.parts, p.current)
}
return p.parts
}
func partitionToMaxArgSizeWithFileGlobs(cmdArgs []string, paths []string) ([][]string, error) {
filePaths, err := pathsToFileGlobs(paths)
if err != nil || len(filePaths) == 0 {
return nil, err
}
return partitionToMaxArgSize(cmdArgs, filePaths)
}
func partitionToPackageFileGlobs(cmdArgs []string, paths []string) ([][]string, error) {
parts := [][]string{}
for _, path := range paths {
filePaths, err := pathsToFileGlobs([]string{path})
if err != nil {
return nil, err
}
if len(filePaths) == 0 {
continue
}
parts = append(parts, append(cmdArgs, filePaths...))
}
return parts, nil
}
func partitionToMaxArgSizeWithPackagePaths(cmdArgs []string, paths []string) ([][]string, error) {
packagePaths, err := pathsToPackagePaths(paths)
if err != nil || len(packagePaths) == 0 {
return nil, err
}
return partitionToMaxArgSize(cmdArgs, packagePaths)
}
func pathsToPackagePaths(paths []string) ([]string, error) {
packages := []string{}
for _, path := range paths {
pkg, err := packageNameFromPath(path)
if err != nil {
return nil, err
}
packages = append(packages, pkg)
}
return packages, nil
}
func packageNameFromPath(path string) (string, error) {
if !filepath.IsAbs(path) {
return path, nil
}
for _, gopath := range getGoPathList() {
rel, err := filepath.Rel(filepath.Join(gopath, "src"), path)
if err != nil {
continue
}
return rel, nil
}
return "", fmt.Errorf("%s not in GOPATH", path)
}

View File

@@ -0,0 +1,29 @@
package main
type stringSet struct {
items map[string]struct{}
}
func newStringSet(items ...string) *stringSet {
setItems := make(map[string]struct{}, len(items))
for _, item := range items {
setItems[item] = struct{}{}
}
return &stringSet{items: setItems}
}
func (s *stringSet) add(item string) {
s.items[item] = struct{}{}
}
func (s *stringSet) asSlice() []string {
items := []string{}
for item := range s.items {
items = append(items, item)
}
return items
}
func (s *stringSet) size() int {
return len(s.items)
}

View File

@@ -1,2 +1,4 @@
# ineffassign
Detect ineffectual assignments in Go code.
This tool misses some cases because does not consider any type information in its analysis. (For example, assignments to struct fields are never marked as ineffectual.) It should, however, never give any false positives.

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"os"
"strconv"
@@ -16,7 +17,7 @@ const usageDoc = `goconst: find repeated strings that could be replaced by a con
Usage:
goconst ARGS <directory>
goconst ARGS <directory> [<directory>...]
Flags:
@@ -26,8 +27,8 @@ Flags:
-min-length only report strings with the minimum given length (default: 3)
-match-constant look for existing constants matching the strings
-numbers search also for duplicated numbers
-min minimum value, only works with -numbers
-max maximum value, only works with -numbers
-min minimum value, only works with -numbers
-max maximum value, only works with -numbers
-output output formatting (text or json)
Examples:
@@ -52,17 +53,25 @@ var (
func main() {
flag.Usage = func() {
fmt.Fprint(os.Stderr, usage)
usage(os.Stderr)
}
flag.Parse()
log.SetPrefix("goconst: ")
args := flag.Args()
if len(args) != 1 {
usage()
if len(args) < 1 {
usage(os.Stderr)
os.Exit(1)
}
path := args[0]
for _, path := range args {
if err := run(path); err != nil {
log.Println(err)
os.Exit(1)
}
}
}
func run(path string) error {
gco := goconst.New(
path,
*flagIgnore,
@@ -73,19 +82,17 @@ func main() {
)
strs, consts, err := gco.ParseTree()
if err != nil {
log.Println(err)
os.Exit(1)
return err
}
printOutput(strs, consts, *flagOutput, *flagMinOccurrences, *flagMin, *flagMax)
return printOutput(strs, consts, *flagOutput, *flagMinOccurrences, *flagMin, *flagMax)
}
func usage() {
fmt.Fprintf(os.Stderr, usageDoc)
os.Exit(1)
func usage(out io.Writer) {
fmt.Fprintf(out, usageDoc)
}
func printOutput(strs goconst.Strings, consts goconst.Constants, output string, minOccurrences, min, max int) {
func printOutput(strs goconst.Strings, consts goconst.Constants, output string, minOccurrences, min, max int) error {
for str, item := range strs {
// Filter out items whose occurrences don't match the min value
if len(item) < minOccurrences {
@@ -113,7 +120,7 @@ func printOutput(strs goconst.Strings, consts goconst.Constants, output string,
strs, consts,
})
if err != nil {
log.Fatal(err)
return err
}
case "text":
for str, item := range strs {
@@ -140,8 +147,9 @@ func printOutput(strs goconst.Strings, consts goconst.Constants, output string,
}
}
default:
fmt.Printf(`Unsupported output format: %s`, output)
return fmt.Errorf(`Unsupported output format: %s`, output)
}
return nil
}
func occurrences(item []goconst.ExtendedPos, current goconst.ExtendedPos) string {

View File

@@ -6,6 +6,7 @@
package main
import (
"bytes"
_ "crypto/sha512"
"encoding/json"
"errors"
@@ -43,7 +44,7 @@ func (a *Flags) Set(value string) error {
var (
extraFlags Flags
pkg = flag.String("package", "", "Go package")
verbose = flag.Bool("v", false, "Pass '-v' argument to 'go test'")
verbose = flag.Bool("v", false, "Pass '-v' argument to 'go test' and output to stdout")
debug = flag.Bool("debug", false, "Enable debug output")
coverprof = flag.String("coverprofile", "", "If supplied, use a go cover profile (comma separated)")
covermode = flag.String("covermode", "count", "sent as covermode argument to go test")
@@ -127,19 +128,25 @@ func getCoverage() ([]*SourceFile, error) {
return nil, err
}
f.Close()
cmd := exec.Command("go")
outBuf := new(bytes.Buffer)
cmd.Stdout = outBuf
cmd.Stderr = outBuf
args := []string{"go", "test", "-covermode", *covermode, "-coverprofile", f.Name(), coverpkg}
if *verbose {
args = append(args, "-v")
cmd.Stdout = os.Stdout
}
args = append(args, extraFlags...)
args = append(args, line)
cmd.Args = args
b, err := cmd.CombinedOutput()
err = cmd.Run()
if err != nil {
return nil, fmt.Errorf("%v: %v", err, string(b))
return nil, fmt.Errorf("%v: %v", err, outBuf.String())
}
pfs, err := cover.ParseProfiles(f.Name())
if err != nil {
return nil, err

View File

@@ -11,11 +11,18 @@ import (
"github.com/mvdan/unparam/check"
)
var tests = flag.Bool("tests", true, "include tests")
var (
tests = flag.Bool("tests", true, "include tests")
debug = flag.Bool("debug", false, "debug prints")
)
func main() {
flag.Usage = func() {
fmt.Fprintln(os.Stderr, "usage: unparam [flags] [package ...]")
flag.PrintDefaults()
}
flag.Parse()
warns, err := check.UnusedParams(*tests, flag.Args()...)
warns, err := check.UnusedParams(*tests, *debug, flag.Args()...)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)