2017-07-04 18:37:19 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-01-25 01:41:40 +02:00
|
|
|
"os"
|
2017-07-04 18:37:19 +02:00
|
|
|
|
2017-08-29 19:47:29 +02:00
|
|
|
"github.com/mgechev/revive/formatter"
|
2018-01-25 01:44:03 +02:00
|
|
|
"github.com/mgechev/revive/lint"
|
2017-08-29 19:47:29 +02:00
|
|
|
"github.com/mgechev/revive/rule"
|
2017-07-04 18:37:19 +02:00
|
|
|
)
|
|
|
|
|
2017-08-28 01:57:16 +02:00
|
|
|
func main() {
|
2017-07-04 18:37:19 +02:00
|
|
|
src := `
|
2018-01-25 21:47:39 +02:00
|
|
|
package p
|
2017-08-28 01:57:16 +02:00
|
|
|
|
2018-01-25 21:47:39 +02:00
|
|
|
func Test() {
|
|
|
|
if true {
|
|
|
|
return 42;
|
|
|
|
} else {
|
|
|
|
return 23;
|
|
|
|
}
|
2017-11-20 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:41:40 +02:00
|
|
|
func foo_bar(a int, b int, c int, d int) {
|
2017-11-20 04:23:01 +02:00
|
|
|
return a + b + c;
|
2018-01-25 21:47:39 +02:00
|
|
|
}`
|
2017-07-04 18:37:19 +02:00
|
|
|
|
2018-01-25 01:44:03 +02:00
|
|
|
revive := lint.New(func(file string) ([]byte, error) {
|
2017-08-28 01:57:16 +02:00
|
|
|
return []byte(src), nil
|
|
|
|
})
|
2018-01-25 01:44:03 +02:00
|
|
|
var result []lint.Rule
|
2018-01-25 21:47:39 +02:00
|
|
|
result = append(result, &rule.ElseRule{}, &rule.ArgumentsLimitRule{}, &rule.NamesRule{})
|
2017-11-20 04:23:01 +02:00
|
|
|
|
2018-01-25 01:44:03 +02:00
|
|
|
var config = lint.RulesConfig{
|
|
|
|
"argument-limit": lint.RuleConfig{
|
2018-01-25 01:41:40 +02:00
|
|
|
Arguments: []string{"3"},
|
2018-01-25 01:46:43 +02:00
|
|
|
Severity: lint.SeverityError,
|
2018-01-25 01:41:40 +02:00
|
|
|
},
|
2017-11-20 04:23:01 +02:00
|
|
|
}
|
2017-08-28 01:57:16 +02:00
|
|
|
|
2018-01-22 04:04:41 +02:00
|
|
|
failures, err := revive.Lint([]string{"foo.go", "bar.go", "baz.go"}, result, config)
|
2017-07-04 18:37:19 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:44:03 +02:00
|
|
|
formatChan := make(chan lint.Failure)
|
2018-01-25 01:41:40 +02:00
|
|
|
exitChan := make(chan bool)
|
|
|
|
var output string
|
2017-07-04 18:37:19 +02:00
|
|
|
|
2018-01-25 01:41:40 +02:00
|
|
|
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 {
|
2018-01-25 01:46:43 +02:00
|
|
|
if exitCode == 0 {
|
2018-01-25 01:41:40 +02:00
|
|
|
exitCode = 1
|
|
|
|
}
|
2018-01-25 01:46:43 +02:00
|
|
|
if c, ok := config[f.RuleName]; ok && c.Severity == lint.SeverityError {
|
|
|
|
exitCode = 2
|
|
|
|
}
|
2018-01-25 01:41:40 +02:00
|
|
|
formatChan <- f
|
|
|
|
}
|
|
|
|
close(formatChan)
|
|
|
|
<-exitChan
|
2017-08-28 01:57:16 +02:00
|
|
|
fmt.Println(output)
|
2018-01-25 01:41:40 +02:00
|
|
|
|
|
|
|
os.Exit(exitCode)
|
2017-07-04 18:37:19 +02:00
|
|
|
}
|