-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
207 lines (177 loc) · 4.22 KB
/
Copy pathmain.go
File metadata and controls
207 lines (177 loc) · 4.22 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"regexp"
"strings"
"flag"
"github.com/fatih/color"
)
type Parsed struct {
Type *string
Scope *string
Header *string
Subject *string
}
type LintConfig struct {
HeaderMaxLength int
AllowedTypes map[string]struct{}
}
type LintError struct {
Description string
}
func parse(text string) Parsed {
res := Parsed{
Header: &text,
}
r, _ := regexp.Compile(`^(\w*)(?:\(([\w$.\-* ]*)\))?: (.*)$`)
if r.MatchString(text) {
submatch := r.FindStringSubmatch(text)
if len(submatch) > 1 {
res.Type = &submatch[1]
}
if len(submatch) > 2 {
res.Scope = &submatch[2]
}
if len(submatch) > 3 {
res.Subject = &submatch[3]
}
}
return res
}
func check(parsed Parsed, config LintConfig) []LintError {
errors := make([]LintError, 0)
if parsed.Header == nil {
lintError := LintError{
Description: "header should be non-empty",
}
errors = append(errors, lintError)
} else if len(*parsed.Header) > config.HeaderMaxLength {
lintError := LintError{
Description: fmt.Sprintf("header should be less than %v, actual %v", config.HeaderMaxLength, len(*parsed.Header)),
}
errors = append(errors, lintError)
}
if parsed.Type == nil {
lintError := LintError{
Description: "type should be non-empty",
}
errors = append(errors, lintError)
} else {
if _, ok := config.AllowedTypes[*parsed.Type]; !ok {
allowedTypes := make([]string, 0, len(config.AllowedTypes))
for allowedType := range config.AllowedTypes {
allowedTypes = append(allowedTypes, allowedType)
}
lintError := LintError{
Description: fmt.Sprintf("type should be on of: %v", strings.Join(allowedTypes, ", ")),
}
errors = append(errors, lintError)
}
}
return errors
}
type internalConfig struct {
HeaderMaxLength int `json:"header-max-length"`
AllowedTypes []string `json:"types"`
}
func generateDefaultConfig() {
configuration := internalConfig{
HeaderMaxLength: 72,
AllowedTypes: []string{
"build",
"ci",
"docs",
"feat",
"fix",
"perf",
"refactor",
"revert",
"style",
"test"},
}
out, err := json.MarshalIndent(configuration, "", " ")
if err != nil {
fmt.Println("can't generate config %v", err)
}
fmt.Printf("%s", string(out))
}
func readConfig() (LintConfig, error) {
lintConfig := LintConfig{}
file, err := os.Open(".commitlint")
if err != nil {
return lintConfig, fmt.Errorf("config .commitlint not found %v", err)
}
defer file.Close()
decoder := json.NewDecoder(file)
configuration := internalConfig{}
err = decoder.Decode(&configuration)
if err != nil {
if err != nil {
return lintConfig, fmt.Errorf("error decode config %v", err)
}
}
allowedTypes := make(map[string]struct{})
for _, v := range configuration.AllowedTypes {
allowedTypes[v] = struct{}{}
}
lintConfig.HeaderMaxLength = configuration.HeaderMaxLength
lintConfig.AllowedTypes = allowedTypes
return lintConfig, nil
}
func parseAndCheck() {
errorColor := color.New(color.FgRed)
neutralColor := color.New(color.Bold)
goodColor := color.New(color.FgGreen, color.Bold)
reader := bufio.NewReader(os.Stdin)
text, err := reader.ReadString('\n')
if err != nil {
errorColor.Println(err)
}
cfg, err := readConfig()
if err != nil {
errorColor.Println(err)
}
parsed := parse(text[:len(text)-1])
lints := check(parsed, cfg)
fmt.Print("⧗\tinput: ")
neutralColor.Print(text)
if len(lints) == 0 {
goodColor.Print("✔")
neutralColor.Println("\tAll ok!")
} else {
fmt.Println()
for _, lintError := range lints {
errorColor.Print("✖\t")
errorColor.Println(lintError.Description)
}
errorColor.Print("✖\t")
neutralColor.Printf("Found %v problems\n", len(lints))
}
}
func main() {
generateConfigCommand := flag.NewFlagSet("config-generate", flag.ExitOnError)
flag.Usage = func() {
fmt.Printf("Usage: commitlint [command]\n")
flag.PrintDefaults()
fmt.Println("\ncommands:")
fmt.Println("\tconfig-generate: print default config")
}
flag.Parse()
if len(os.Args) < 2 {
parseAndCheck()
} else {
switch os.Args[1] {
case "config-generate":
generateConfigCommand.Parse(os.Args[2:])
default:
flag.PrintDefaults()
os.Exit(1)
}
}
if generateConfigCommand.Parsed() {
generateDefaultConfig()
}
}