1
0
mirror of https://github.com/mgechev/revive.git synced 2025-03-21 21:17:06 +02:00
revive/main.go

75 lines
1.3 KiB
Go
Raw Normal View History

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