Skip to content

Commit f9c5b86

Browse files
refactor: simplify hook, config command, better usage message
1 parent bdc29dd commit f9c5b86

File tree

1 file changed

+65
-81
lines changed

1 file changed

+65
-81
lines changed

internal/cmd/cmd.go

Lines changed: 65 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func newCliApp() *cli.App {
2525

2626
app := &cli.App{
2727
Name: "commitlint",
28-
Usage: "linter for conventional commits",
28+
Usage: "Lint commit messages using Conventional Commits rules",
2929
Commands: cmds,
3030
Version: internal.FullVersion(),
3131
}
@@ -34,20 +34,19 @@ func newCliApp() *cli.App {
3434

3535
func newLintCmd() *cli.Command {
3636
return &cli.Command{
37-
Name: "lint",
38-
Usage: "Check commit message against lint rules",
37+
Name: "lint",
38+
Usage: "Check a commit message",
39+
Description: "Reads from stdin (piped), or --message file, or .git/COMMIT_EDITMSG (in that order).",
3940
Flags: []cli.Flag{
4041
&cli.StringFlag{
4142
Name: "config",
4243
Aliases: []string{"c"},
43-
Value: "",
44-
Usage: "optional config file `conf.yaml`",
44+
Usage: "Use this config `FILE` instead of auto-detected one",
4545
},
4646
&cli.StringFlag{
4747
Name: "message",
4848
Aliases: []string{"m", "msg"},
49-
Value: "",
50-
Usage: "path to commit message `FILE`",
49+
Usage: "Read commit message from `FILE`",
5150
},
5251
},
5352
Action: func(ctx *cli.Context) error {
@@ -59,20 +58,32 @@ func newLintCmd() *cli.Command {
5958
}
6059

6160
func newInitCmd() *cli.Command {
62-
confFlag := newConfFlag()
63-
replaceFlag := newReplaceFlag()
64-
hooksFlag := newHooksPathFlag()
65-
66-
globalFlag := &cli.BoolFlag{
67-
Name: "global",
68-
Aliases: []string{"g"},
69-
Usage: "Sets git hook in global config",
70-
}
71-
7261
return &cli.Command{
73-
Name: "init",
74-
Usage: "Setup commitlint for git repos",
75-
Flags: []cli.Flag{globalFlag, confFlag, replaceFlag, hooksFlag},
62+
Name: "init",
63+
Usage: "Set up commitlint for a git repository",
64+
Description: "Creates the commit-msg hook and points git to it.\nUse --global to apply across all your repositories.",
65+
Flags: []cli.Flag{
66+
&cli.BoolFlag{
67+
Name: "global",
68+
Aliases: []string{"g"},
69+
Usage: "Set up for all repositories (uses global git config)",
70+
},
71+
&cli.StringFlag{
72+
Name: "config",
73+
Aliases: []string{"c"},
74+
Usage: "Pass a config `FILE` to the hook",
75+
},
76+
&cli.BoolFlag{
77+
Name: "replace",
78+
Aliases: []string{"r"},
79+
Usage: "Overwrite existing hook files",
80+
},
81+
&cli.StringFlag{
82+
Name: "hookspath",
83+
Aliases: []string{"p"},
84+
Usage: "Where to write hook files (default: .commitlint/hooks)",
85+
},
86+
},
7687
Action: func(ctx *cli.Context) error {
7788
confPath := ctx.String("config")
7889
isGlobal := ctx.Bool("global")
@@ -82,8 +93,8 @@ func newInitCmd() *cli.Command {
8293
err := initLint(confPath, hooksPath, isGlobal, isReplace)
8394
if err != nil {
8495
if isHookExists(err) {
85-
fmt.Println("commitlint init failed")
86-
fmt.Println("run with --replace to replace existing files")
96+
fmt.Println("commitlint init failed: hook files already exist")
97+
fmt.Println("use --replace to overwrite them")
8798
return nil
8899
}
89100
return err
@@ -98,17 +109,16 @@ func newInitCmd() *cli.Command {
98109
func newConfigCmd() *cli.Command {
99110
createCmd := &cli.Command{
100111
Name: "create",
101-
Usage: "Creates default config in current directory",
112+
Usage: "Create a default config file",
102113
Flags: []cli.Flag{
103114
&cli.BoolFlag{
104115
Name: "replace",
105116
Aliases: []string{"r"},
106-
Usage: "Replace conf file if already exists",
107-
Value: false,
117+
Usage: "Overwrite if the file already exists",
108118
},
109119
&cli.StringFlag{
110120
Name: "file",
111-
Usage: "Config file name",
121+
Usage: "Output file name",
112122
Value: ".commitlint.yaml",
113123
},
114124
},
@@ -118,8 +128,8 @@ func newConfigCmd() *cli.Command {
118128
err := configCreate(fileName, isReplace)
119129
if err != nil {
120130
if isConfExists(err) {
121-
fmt.Println("config create failed")
122-
fmt.Println("run with --replace to replace existing file")
131+
fmt.Println("config file already exists")
132+
fmt.Println("use --replace to overwrite it")
123133
return nil
124134
}
125135
return err
@@ -130,21 +140,17 @@ func newConfigCmd() *cli.Command {
130140
}
131141

132142
checkCmd := &cli.Command{
133-
Name: "check",
134-
Usage: "Checks if given config is valid",
135-
Flags: []cli.Flag{
136-
&cli.StringFlag{
137-
Name: "config",
138-
Aliases: []string{"c"},
139-
Usage: "config file `conf.yaml`",
140-
Required: true,
141-
},
142-
},
143+
Name: "check",
144+
Usage: "Check if a config file is valid",
145+
ArgsUsage: "<config-file>",
143146
Action: func(ctx *cli.Context) error {
144-
confFile := ctx.String("config")
147+
confFile := ctx.Args().First()
148+
if confFile == "" {
149+
return fmt.Errorf("please provide a config file path\n\nUsage: commitlint config check <config-file>")
150+
}
145151
errs := configCheck(confFile)
146152
if len(errs) == 0 {
147-
fmt.Printf("%s config is valid\n", confFile)
153+
fmt.Printf("%s: valid\n", confFile)
148154
return nil
149155
}
150156
if len(errs) == 1 {
@@ -157,27 +163,35 @@ func newConfigCmd() *cli.Command {
157163

158164
return &cli.Command{
159165
Name: "config",
160-
Usage: "Manage commitlint config",
166+
Usage: "Manage configuration",
161167
Subcommands: []*cli.Command{createCmd, checkCmd},
162168
}
163169
}
164170

165171
func newHookCmd() *cli.Command {
166-
replaceFlag := newReplaceFlag()
167-
hooksFlag := newHooksPathFlag()
168-
169-
createCmd := &cli.Command{
170-
Name: "create",
171-
Usage: "Creates git hook files in current directory",
172-
Flags: []cli.Flag{replaceFlag, hooksFlag},
172+
return &cli.Command{
173+
Name: "hook",
174+
Usage: "Create the commit-msg git hook",
175+
Flags: []cli.Flag{
176+
&cli.BoolFlag{
177+
Name: "replace",
178+
Aliases: []string{"r"},
179+
Usage: "Overwrite existing hook files",
180+
},
181+
&cli.StringFlag{
182+
Name: "hookspath",
183+
Aliases: []string{"p"},
184+
Usage: "Where to write hook files (default: .commitlint/hooks)",
185+
},
186+
},
173187
Action: func(ctx *cli.Context) error {
174188
isReplace := ctx.Bool("replace")
175189
hooksPath := ctx.String("hookspath")
176190
err := hookCreate(hooksPath, isReplace)
177191
if err != nil {
178192
if isHookExists(err) {
179-
fmt.Println("create failed. hook files already exists")
180-
fmt.Println("run with --replace to replace existing hook files")
193+
fmt.Println("hook files already exist")
194+
fmt.Println("use --replace to overwrite them")
181195
return nil
182196
}
183197
return err
@@ -186,44 +200,14 @@ func newHookCmd() *cli.Command {
186200
return nil
187201
},
188202
}
189-
190-
return &cli.Command{
191-
Name: "hook",
192-
Usage: "Manage commitlint git hooks",
193-
Subcommands: []*cli.Command{createCmd},
194-
}
195203
}
196204

197205
func newDebugCmd() *cli.Command {
198206
return &cli.Command{
199207
Name: "debug",
200-
Usage: "prints useful information for debugging",
208+
Usage: "Show debug info (version, hooks, config)",
201209
Action: func(ctx *cli.Context) error {
202210
return printDebug()
203211
},
204212
}
205213
}
206-
207-
func newConfFlag() *cli.StringFlag {
208-
return &cli.StringFlag{
209-
Name: "config",
210-
Aliases: []string{"c"},
211-
Value: "",
212-
Usage: "Optional config file `conf.yaml` which will be passed to 'commitlint lint'. Check config precedence",
213-
}
214-
}
215-
216-
func newHooksPathFlag() *cli.StringFlag {
217-
return &cli.StringFlag{
218-
Name: "hookspath",
219-
Value: "",
220-
Usage: "Optional hookspath to install git hooks",
221-
}
222-
}
223-
224-
func newReplaceFlag() *cli.BoolFlag {
225-
return &cli.BoolFlag{
226-
Name: "replace",
227-
Usage: "Replace hook files if already exists",
228-
}
229-
}

0 commit comments

Comments
 (0)