-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.go
134 lines (104 loc) · 3.5 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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/bombsimon/wsl/v3"
)
// nolint: gocognit
func main() {
var (
args []string
help bool
noTest bool
showWarnings bool
cwd, _ = os.Getwd()
files = []string{}
finalFiles = []string{}
config = wsl.DefaultConfig()
)
flag.BoolVar(&help, "h", false, "Show this help text")
flag.BoolVar(&help, "help", false, "")
flag.BoolVar(&noTest, "n", false, "Don't lint test files")
flag.BoolVar(&noTest, "no-test", false, "")
flag.BoolVar(&showWarnings, "w", false, "Show warnings (ignored rules)")
flag.BoolVar(&showWarnings, "warnings", false, "")
flag.BoolVar(&config.StrictAppend, "strict-append", true, "Strict rules for append")
flag.BoolVar(&config.AllowAssignAndCallCuddle, "allow-assign-and-call", true, "Allow assignments and calls to be cuddled (if using same variable/type)")
flag.BoolVar(&config.AllowAssignAndAnythingCuddle, "allow-assign-and-anything", false, "Allow assignments and anything to be cuddled")
flag.BoolVar(&config.AllowMultiLineAssignCuddle, "allow-multi-line-assign", true, "Allow cuddling with multi line assignments")
flag.BoolVar(&config.AllowCuddleDeclaration, "allow-cuddle-declarations", false, "Allow declarations to be cuddled")
flag.BoolVar(&config.AllowTrailingComment, "allow-trailing-comment", false, "Allow blocks to end with a comment")
flag.BoolVar(&config.AllowSeparatedLeadingComment, "allow-separated-leading-comment", false, "Allow empty newlines in leading comments")
flag.BoolVar(&config.ForceCuddleErrCheckAndAssign, "force-err-cuddling", false, "Force cuddling of error checks with error var assignment")
flag.BoolVar(&config.ForceExclusiveShortDeclarations, "force-short-decl-cuddling", false, "Force short declarations to cuddle by themselves")
flag.IntVar(&config.ForceCaseTrailingWhitespaceLimit, "force-case-trailing-whitespace", 0, "Force newlines for case blocks > this number.")
flag.Parse()
if help {
showHelp()
return
}
args = flag.Args()
if len(args) == 0 {
args = []string{"./..."}
}
for _, f := range args {
if strings.HasSuffix(f, "/...") {
dir, _ := filepath.Split(f)
files = append(files, expandGoWildcard(dir)...)
continue
}
if _, err := os.Stat(f); err == nil {
files = append(files, f)
}
}
// Use relative path to print shorter names, sort out test files if chosen.
for _, f := range files {
if noTest {
if strings.HasSuffix(f, "_test.go") {
continue
}
}
if relativePath, err := filepath.Rel(cwd, f); err == nil {
finalFiles = append(finalFiles, relativePath)
continue
}
finalFiles = append(finalFiles, f)
}
processor := wsl.NewProcessorWithConfig(config)
result, warnings := processor.ProcessFiles(finalFiles)
for _, r := range result {
fmt.Println(r.String())
}
if showWarnings && len(warnings) > 0 {
fmt.Println()
fmt.Println("⚠️ Warnings found")
for _, w := range warnings {
fmt.Println(w)
}
}
if len(result) > 0 {
os.Exit(2)
}
}
func expandGoWildcard(root string) []string {
foundFiles := []string{}
_ = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
// Only append go files
if !strings.HasSuffix(info.Name(), ".go") {
return nil
}
foundFiles = append(foundFiles, path)
return nil
})
return foundFiles
}
func showHelp() {
helpText := `Usage: wsl <file> [files...]
Also supports package syntax but will use it in relative path, i.e. ./pkg/...
Flags:`
fmt.Println(helpText)
flag.PrintDefaults()
}