1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-24 08:32:22 +02:00
revive/main.go
2018-01-25 11:47:39 -08:00

75 lines
1.3 KiB
Go

package main
import (
"fmt"
"os"
"github.com/mgechev/revive/formatter"
"github.com/mgechev/revive/lint"
"github.com/mgechev/revive/rule"
)
func main() {
src := `
package p
func Test() {
if true {
return 42;
} else {
return 23;
}
}
func foo_bar(a int, b int, c int, d int) {
return a + b + c;
}`
revive := lint.New(func(file string) ([]byte, error) {
return []byte(src), nil
})
var result []lint.Rule
result = append(result, &rule.ElseRule{}, &rule.ArgumentsLimitRule{}, &rule.NamesRule{})
var config = lint.RulesConfig{
"argument-limit": lint.RuleConfig{
Arguments: []string{"3"},
Severity: lint.SeverityError,
},
}
failures, err := revive.Lint([]string{"foo.go", "bar.go", "baz.go"}, result, config)
if err != nil {
panic(err)
}
formatChan := make(chan lint.Failure)
exitChan := make(chan bool)
var output string
go (func() {
var formatter formatter.CLIFormatter
output, err = formatter.Format(formatChan, config)
if err != nil {
panic(err)
}
exitChan <- true
})()
exitCode := 0
for f := range failures {
if exitCode == 0 {
exitCode = 1
}
if c, ok := config[f.RuleName]; ok && c.Severity == lint.SeverityError {
exitCode = 2
}
formatChan <- f
}
close(formatChan)
<-exitChan
fmt.Println(output)
os.Exit(exitCode)
}