1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-24 08:32:22 +02:00
revive/config.go

268 lines
5.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"
"path/filepath"
2018-01-28 02:22:17 +02:00
"strings"
2018-02-04 04:56:45 +02:00
"github.com/mgechev/dots"
"github.com/mitchellh/go-homedir"
2018-02-04 04:56:45 +02:00
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{},
&rule.EmptyBlockRule{},
2018-06-08 23:22:21 +02:00
&rule.SuperfluousElseRule{},
&rule.ConfusingNamingRule{},
&rule.GetReturnRule{},
&rule.ModifiesParamRule{},
&rule.ConfusingResultsRule{},
&rule.DeepExitRule{},
&rule.UnusedParamRule{},
&rule.UnreachableCodeRule{},
2018-07-17 21:21:27 +02:00
&rule.AddConstantRule{},
&rule.FlagParamRule{},
&rule.UnnecessaryStmtRule{},
2018-07-28 18:07:31 +02:00
&rule.StructTagRule{},
&rule.ModifiesValRecRule{},
&rule.ConstantLogicalExprRule{},
&rule.BoolLiteralRule{},
2018-09-14 04:19:49 +02:00
&rule.RedefinesBuiltinIDRule{},
&rule.ImportsBlacklistRule{},
&rule.FunctionResultsLimitRule{},
&rule.MaxPublicStructsRule{},
&rule.RangeValInClosureRule{},
&rule.RangeValAddress{},
&rule.WaitGroupByValueRule{},
&rule.AtomicRule{},
&rule.EmptyLinesRule{},
&rule.LineLengthLimitRule{},
2018-10-31 16:32:23 +02:00
&rule.CallToGCRule{},
&rule.DuplicatedImportsRule{},
&rule.ImportShadowingRule{},
&rule.BareReturnRule{},
&rule.UnusedReceiverRule{},
&rule.UnhandledErrorRule{},
2019-12-14 09:27:06 +02:00
&rule.CognitiveComplexityRule{},
&rule.StringOfIntRule{},
&rule.EarlyReturnRule{},
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{},
&formatter.NDJSON{},
2018-01-28 03:01:18 +02:00
&formatter.Default{},
2018-09-17 20:57:56 +02:00
&formatter.Unix{},
&formatter.Checkstyle{},
&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
}
for k, v := range config.Directives {
if v.Severity == "" {
v.Severity = severity
}
config.Directives[k] = v
}
2018-01-28 02:22:17 +02:00
}
}
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 buildDefaultConfigPath() string {
var result string
if homeDir, err := homedir.Dir(); err == nil {
result = filepath.Join(homeDir, "revive.toml")
if _, err := os.Stat(result); err != nil {
result = ""
}
}
return result
}
2018-01-28 02:22:17 +02:00
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 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 {
globs := normalizeSplit(flag.Args())
2018-01-28 02:22:17 +02:00
if len(globs) == 0 {
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
}
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
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()
}
// command line help strings
2018-01-28 02:22:17 +02:00
const (
configUsage = "path to the configuration TOML file, defaults to $HOME/revive.toml, if present (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
)
defaultConfigPath := buildDefaultConfigPath()
flag.StringVar(&configPath, "config", defaultConfigPath, configUsage)
flag.Var(&excludePaths, "exclude", excludeUsage)
2018-01-28 02:22:17 +02:00
flag.StringVar(&formatterName, "formatter", "", formatterUsage)
flag.Parse()
}