1
0
mirror of https://github.com/mgechev/revive.git synced 2025-07-05 00:28:53 +02:00

Basic configuration

This commit is contained in:
mgechev
2018-01-26 20:20:49 -08:00
parent 0b310191f2
commit 9f437c09a5
4 changed files with 69 additions and 16 deletions

12
config.toml Normal file
View File

@ -0,0 +1,12 @@
severity = "error"
confidence = 1.0
[rules]
[rules.else]
arguments = []
severity = "error"
[rules.argument-limit]
arguments = ['2']
severity = "warning"
[rules.names]
arguments = []
severity = "error"

View File

@ -51,8 +51,8 @@ type Arguments = []string
// RuleConfig is type used for the rule configuration. // RuleConfig is type used for the rule configuration.
type RuleConfig struct { type RuleConfig struct {
Arguments Arguments Arguments Arguments `toml:"arguments"`
Severity Severity Severity Severity `toml:"severity"`
} }
// RulesConfig defines the config for all rules. // RulesConfig defines the config for all rules.
@ -60,9 +60,9 @@ type RulesConfig = map[string]RuleConfig
// Config defines the config of the linter. // Config defines the config of the linter.
type Config struct { type Config struct {
Confidence float64 Confidence float64 `toml:"confidence"`
Severity Severity Severity Severity `toml:"severity"`
Rules RulesConfig Rules RulesConfig `toml:"rules"`
} }
// Rule defines an abstract rule interaface // Rule defines an abstract rule interaface

61
main.go
View File

@ -2,13 +2,38 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"github.com/BurntSushi/toml"
"github.com/mgechev/revive/formatter" "github.com/mgechev/revive/formatter"
"github.com/mgechev/revive/lint" "github.com/mgechev/revive/lint"
"github.com/mgechev/revive/rule" "github.com/mgechev/revive/rule"
) )
var allRules = []lint.Rule{
&rule.ArgumentsLimitRule{},
&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{},
}
func main() { func main() {
src := ` src := `
package p package p
@ -28,17 +53,33 @@ func main() {
revive := lint.New(func(file string) ([]byte, error) { revive := lint.New(func(file string) ([]byte, error) {
return []byte(src), nil return []byte(src), nil
}) })
var result []lint.Rule
result = append(result, &rule.ElseRule{}, &rule.ArgumentsLimitRule{}, &rule.NamesRule{})
var config = lint.RulesConfig{ config := &lint.Config{}
"argument-limit": lint.RuleConfig{
Arguments: []string{"3"}, file, err := ioutil.ReadFile("config.toml")
Severity: lint.SeverityError, if err != nil {
}, panic("cannot read the config file")
}
_, err = toml.Decode(string(file), config)
if err != nil {
panic("cannot parse the config file: " + err.Error())
} }
failures, err := revive.Lint([]string{"foo.go", "bar.go", "baz.go"}, result, config) 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 {
panic("cannot find rule: " + name)
}
lintingRules = append(lintingRules, rule)
}
failures, err := revive.Lint([]string{"foo.go", "bar.go", "baz.go"}, lintingRules, config.Rules)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -49,7 +90,7 @@ func main() {
go (func() { go (func() {
var formatter formatter.CLIFormatter var formatter formatter.CLIFormatter
output, err = formatter.Format(formatChan, config) output, err = formatter.Format(formatChan, config.Rules)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -61,7 +102,7 @@ func main() {
if exitCode == 0 { if exitCode == 0 {
exitCode = 1 exitCode = 1
} }
if c, ok := config[f.RuleName]; ok && c.Severity == lint.SeverityError { if c, ok := config.Rules[f.RuleName]; ok && c.Severity == lint.SeverityError {
exitCode = 2 exitCode = 2
} }
formatChan <- f formatChan <- f

View File

@ -47,7 +47,7 @@ func (r *NamesRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Fail
// Name returns the rule name. // Name returns the rule name.
func (r *NamesRule) Name() string { func (r *NamesRule) Name() string {
return "imports" return "names"
} }
func checkList(fl *ast.FieldList, thing string, w *lintNames) { func checkList(fl *ast.FieldList, thing string, w *lintNames) {