golangci-lint/pkg/golinters/gas.go

67 lines
1.5 KiB
Go
Raw Normal View History

2018-05-07 14:02:27 +03:00
package golinters
import (
"context"
"fmt"
2018-05-08 11:54:30 +03:00
"go/token"
2018-05-07 14:02:27 +03:00
"io/ioutil"
"log"
"strconv"
2018-06-02 11:36:50 +03:00
"github.com/golangci/golangci-lint/pkg/lint/linter"
2018-05-07 14:02:27 +03:00
"github.com/golangci/golangci-lint/pkg/result"
"github.com/golangci/gosec"
"github.com/golangci/gosec/rules"
2018-05-07 14:02:27 +03:00
)
type Gosec struct{}
2018-05-07 14:02:27 +03:00
func (Gosec) Name() string {
return "gosec"
2018-05-07 14:02:27 +03:00
}
func (Gosec) Desc() string {
return "Inspects source code for security problems"
}
func (lint Gosec) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
gasConfig := gosec.NewConfig()
enabledRules := rules.Generate()
2018-05-07 14:02:27 +03:00
logger := log.New(ioutil.Discard, "", 0)
analyzer := gosec.NewAnalyzer(gasConfig, logger)
2018-05-07 14:02:27 +03:00
analyzer.LoadRules(enabledRules.Builders())
analyzer.ProcessProgram(lintCtx.Program)
issues, _ := analyzer.Report()
2018-05-29 19:42:49 -04:00
if len(issues) == 0 {
return nil, nil
}
2018-05-07 14:02:27 +03:00
2018-05-29 19:42:49 -04:00
res := make([]result.Issue, 0, len(issues))
2018-05-07 14:02:27 +03:00
for _, i := range issues {
2018-08-18 22:15:24 +03:00
text := fmt.Sprintf("%s: %s", i.RuleID, markIdentifiers(i.What)) // TODO: use severity and confidence
var r *result.Range
line, err := strconv.Atoi(i.Line)
if err != nil {
r = &result.Range{}
if n, rerr := fmt.Sscanf(i.Line, "%d-%d", &r.From, &r.To); rerr != nil || n != 2 {
lintCtx.Log.Warnf("Can't convert gosec line number %q of %v to int: %s", i.Line, i, err)
continue
}
line = r.From
}
2018-05-07 21:44:40 +03:00
res = append(res, result.Issue{
2018-05-08 11:54:30 +03:00
Pos: token.Position{
Filename: i.File,
Line: line,
},
2018-05-07 14:02:27 +03:00
Text: text,
LineRange: r,
2018-05-07 14:02:27 +03:00
FromLinter: lint.Name(),
})
}
return res, nil
}