1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-28 08:49:11 +02:00
revive/lintconfig.go

185 lines
3.7 KiB
Go
Raw Normal View History

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{},
&rule.NamesRule{},
&rule.ElseRule{},
&rule.IfReturnRule{},
&rule.RangeRule{},
&rule.ErrorfRule{},
&rule.ErrorsRule{},
&rule.ErrorStringsRule{},
&rule.ReceiverNameRule{},
&rule.IncrementDecrementRule{},
&rule.ErrorReturnRule{},
&rule.UnexportedReturnRule{},
&rule.TimeNamesRule{},
&rule.ContextKeyTypeRule{},
&rule.ContextArgumentsRule{},
}
var allRules = append([]lint.Rule{
&rule.ArgumentsLimitRule{},
&rule.CyclomaticRule{},
}, defaultRules...)
var allFormatters = []lint.Formatter{
2018-01-28 03:01:18 +02:00
&formatter.CLI{},
&formatter.JSON{},
&formatter.Default{},
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
}
func getFiles() []string {
globs := flag.Args()
if len(globs) == 0 {
2018-02-02 20:11:40 +02:00
fail("files not specified")
2018-01-28 02:22:17 +02:00
}
2018-02-04 22:25:29 +02:00
files, errs := dots.Resolve(globs, strings.Split(excludePaths, " "))
if errs != nil && len(errs) > 0 {
err := ""
for _, e := range errs {
err += e.Error() + "\n"
}
fail(err)
2018-01-28 02:22:17 +02:00
}
2018-02-04 04:56:45 +02:00
return files
2018-01-28 02:22:17 +02:00
}
var configPath string
2018-02-04 04:56:45 +02:00
var excludePaths string
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 (
configUsage = "path to the configuration TOML file"
excludeUsage = "glob which specifies files to be excluded"
formatterUsage = "formatter to be used for the output"
)
flag.StringVar(&configPath, "config", "", configUsage)
2018-02-04 04:56:45 +02:00
flag.StringVar(&excludePaths, "exclude", "", excludeUsage)
2018-01-28 02:22:17 +02:00
flag.StringVar(&formatterName, "formatter", "", formatterUsage)
flag.Parse()
}