-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcli.go
226 lines (205 loc) · 5.3 KB
/
cli.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Package cmd contains commitlint cli
package cmd
import (
"fmt"
"github.com/urfave/cli/v2"
"github.com/conventionalcommit/commitlint/internal"
)
// newCliApp returns commitlint cli.App
func newCliApp() *cli.App {
cmds := []*cli.Command{
newInitCmd(),
newLintCmd(),
newConfigCmd(),
newHookCmd(),
newDebugCmd(),
}
app := &cli.App{
Name: "commitlint",
Usage: "linter for conventional commits",
Commands: cmds,
Version: internal.FullVersion(),
}
return app
}
func newLintCmd() *cli.Command {
return &cli.Command{
Name: "lint",
Usage: "Check commit message against lint rules",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Value: "",
Usage: "optional config file `conf.yaml`",
},
&cli.StringFlag{
Name: "message",
Aliases: []string{"m", "msg"},
Value: "",
Usage: "path to commit message `FILE`",
},
},
Action: func(ctx *cli.Context) error {
confFilePath := ctx.String("config")
fileInput := ctx.String("message")
err := lintMsg(confFilePath, fileInput)
return handleError(err, "Failed to run lint command")
},
}
}
func newInitCmd() *cli.Command {
confFlag := newConfFlag()
replaceFlag := newReplaceFlag()
hooksFlag := newHooksPathFlag()
globalFlag := &cli.BoolFlag{
Name: "global",
Aliases: []string{"g"},
Usage: "Sets git hook in global config",
}
return &cli.Command{
Name: "init",
Usage: "Setup commitlint for git repos",
Flags: []cli.Flag{globalFlag, confFlag, replaceFlag, hooksFlag},
Action: func(ctx *cli.Context) error {
confPath := ctx.String("config")
isGlobal := ctx.Bool("global")
isReplace := ctx.Bool("replace")
hooksPath := ctx.String("hookspath")
err := initLint(confPath, hooksPath, isGlobal, isReplace)
if handleError(err, "Failed to initialize commitlint") != nil {
if isHookExists(err) {
fmt.Println("commitlint init failed")
fmt.Println("run with --replace to replace existing files")
return nil
}
return err
}
fmt.Println("commitlint init successfully")
return nil
},
}
}
func newConfigCmd() *cli.Command {
createCmd := &cli.Command{
Name: "create",
Usage: "Creates default config in current directory",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "replace",
Aliases: []string{"r"},
Usage: "Replace conf file if already exists",
Value: false,
},
&cli.StringFlag{
Name: "file",
Usage: "Config file name",
Value: ".commitlint.yaml",
},
},
Action: func(ctx *cli.Context) error {
isReplace := ctx.Bool("replace")
fileName := ctx.String("file")
err := configCreate(fileName, isReplace)
if handleError(err, "Failed to create config file") != nil {
if isConfExists(err) {
fmt.Println("config create failed")
fmt.Println("run with --replace to replace existing file")
return nil
}
return err
}
fmt.Println("config file created")
return nil
},
}
checkCmd := &cli.Command{
Name: "check",
Usage: "Checks if given config is valid",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "config file `conf.yaml`",
Required: true,
},
},
Action: func(ctx *cli.Context) error {
confFile := ctx.String("config")
errs := configCheck(confFile)
if len(errs) == 0 {
fmt.Printf("%s config is valid\n", confFile)
return nil
}
if len(errs) == 1 {
return handleError(errs[0], "Config check failed")
}
merr := multiError(errs)
return handleError(&merr, "Config check failed")
},
}
return &cli.Command{
Name: "config",
Usage: "Manage commitlint config",
Subcommands: []*cli.Command{createCmd, checkCmd},
}
}
func newHookCmd() *cli.Command {
replaceFlag := newReplaceFlag()
hooksFlag := newHooksPathFlag()
createCmd := &cli.Command{
Name: "create",
Usage: "Creates git hook files in current directory",
Flags: []cli.Flag{replaceFlag, hooksFlag},
Action: func(ctx *cli.Context) error {
isReplace := ctx.Bool("replace")
hooksPath := ctx.String("hookspath")
err := hookCreate(hooksPath, isReplace)
if handleError(err, "Failed to create hooks") != nil {
if isHookExists(err) {
fmt.Println("create failed. hook files already exist")
fmt.Println("run with --replace to replace existing hook files")
return nil
}
return err
}
fmt.Println("hooks created")
return nil
},
}
return &cli.Command{
Name: "hook",
Usage: "Manage commitlint git hooks",
Subcommands: []*cli.Command{createCmd},
}
}
func newDebugCmd() *cli.Command {
return &cli.Command{
Name: "debug",
Usage: "prints useful information for debugging",
Action: func(ctx *cli.Context) error {
return handleError(printDebug(), "Debugging information failed")
},
}
}
func newConfFlag() *cli.StringFlag {
return &cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Value: "",
Usage: "Optional config file `conf.yaml` which will be passed to 'commitlint lint'. Check config precedence",
}
}
func newHooksPathFlag() *cli.StringFlag {
return &cli.StringFlag{
Name: "hookspath",
Value: "",
Usage: "Optional hookspath to install git hooks",
}
}
func newReplaceFlag() *cli.BoolFlag {
return &cli.BoolFlag{
Name: "replace",
Usage: "Replace hook files if already exists",
}
}