1
0
mirror of https://github.com/mgechev/revive.git synced 2025-02-01 13:07:44 +02:00
revive/main.go

86 lines
1.5 KiB
Go
Raw Normal View History

2017-07-04 19:37:19 +03:00
package main
import (
"fmt"
2018-01-26 20:20:49 -08:00
"io/ioutil"
2018-01-24 15:41:40 -08:00
"os"
2017-07-04 19:37:19 +03:00
2018-05-26 15:29:05 -07:00
"github.com/fatih/color"
2018-01-24 15:44:03 -08:00
"github.com/mgechev/revive/lint"
2017-07-04 19:37:19 +03:00
)
func getLogo() string {
return color.YellowString(` _ __ _____ _(_)__ _____
2018-01-26 20:57:51 -08:00
| '__/ _ \ \ / / \ \ / / _ \
| | | __/\ V /| |\ V / __/
2018-05-26 15:29:05 -07:00
|_| \___| \_/ |_| \_/ \___|`)
}
2018-05-26 15:29:05 -07:00
func getCall() string {
return color.MagentaString("revive -config c.toml -formatter friendly -exclude a.go -exclude b.go ./...")
}
2018-05-26 15:29:05 -07:00
func getBanner() string {
return fmt.Sprintf(`
2018-05-26 15:29:05 -07:00
%s
Example:
%s
`, getLogo(), getCall())
}
2018-01-26 20:57:51 -08:00
2017-08-27 16:57:16 -07:00
func main() {
2018-01-27 16:22:17 -08:00
config := getConfig()
formatter := getFormatter()
2018-05-31 19:42:58 -07:00
packages := getPackages()
2017-07-04 19:37:19 +03:00
2018-01-24 15:44:03 -08:00
revive := lint.New(func(file string) ([]byte, error) {
2018-01-27 16:22:17 -08:00
return ioutil.ReadFile(file)
2017-08-27 16:57:16 -07:00
})
2017-11-19 18:23:01 -08:00
2018-01-26 20:25:22 -08:00
lintingRules := getLintingRules(config)
2017-08-27 16:57:16 -07:00
2018-05-31 19:42:58 -07:00
failures, err := revive.Lint(packages, lintingRules, *config)
2017-07-04 19:37:19 +03:00
if err != nil {
2018-02-02 13:11:40 -05:00
fail(err.Error())
2017-07-04 19:37:19 +03:00
}
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)
2017-07-04 19:37:19 +03:00
2018-01-26 20:25:22 -08:00
var output string
2018-01-24 15:41:40 -08:00
go (func() {
output, err = formatter.Format(formatChan, *config)
2018-01-24 15:41:40 -08:00
if err != nil {
2018-02-02 13:11:40 -05:00
fail(err.Error())
2018-01-24 15:41:40 -08:00
}
exitChan <- true
})()
exitCode := 0
for f := range failures {
2018-01-26 20:34:20 -08:00
if f.Confidence < config.Confidence {
continue
}
2018-01-24 15:46:43 -08:00
if exitCode == 0 {
2018-02-03 19:33:14 -08:00
exitCode = config.WarningCode
2018-01-24 15:41:40 -08:00
}
2018-01-26 20:20:49 -08:00
if c, ok := config.Rules[f.RuleName]; ok && c.Severity == lint.SeverityError {
2018-02-03 19:33:14 -08:00
exitCode = config.ErrorCode
2018-01-24 15:46:43 -08:00
}
if c, ok := config.Directives[f.RuleName]; ok && c.Severity == lint.SeverityError {
exitCode = config.ErrorCode
}
2018-01-24 15:41:40 -08:00
formatChan <- f
}
2018-01-26 20:25:22 -08:00
2018-01-24 15:41:40 -08:00
close(formatChan)
<-exitChan
2018-01-27 23:03:07 -08:00
if output != "" {
fmt.Println(output)
}
2018-01-24 15:41:40 -08:00
os.Exit(exitCode)
2017-07-04 19:37:19 +03:00
}