1
0
mirror of https://github.com/mgechev/revive.git synced 2025-01-26 03:52:12 +02:00
revive/main.go

76 lines
1.4 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
)
2018-05-26 15:29:05 -07:00
var logo = color.YellowString(` _ __ _____ _(_)__ _____
2018-01-26 20:57:51 -08:00
| '__/ _ \ \ / / \ \ / / _ \
| | | __/\ V /| |\ V / __/
2018-05-26 15:29:05 -07:00
|_| \___| \_/ |_| \_/ \___|`)
var call = color.MagentaString("revive -config c.toml -formatter friendly -exclude a.go -exclude b.go ./...")
var banner = fmt.Sprintf(`
%s
Example:
%s
`, logo, call)
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() {
2018-01-26 20:20:49 -08:00
output, err = formatter.Format(formatChan, config.Rules)
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
}
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
}