Skip to content

Commit 0ee7ce1

Browse files
feat!: add init command, change create behavior
* init - creates hook files and sets git config * create hook - create hook file in current directory * create config - create config file
1 parent e2197b8 commit 0ee7ce1

File tree

2 files changed

+41
-26
lines changed

2 files changed

+41
-26
lines changed

cmd/commitlint/cmd.go

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,46 @@ import (
1111
func getApp() *cli.App {
1212
createCmd := &cli.Command{
1313
Name: "create",
14-
Usage: "helpers for initializing commitlint",
14+
Usage: "for creating commitlint conf, hooks",
1515
Subcommands: []*cli.Command{
1616
{
17-
Name: "config",
18-
Usage: "creates default commitlint.yaml in current directory",
19-
Action: initConfCallback,
17+
Name: "config",
18+
Usage: "creates commitlint.yaml in current directory",
19+
Action: func(ctx *cli.Context) error {
20+
err := commitlint.DefaultConfToFile(defConfFileName)
21+
if err != nil {
22+
return cli.Exit(err, exitCode)
23+
}
24+
return nil
25+
},
2026
},
2127
{
22-
Name: "hook",
23-
Usage: "creates commit hook and sets git config",
24-
Action: initHookCallback,
25-
Flags: []cli.Flag{
26-
&cli.BoolFlag{
27-
Name: "global",
28-
Aliases: []string{"g"},
29-
Usage: "sets git hook config in global`",
30-
},
28+
Name: "hook",
29+
Usage: "creates commit-msg in current directory",
30+
Action: func(ctx *cli.Context) (retErr error) {
31+
err := commitlint.WriteHookToFile(commitMsgHook)
32+
if err != nil {
33+
return cli.Exit(err, exitCode)
34+
}
35+
return nil
3136
},
3237
},
3338
},
3439
}
3540

41+
initCmd := &cli.Command{
42+
Name: "init",
43+
Usage: "setup commitlint for git repos",
44+
Action: initCallback,
45+
Flags: []cli.Flag{
46+
&cli.BoolFlag{
47+
Name: "global",
48+
Aliases: []string{"g"},
49+
Usage: "sets git hook config in global",
50+
},
51+
},
52+
}
53+
3654
lintCmd := &cli.Command{
3755
Name: "lint",
3856
Usage: "lints commit message",
@@ -63,9 +81,14 @@ func getApp() *cli.App {
6381
}
6482

6583
return &cli.App{
66-
Name: "commitlint",
67-
Usage: "linter for conventional commits",
68-
Action: nil,
69-
Commands: []*cli.Command{lintCmd, createCmd, versionCmd},
84+
Name: "commitlint",
85+
Usage: "linter for conventional commits",
86+
Action: nil,
87+
Commands: []*cli.Command{
88+
createCmd,
89+
initCmd,
90+
lintCmd,
91+
versionCmd,
92+
},
7093
}
7194
}

cmd/commitlint/main.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,7 @@ func main() {
3131
}
3232
}
3333

34-
func initConfCallback(ctx *cli.Context) error {
35-
err := commitlint.DefaultConfToFile(defConfFileName)
36-
if err != nil {
37-
return cli.Exit(err, exitCode)
38-
}
39-
return nil
40-
}
41-
42-
func initHookCallback(ctx *cli.Context) (retErr error) {
34+
func initCallback(ctx *cli.Context) (retErr error) {
4335
// get user home dir
4436
homeDir, err := os.UserHomeDir()
4537
if err != nil {

0 commit comments

Comments
 (0)