Skip to content

Commit 51dcad3

Browse files
feat(cmd): add config, hook and remove create, verify cmds
1 parent 539fbd7 commit 51dcad3

File tree

2 files changed

+94
-97
lines changed

2 files changed

+94
-97
lines changed

cmd/callback.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,21 @@ func Lint(confPath, msgPath string) error {
3939
return nil
4040
}
4141

42-
// CreateHook is the callback function for create hook command
43-
func CreateHook(confPath string, isReplace bool) error {
42+
// HookCreate is the callback function for create hook command
43+
func HookCreate(confPath string, isReplace bool) error {
4444
return createHooks(confPath, isReplace)
4545
}
4646

47-
// CreateConfig is the callback function for create config command
48-
func CreateConfig(onlyEnabled bool) error {
47+
// ConfigCreate is the callback function for create config command
48+
func ConfigCreate(onlyEnabled bool) error {
4949
return config.DefaultConfToFile(onlyEnabled)
5050
}
5151

52-
// VerifyConfig is the callback function for verify command
53-
func VerifyConfig(confFlag string) error {
54-
confPath, useDefault, err := config.GetConfigPath(confFlag)
52+
// ConfigCheck is the callback function for check/verify command
53+
func ConfigCheck(confPath string) error {
54+
conf, err := config.Parse(confPath)
5555
if err != nil {
5656
return err
5757
}
58-
59-
if useDefault {
60-
fmt.Println("no config file found, default config will be used")
61-
return nil
62-
}
63-
64-
_, _, err = getLinter(confPath)
65-
if err != nil {
66-
return err
67-
}
68-
69-
fmt.Printf("%s config is valid\n", confPath)
70-
return nil
58+
return config.Validate(conf)
7159
}

cmd/cmd.go

Lines changed: 86 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ func NewWith(versionNo, commitHash, builtTime string) *cli.App {
1919
cmds := []*cli.Command{
2020
initCmd(),
2121
lintCmd(),
22-
createCmd(),
23-
verifyCmd(),
22+
configCmd(),
23+
hookCmd(),
2424
}
2525

2626
app := &cli.App{
@@ -32,27 +32,46 @@ func NewWith(versionNo, commitHash, builtTime string) *cli.App {
3232
return app
3333
}
3434

35-
func initCmd() *cli.Command {
35+
func lintCmd() *cli.Command {
3636
return &cli.Command{
37-
Name: "init",
38-
Usage: "setup commitlint for git repos",
37+
Name: "lint",
38+
Usage: "Check commit message against lint rules",
3939
Flags: []cli.Flag{
40-
&cli.BoolFlag{
41-
Name: "global",
42-
Aliases: []string{"g"},
43-
Usage: "sets git hook in global config",
44-
},
4540
&cli.StringFlag{
4641
Name: "config",
4742
Aliases: []string{"c", "conf"},
4843
Value: "",
4944
Usage: "optional config file `conf.yaml`",
5045
},
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`",
5451
},
5552
},
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},
5675
Action: func(ctx *cli.Context) error {
5776
confPath := ctx.String("config")
5877
isGlobal := ctx.Bool("global")
@@ -74,46 +93,68 @@ func initCmd() *cli.Command {
7493
}
7594
}
7695

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",
81100
Flags: []cli.Flag{
82101
&cli.BoolFlag{
83102
Name: "enabled",
84103
Aliases: []string{"e"},
85-
Usage: "writes only default enabled rules to file",
104+
Usage: "writes only default enabled rules",
86105
Value: false,
87106
},
88107
},
89108
Action: func(ctx *cli.Context) error {
90109
isOnlyEnabled := ctx.Bool("enabled")
91-
return CreateConfig(isOnlyEnabled)
110+
return ConfigCreate(isOnlyEnabled)
92111
},
93112
}
94113

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",
98117
Flags: []cli.Flag{
99118
&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,
108123
},
109124
},
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},
110151
Action: func(ctx *cli.Context) error {
111152
confPath := ctx.String("config")
112153
isReplace := ctx.Bool("replace")
113-
err := CreateHook(confPath, isReplace)
154+
err := HookCreate(confPath, isReplace)
114155
if err != nil {
115156
if isHookExists(err) {
116-
fmt.Println("create hook failed. files already exists")
157+
fmt.Println("create failed. hook files already exists")
117158
fmt.Println("run with --replace to replace existing hook files")
118159
return nil
119160
}
@@ -125,56 +166,24 @@ func createCmd() *cli.Command {
125166
}
126167

127168
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},
134172
}
135173
}
136174

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",
160181
}
161182
}
162183

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",
179188
}
180189
}

0 commit comments

Comments
 (0)