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

252 lines
5.2 KiB
Go
Raw Normal View History

2017-07-04 18:37:19 +02:00
package main
import (
2020-10-29 15:11:52 +02:00
"flag"
2017-07-04 18:37:19 +02:00
"fmt"
2018-01-27 06:20:49 +02:00
"io/ioutil"
2018-01-25 01:41:40 +02:00
"os"
2020-10-29 15:11:52 +02:00
"path/filepath"
"runtime/debug"
2020-10-29 15:11:52 +02:00
"strings"
2017-07-04 18:37:19 +02:00
2018-05-27 00:29:05 +02:00
"github.com/fatih/color"
2020-10-29 15:11:52 +02:00
"github.com/mgechev/dots"
"github.com/mgechev/revive/config"
2018-01-25 01:44:03 +02:00
"github.com/mgechev/revive/lint"
"github.com/mgechev/revive/logging"
2020-10-29 15:11:52 +02:00
"github.com/mitchellh/go-homedir"
2017-07-04 18:37:19 +02:00
)
var (
version = "dev"
commit = "none"
date = "unknown"
builtBy = "unknown"
)
2020-10-29 15:11:52 +02:00
func fail(err string) {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
2018-01-27 06:57:51 +02:00
2017-08-28 01:57:16 +02:00
func main() {
log, err := logging.GetLogger()
if err != nil {
fail(err.Error())
}
2020-10-29 15:11:52 +02:00
conf, err := config.GetConfig(configPath)
if err != nil {
fail(err.Error())
}
formatter, err := config.GetFormatter(formatterName)
if err != nil {
fail(err.Error())
}
if setExitStatus {
conf.ErrorCode = 1
conf.WarningCode = 1
}
if len(excludePaths) == 0 { // if no excludes were set in the command line
excludePaths = conf.Exclude // use those from the configuration
}
2020-10-29 15:11:52 +02:00
packages, err := getPackages(excludePaths)
if err != nil {
fail(err.Error())
}
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
2020-10-29 15:11:52 +02:00
lintingRules, err := config.GetLintingRules(conf)
if err != nil {
fail(err.Error())
}
2017-08-28 01:57:16 +02:00
log.Println("Config loaded")
2020-10-29 15:11:52 +02:00
failures, err := revive.Lint(packages, lintingRules, *conf)
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() {
2020-10-29 15:11:52 +02:00
output, err = formatter.Format(formatChan, *conf)
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 {
2020-10-29 15:11:52 +02:00
if f.Confidence < conf.Confidence {
2018-01-27 06:34:20 +02:00
continue
}
2018-01-25 01:46:43 +02:00
if exitCode == 0 {
2020-10-29 15:11:52 +02:00
exitCode = conf.WarningCode
2018-01-25 01:41:40 +02:00
}
2020-10-29 15:11:52 +02:00
if c, ok := conf.Rules[f.RuleName]; ok && c.Severity == lint.SeverityError {
exitCode = conf.ErrorCode
2018-01-25 01:46:43 +02:00
}
2020-10-29 15:11:52 +02:00
if c, ok := conf.Directives[f.RuleName]; ok && c.Severity == lint.SeverityError {
exitCode = conf.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
}
2020-10-29 15:11:52 +02:00
func normalizeSplit(strs []string) []string {
res := []string{}
for _, s := range strs {
t := strings.Trim(s, " \t")
if len(t) > 0 {
res = append(res, t)
}
}
return res
}
func getPackages(excludePaths arrayFlags) ([][]string, error) {
globs := normalizeSplit(flag.Args())
if len(globs) == 0 {
globs = append(globs, ".")
}
packages, err := dots.ResolvePackages(globs, normalizeSplit(excludePaths))
if err != nil {
return nil, err
}
return packages, nil
}
type arrayFlags []string
func (i *arrayFlags) String() string {
return strings.Join([]string(*i), " ")
}
func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
var (
configPath string
excludePaths arrayFlags
formatterName string
help bool
versionFlag bool
setExitStatus bool
)
2020-10-29 15:11:52 +02:00
var originalUsage = flag.Usage
func getLogo() string {
return color.YellowString(` _ __ _____ _(_)__ _____
| '__/ _ \ \ / / \ \ / / _ \
| | | __/\ V /| |\ V / __/
|_| \___| \_/ |_| \_/ \___|`)
}
func getCall() string {
return color.MagentaString("revive -config c.toml -formatter friendly -exclude a.go -exclude b.go ./...")
}
func getBanner() string {
return fmt.Sprintf(`
%s
Example:
%s
`, getLogo(), getCall())
}
func buildDefaultConfigPath() string {
var result string
if homeDir, err := homedir.Dir(); err == nil {
result = filepath.Join(homeDir, "revive.toml")
if _, err := os.Stat(result); err != nil {
result = ""
}
}
return result
}
func init() {
// Force colorizing for no TTY environments
if os.Getenv("REVIVE_FORCE_COLOR") == "1" {
color.NoColor = false
}
flag.Usage = func() {
fmt.Println(getBanner())
originalUsage()
}
2020-10-29 15:11:52 +02:00
// command line help strings
const (
configUsage = "path to the configuration TOML file, defaults to $HOME/revive.toml, if present (i.e. -config myconf.toml)"
excludeUsage = "list of globs which specify files to be excluded (i.e. -exclude foo/...)"
formatterUsage = "formatter to be used for the output (i.e. -formatter stylish)"
versionUsage = "get revive version"
exitStatusUsage = "set exit status to 1 if any issues are found, overwrites errorCode and warningCode in config"
2020-10-29 15:11:52 +02:00
)
defaultConfigPath := buildDefaultConfigPath()
flag.StringVar(&configPath, "config", defaultConfigPath, configUsage)
flag.Var(&excludePaths, "exclude", excludeUsage)
flag.StringVar(&formatterName, "formatter", "", formatterUsage)
flag.BoolVar(&versionFlag, "version", false, versionUsage)
flag.BoolVar(&setExitStatus, "set_exit_status", false, exitStatusUsage)
2020-10-29 15:11:52 +02:00
flag.Parse()
// Output build info (version, commit, date and builtBy)
if versionFlag {
var buildInfo string
if date != "unknown" && builtBy != "unknown" {
buildInfo = fmt.Sprintf("Built\t\t%s by %s\n", date, builtBy)
}
if commit != "none" {
buildInfo = fmt.Sprintf("Commit:\t\t%s\n%s", commit, buildInfo)
}
if version == "dev" {
bi, ok := debug.ReadBuildInfo()
if ok {
version = bi.Main.Version
if strings.HasPrefix(version, "v") {
version = bi.Main.Version[1:]
}
if len(buildInfo) == 0 {
fmt.Printf("version %s\n", version)
os.Exit(0)
}
}
}
fmt.Printf("Version:\t%s\n%s", version, buildInfo)
os.Exit(0)
}
2020-10-29 15:11:52 +02:00
}