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

86 lines
1.5 KiB
Go
Raw Normal View History

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