@@ -19,8 +19,8 @@ func NewWith(versionNo, commitHash, builtTime string) *cli.App {
19
19
cmds := []* cli.Command {
20
20
initCmd (),
21
21
lintCmd (),
22
- createCmd (),
23
- verifyCmd (),
22
+ configCmd (),
23
+ hookCmd (),
24
24
}
25
25
26
26
app := & cli.App {
@@ -32,27 +32,46 @@ func NewWith(versionNo, commitHash, builtTime string) *cli.App {
32
32
return app
33
33
}
34
34
35
- func initCmd () * cli.Command {
35
+ func lintCmd () * cli.Command {
36
36
return & cli.Command {
37
- Name : "init " ,
38
- Usage : "setup commitlint for git repos " ,
37
+ Name : "lint " ,
38
+ Usage : "Check commit message against lint rules " ,
39
39
Flags : []cli.Flag {
40
- & cli.BoolFlag {
41
- Name : "global" ,
42
- Aliases : []string {"g" },
43
- Usage : "sets git hook in global config" ,
44
- },
45
40
& cli.StringFlag {
46
41
Name : "config" ,
47
42
Aliases : []string {"c" , "conf" },
48
43
Value : "" ,
49
44
Usage : "optional config file `conf.yaml`" ,
50
45
},
51
- & cli.BoolFlag {
52
- Name : "replace" ,
53
- Usage : "replace files if already exists" ,
46
+ & cli.StringFlag {
47
+ Name : "message" ,
48
+ Aliases : []string {"m" , "msg" },
49
+ Value : "" ,
50
+ Usage : "path to commit message `FILE`" ,
54
51
},
55
52
},
53
+ Action : func (ctx * cli.Context ) error {
54
+ confFilePath := ctx .String ("config" )
55
+ fileInput := ctx .String ("message" )
56
+ return Lint (confFilePath , fileInput )
57
+ },
58
+ }
59
+ }
60
+
61
+ func initCmd () * cli.Command {
62
+ confFlag := formConfFlag ()
63
+ replaceFlag := formReplaceFlag ()
64
+
65
+ globalFlag := & cli.BoolFlag {
66
+ Name : "global" ,
67
+ Aliases : []string {"g" },
68
+ Usage : "Sets git hook in global config" ,
69
+ }
70
+
71
+ return & cli.Command {
72
+ Name : "init" ,
73
+ Usage : "Setup commitlint for git repos" ,
74
+ Flags : []cli.Flag {globalFlag , confFlag , replaceFlag },
56
75
Action : func (ctx * cli.Context ) error {
57
76
confPath := ctx .String ("config" )
58
77
isGlobal := ctx .Bool ("global" )
@@ -74,46 +93,68 @@ func initCmd() *cli.Command {
74
93
}
75
94
}
76
95
77
- func createCmd () * cli.Command {
78
- configCmd := & cli.Command {
79
- Name : "config " ,
80
- Usage : "creates commitlint.yaml in current directory" ,
96
+ func configCmd () * cli.Command {
97
+ createCmd := & cli.Command {
98
+ Name : "create " ,
99
+ Usage : "Creates default config in current directory" ,
81
100
Flags : []cli.Flag {
82
101
& cli.BoolFlag {
83
102
Name : "enabled" ,
84
103
Aliases : []string {"e" },
85
- Usage : "writes only default enabled rules to file " ,
104
+ Usage : "writes only default enabled rules" ,
86
105
Value : false ,
87
106
},
88
107
},
89
108
Action : func (ctx * cli.Context ) error {
90
109
isOnlyEnabled := ctx .Bool ("enabled" )
91
- return CreateConfig (isOnlyEnabled )
110
+ return ConfigCreate (isOnlyEnabled )
92
111
},
93
112
}
94
113
95
- hookCmd := & cli.Command {
96
- Name : "hook " ,
97
- Usage : "creates commit-msg file in current directory " ,
114
+ checkCmd := & cli.Command {
115
+ Name : "check " ,
116
+ Usage : "Checks if given config is valid " ,
98
117
Flags : []cli.Flag {
99
118
& cli.StringFlag {
100
- Name : "config" ,
101
- Aliases : []string {"c" , "conf" },
102
- Value : "" ,
103
- Usage : "optional config file `conf.yaml`" ,
104
- },
105
- & cli.BoolFlag {
106
- Name : "replace" ,
107
- Usage : "replace hook files if already exists" ,
119
+ Name : "config" ,
120
+ Aliases : []string {"c" , "conf" },
121
+ Usage : "config file `conf.yaml`" ,
122
+ Required : true ,
108
123
},
109
124
},
125
+ Action : func (ctx * cli.Context ) error {
126
+ confFile := ctx .String ("config" )
127
+ err := ConfigCheck (confFile )
128
+ if err != nil {
129
+ return err
130
+ }
131
+ fmt .Printf ("%s config is valid\n " , confFile )
132
+ return nil
133
+ },
134
+ }
135
+
136
+ return & cli.Command {
137
+ Name : "config" ,
138
+ Usage : "Manage commitlint config" ,
139
+ Subcommands : []* cli.Command {createCmd , checkCmd },
140
+ }
141
+ }
142
+
143
+ func hookCmd () * cli.Command {
144
+ confFlag := formConfFlag ()
145
+ replaceFlag := formReplaceFlag ()
146
+
147
+ createCmd := & cli.Command {
148
+ Name : "create" ,
149
+ Usage : "Creates git hook files in current directory" ,
150
+ Flags : []cli.Flag {confFlag , replaceFlag },
110
151
Action : func (ctx * cli.Context ) error {
111
152
confPath := ctx .String ("config" )
112
153
isReplace := ctx .Bool ("replace" )
113
- err := CreateHook (confPath , isReplace )
154
+ err := HookCreate (confPath , isReplace )
114
155
if err != nil {
115
156
if isHookExists (err ) {
116
- fmt .Println ("create hook failed. files already exists" )
157
+ fmt .Println ("create failed. hook files already exists" )
117
158
fmt .Println ("run with --replace to replace existing hook files" )
118
159
return nil
119
160
}
@@ -125,56 +166,24 @@ func createCmd() *cli.Command {
125
166
}
126
167
127
168
return & cli.Command {
128
- Name : "create" ,
129
- Usage : "create commitlint config, hooks files" ,
130
- Subcommands : []* cli.Command {
131
- configCmd ,
132
- hookCmd ,
133
- },
169
+ Name : "hook" ,
170
+ Usage : "Manage commitlint git hooks" ,
171
+ Subcommands : []* cli.Command {createCmd },
134
172
}
135
173
}
136
174
137
- func lintCmd () * cli.Command {
138
- return & cli.Command {
139
- Name : "lint" ,
140
- Usage : "lints commit message" ,
141
- Flags : []cli.Flag {
142
- & cli.StringFlag {
143
- Name : "config" ,
144
- Aliases : []string {"c" , "conf" },
145
- Value : "" ,
146
- Usage : "optional config file `conf.yaml`" ,
147
- },
148
- & cli.StringFlag {
149
- Name : "message" ,
150
- Aliases : []string {"m" , "msg" },
151
- Value : "" ,
152
- Usage : "path to git commit message `FILE`" ,
153
- },
154
- },
155
- Action : func (ctx * cli.Context ) error {
156
- confFilePath := ctx .String ("config" )
157
- fileInput := ctx .String ("message" )
158
- return Lint (confFilePath , fileInput )
159
- },
175
+ func formConfFlag () * cli.StringFlag {
176
+ return & cli.StringFlag {
177
+ Name : "config" ,
178
+ Aliases : []string {"c" , "conf" },
179
+ Value : "" ,
180
+ Usage : "Optional config file `conf.yaml` which will be passed to 'commitlint lint'. Check config precedence" ,
160
181
}
161
182
}
162
183
163
- func verifyCmd () * cli.Command {
164
- return & cli.Command {
165
- Name : "verify" ,
166
- Usage : "verifies commitlint config" ,
167
- Flags : []cli.Flag {
168
- & cli.StringFlag {
169
- Name : "config" ,
170
- Aliases : []string {"c" , "conf" },
171
- Value : "" ,
172
- Usage : "optional config file `conf.yaml`" ,
173
- },
174
- },
175
- Action : func (ctx * cli.Context ) error {
176
- confFilePath := ctx .String ("config" )
177
- return VerifyConfig (confFilePath )
178
- },
184
+ func formReplaceFlag () * cli.BoolFlag {
185
+ return & cli.BoolFlag {
186
+ Name : "replace" ,
187
+ Usage : "Replace hook files if already exists" ,
179
188
}
180
189
}
0 commit comments