-
Notifications
You must be signed in to change notification settings - Fork 265
/
main.go
193 lines (159 loc) 路 3.9 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/fatih/color"
"github.com/mgechev/dots"
"github.com/mgechev/revive/config"
"github.com/mgechev/revive/lint"
"github.com/mitchellh/go-homedir"
)
func fail(err string) {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
func main() {
conf, err := config.GetConfig(configPath)
if err != nil {
fail(err.Error())
}
formatter, err := config.GetFormatter(formatterName)
if err != nil {
fail(err.Error())
}
if len(excludePaths) == 0 { // if no excludes were set in the command line
excludePaths = conf.Exclude // use those from the configuration
}
packages, err := getPackages(excludePaths)
if err != nil {
fail(err.Error())
}
revive := lint.New(func(file string) ([]byte, error) {
return ioutil.ReadFile(file)
})
lintingRules, err := config.GetLintingRules(conf)
if err != nil {
fail(err.Error())
}
failures, err := revive.Lint(packages, lintingRules, *conf)
if err != nil {
fail(err.Error())
}
formatChan := make(chan lint.Failure)
exitChan := make(chan bool)
var output string
go (func() {
output, err = formatter.Format(formatChan, *conf)
if err != nil {
fail(err.Error())
}
exitChan <- true
})()
exitCode := 0
for f := range failures {
if f.Confidence < conf.Confidence {
continue
}
if exitCode == 0 {
exitCode = conf.WarningCode
}
if c, ok := conf.Rules[f.RuleName]; ok && c.Severity == lint.SeverityError {
exitCode = conf.ErrorCode
}
if c, ok := conf.Directives[f.RuleName]; ok && c.Severity == lint.SeverityError {
exitCode = conf.ErrorCode
}
formatChan <- f
}
close(formatChan)
<-exitChan
if output != "" {
fmt.Println(output)
}
os.Exit(exitCode)
}
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
var excludePaths arrayFlags
var formatterName string
var help bool
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()
}
// 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)"
)
defaultConfigPath := buildDefaultConfigPath()
flag.StringVar(&configPath, "config", defaultConfigPath, configUsage)
flag.Var(&excludePaths, "exclude", excludeUsage)
flag.StringVar(&formatterName, "formatter", "", formatterUsage)
flag.Parse()
}