2018-01-28 02:22:17 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2018-02-02 20:11:40 +02:00
|
|
|
"os"
|
2018-01-28 02:22:17 +02:00
|
|
|
"strings"
|
|
|
|
|
2018-02-04 04:56:45 +02:00
|
|
|
"github.com/mgechev/dots"
|
|
|
|
|
2018-01-28 02:22:17 +02:00
|
|
|
"github.com/mgechev/revive/formatter"
|
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
"github.com/mgechev/revive/lint"
|
|
|
|
"github.com/mgechev/revive/rule"
|
|
|
|
)
|
|
|
|
|
2018-02-02 20:11:40 +02:00
|
|
|
func fail(err string) {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-01-28 02:22:17 +02:00
|
|
|
var defaultRules = []lint.Rule{
|
|
|
|
&rule.VarDeclarationsRule{},
|
|
|
|
&rule.PackageCommentsRule{},
|
|
|
|
&rule.DotImportsRule{},
|
|
|
|
&rule.BlankImportsRule{},
|
|
|
|
&rule.ExportedRule{},
|
2018-05-27 06:28:31 +02:00
|
|
|
&rule.VarNamingRule{},
|
2018-05-27 01:14:36 +02:00
|
|
|
&rule.IndentErrorFlowRule{},
|
2018-01-28 02:22:17 +02:00
|
|
|
&rule.IfReturnRule{},
|
|
|
|
&rule.RangeRule{},
|
|
|
|
&rule.ErrorfRule{},
|
2018-05-27 06:28:31 +02:00
|
|
|
&rule.ErrorNamingRule{},
|
2018-01-28 02:22:17 +02:00
|
|
|
&rule.ErrorStringsRule{},
|
2018-05-27 06:28:31 +02:00
|
|
|
&rule.ReceiverNamingRule{},
|
2018-01-28 02:22:17 +02:00
|
|
|
&rule.IncrementDecrementRule{},
|
|
|
|
&rule.ErrorReturnRule{},
|
|
|
|
&rule.UnexportedReturnRule{},
|
2018-05-27 06:28:31 +02:00
|
|
|
&rule.TimeNamingRule{},
|
|
|
|
&rule.ContextKeysType{},
|
|
|
|
&rule.ContextAsArgumentRule{},
|
2018-01-28 02:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var allRules = append([]lint.Rule{
|
|
|
|
&rule.ArgumentsLimitRule{},
|
|
|
|
&rule.CyclomaticRule{},
|
2018-05-27 06:28:31 +02:00
|
|
|
&rule.FileHeaderRule{},
|
2018-06-08 21:41:49 +02:00
|
|
|
&rule.EmptyBlockRule{},
|
2018-06-08 23:22:21 +02:00
|
|
|
&rule.SuperfluousElseRule{},
|
2018-07-02 04:05:20 +02:00
|
|
|
&rule.ConfusingNamingRule{},
|
2018-06-22 13:21:09 +02:00
|
|
|
&rule.GetReturnRule{},
|
2018-06-24 09:26:21 +02:00
|
|
|
&rule.ModifiesParamRule{},
|
2018-07-02 04:09:58 +02:00
|
|
|
&rule.ConfusingResultsRule{},
|
2018-06-26 22:21:03 +02:00
|
|
|
&rule.DeepExitRule{},
|
2018-07-07 10:40:02 +02:00
|
|
|
&rule.UnusedParamRule{},
|
2018-07-16 23:23:47 +02:00
|
|
|
&rule.UnreachableCodeRule{},
|
2018-07-17 21:21:27 +02:00
|
|
|
&rule.AddConstantRule{},
|
2018-07-22 07:58:48 +02:00
|
|
|
&rule.FlagParamRule{},
|
2018-07-28 07:38:39 +02:00
|
|
|
&rule.UnnecessaryStmtRule{},
|
2018-07-28 18:07:31 +02:00
|
|
|
&rule.StructTagRule{},
|
2018-08-14 00:18:28 +02:00
|
|
|
&rule.ModifiesValRecRule{},
|
2018-08-23 20:45:10 +02:00
|
|
|
&rule.ConstantLogicalExprRule{},
|
2018-08-23 20:10:17 +02:00
|
|
|
&rule.BoolLiteralRule{},
|
2018-09-14 04:19:49 +02:00
|
|
|
&rule.RedefinesBuiltinIDRule{},
|
2018-09-17 22:06:42 +02:00
|
|
|
&rule.ImportsBlacklistRule{},
|
2018-09-17 11:06:53 +02:00
|
|
|
&rule.FunctionResultsLimitRule{},
|
2018-09-18 17:19:25 +02:00
|
|
|
&rule.MaxPublicStructsRule{},
|
2018-09-29 05:32:32 +02:00
|
|
|
&rule.RangeValInClosureRule{},
|
2018-10-03 10:01:41 +02:00
|
|
|
&rule.WaitGroupByValueRule{},
|
2018-10-03 08:12:19 +02:00
|
|
|
&rule.AtomicRule{},
|
2018-10-11 23:52:46 +02:00
|
|
|
&rule.EmptyLinesRule{},
|
2018-10-19 14:18:33 +02:00
|
|
|
&rule.LineLengthLimitRule{},
|
2018-10-31 16:32:23 +02:00
|
|
|
&rule.CallToGCRule{},
|
2019-03-20 20:54:03 +02:00
|
|
|
&rule.DuplicatedImportsRule{},
|
2019-03-27 20:46:20 +02:00
|
|
|
&rule.ImportShadowingRule{},
|
2019-04-17 22:55:52 +02:00
|
|
|
&rule.BareReturnRule{},
|
2019-04-18 19:35:51 +02:00
|
|
|
&rule.UnusedReceiverRule{},
|
2019-04-28 04:23:17 +02:00
|
|
|
&rule.UnhandledErrorRule{},
|
2018-01-28 02:22:17 +02:00
|
|
|
}, defaultRules...)
|
|
|
|
|
|
|
|
var allFormatters = []lint.Formatter{
|
2018-05-26 21:08:02 +02:00
|
|
|
&formatter.Stylish{},
|
2018-05-26 22:47:13 +02:00
|
|
|
&formatter.Friendly{},
|
2018-01-28 03:01:18 +02:00
|
|
|
&formatter.JSON{},
|
2018-07-14 00:01:27 +02:00
|
|
|
&formatter.NDJSON{},
|
2018-01-28 03:01:18 +02:00
|
|
|
&formatter.Default{},
|
2018-09-17 20:57:56 +02:00
|
|
|
&formatter.Unix{},
|
2018-07-15 21:45:15 +02:00
|
|
|
&formatter.Checkstyle{},
|
2018-10-31 01:07:32 +02:00
|
|
|
&formatter.Plain{},
|
2018-01-28 02:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getFormatters() map[string]lint.Formatter {
|
|
|
|
result := map[string]lint.Formatter{}
|
|
|
|
for _, f := range allFormatters {
|
|
|
|
result[f.Name()] = f
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLintingRules(config *lint.Config) []lint.Rule {
|
|
|
|
rulesMap := map[string]lint.Rule{}
|
|
|
|
for _, r := range allRules {
|
|
|
|
rulesMap[r.Name()] = r
|
|
|
|
}
|
|
|
|
|
|
|
|
lintingRules := []lint.Rule{}
|
|
|
|
for name := range config.Rules {
|
|
|
|
rule, ok := rulesMap[name]
|
|
|
|
if !ok {
|
2018-02-02 20:11:40 +02:00
|
|
|
fail("cannot find rule: " + name)
|
2018-01-28 02:22:17 +02:00
|
|
|
}
|
|
|
|
lintingRules = append(lintingRules, rule)
|
|
|
|
}
|
|
|
|
|
|
|
|
return lintingRules
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseConfig(path string) *lint.Config {
|
|
|
|
config := &lint.Config{}
|
|
|
|
file, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
2018-02-02 20:11:40 +02:00
|
|
|
fail("cannot read the config file")
|
2018-01-28 02:22:17 +02:00
|
|
|
}
|
|
|
|
_, err = toml.Decode(string(file), config)
|
|
|
|
if err != nil {
|
2018-02-02 20:11:40 +02:00
|
|
|
fail("cannot parse the config file: " + err.Error())
|
2018-01-28 02:22:17 +02:00
|
|
|
}
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
func normalizeConfig(config *lint.Config) {
|
2018-01-28 03:01:18 +02:00
|
|
|
if config.Confidence == 0 {
|
|
|
|
config.Confidence = 0.8
|
|
|
|
}
|
2018-01-28 02:22:17 +02:00
|
|
|
severity := config.Severity
|
|
|
|
if severity != "" {
|
|
|
|
for k, v := range config.Rules {
|
|
|
|
if v.Severity == "" {
|
|
|
|
v.Severity = severity
|
|
|
|
}
|
|
|
|
config.Rules[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getConfig() *lint.Config {
|
|
|
|
config := defaultConfig()
|
|
|
|
if configPath != "" {
|
|
|
|
config = parseConfig(configPath)
|
|
|
|
}
|
|
|
|
normalizeConfig(config)
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFormatter() lint.Formatter {
|
|
|
|
formatters := getFormatters()
|
2018-01-28 03:01:18 +02:00
|
|
|
formatter := formatters["default"]
|
2018-01-28 02:22:17 +02:00
|
|
|
if formatterName != "" {
|
|
|
|
f, ok := formatters[formatterName]
|
|
|
|
if !ok {
|
2018-02-02 20:11:40 +02:00
|
|
|
fail("unknown formatter " + formatterName)
|
2018-01-28 02:22:17 +02:00
|
|
|
}
|
|
|
|
formatter = f
|
|
|
|
}
|
|
|
|
return formatter
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultConfig() *lint.Config {
|
|
|
|
defaultConfig := lint.Config{
|
|
|
|
Confidence: 0.0,
|
|
|
|
Severity: lint.SeverityWarning,
|
|
|
|
Rules: map[string]lint.RuleConfig{},
|
|
|
|
}
|
|
|
|
for _, r := range defaultRules {
|
|
|
|
defaultConfig.Rules[r.Name()] = lint.RuleConfig{}
|
|
|
|
}
|
|
|
|
return &defaultConfig
|
|
|
|
}
|
|
|
|
|
2018-02-04 22:54:37 +02:00
|
|
|
func normalizeSplit(strs []string) []string {
|
|
|
|
res := []string{}
|
|
|
|
for _, s := range strs {
|
|
|
|
t := strings.Trim(s, " \t")
|
|
|
|
if len(t) > 0 {
|
|
|
|
res = append(res, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2018-06-01 04:42:58 +02:00
|
|
|
func getPackages() [][]string {
|
2018-02-04 22:54:37 +02:00
|
|
|
globs := normalizeSplit(flag.Args())
|
2018-01-28 02:22:17 +02:00
|
|
|
if len(globs) == 0 {
|
2018-05-31 03:31:38 +02:00
|
|
|
globs = append(globs, ".")
|
2018-01-28 02:22:17 +02:00
|
|
|
}
|
|
|
|
|
2018-06-01 04:42:58 +02:00
|
|
|
packages, err := dots.ResolvePackages(globs, normalizeSplit(excludePaths))
|
2018-05-31 02:03:27 +02:00
|
|
|
if err != nil {
|
|
|
|
fail(err.Error())
|
2018-01-28 02:22:17 +02:00
|
|
|
}
|
|
|
|
|
2018-06-01 04:42:58 +02:00
|
|
|
return packages
|
2018-01-28 02:22:17 +02:00
|
|
|
}
|
|
|
|
|
2018-02-04 22:54:37 +02:00
|
|
|
type arrayFlags []string
|
|
|
|
|
|
|
|
func (i *arrayFlags) String() string {
|
|
|
|
return strings.Join([]string(*i), " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *arrayFlags) Set(value string) error {
|
|
|
|
*i = append(*i, value)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-28 02:22:17 +02:00
|
|
|
var configPath string
|
2018-02-04 22:54:37 +02:00
|
|
|
var excludePaths arrayFlags
|
2018-01-28 02:22:17 +02:00
|
|
|
var formatterName string
|
|
|
|
var help bool
|
|
|
|
|
|
|
|
var originalUsage = flag.Usage
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.Usage = func() {
|
|
|
|
fmt.Println(banner)
|
|
|
|
originalUsage()
|
|
|
|
}
|
|
|
|
const (
|
2018-02-04 22:54:37 +02:00
|
|
|
configUsage = "path to the configuration TOML file (i.e. -config myconf.toml)"
|
|
|
|
excludeUsage = "list of globs which specify files to be excluded (i.e. -exclude foo/...)"
|
2018-05-26 21:08:02 +02:00
|
|
|
formatterUsage = "formatter to be used for the output (i.e. -formatter stylish)"
|
2018-01-28 02:22:17 +02:00
|
|
|
)
|
|
|
|
flag.StringVar(&configPath, "config", "", configUsage)
|
2018-02-04 22:54:37 +02:00
|
|
|
flag.Var(&excludePaths, "exclude", excludeUsage)
|
2018-01-28 02:22:17 +02:00
|
|
|
flag.StringVar(&formatterName, "formatter", "", formatterUsage)
|
|
|
|
flag.Parse()
|
|
|
|
}
|