Skip to content

Commit f89f498

Browse files
refactor: split cli commands into separate func
1 parent d5963f6 commit f89f498

File tree

1 file changed

+54
-39
lines changed

1 file changed

+54
-39
lines changed

cmd/cmd.go

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,58 @@ import (
77
"github.com/urfave/cli/v2"
88
)
99

10-
var verTmpl = `commitlint version %s - built from %s on %s
10+
var versionTmpl = `commitlint version %s - built from %s on %s
1111
`
1212

1313
// New returns commitlint cli.App
1414
func New(versionNo, commitHash, builtTime string) *cli.App {
15-
createCmd := &cli.Command{
15+
versionInfo := fmt.Sprintf(versionTmpl, versionNo, commitHash, builtTime)
16+
17+
cmds := []*cli.Command{
18+
createCmd(),
19+
initCmd(),
20+
lintCmd(),
21+
verifyCmd(),
22+
versionCmd(versionInfo),
23+
}
24+
25+
app := &cli.App{
26+
Name: "commitlint",
27+
Usage: "linter for conventional commits",
28+
Commands: cmds,
29+
Action: nil,
30+
}
31+
return app
32+
}
33+
34+
func versionCmd(versionInfo string) *cli.Command {
35+
return &cli.Command{
36+
Name: "version",
37+
Usage: "prints commitlint version",
38+
Action: func(c *cli.Context) error {
39+
fmt.Printf(versionInfo)
40+
return nil
41+
},
42+
}
43+
}
44+
45+
func initCmd() *cli.Command {
46+
return &cli.Command{
47+
Name: "init",
48+
Usage: "setup commitlint for git repos",
49+
Action: initCallback,
50+
Flags: []cli.Flag{
51+
&cli.BoolFlag{
52+
Name: "global",
53+
Aliases: []string{"g"},
54+
Usage: "sets git hook in global config",
55+
},
56+
},
57+
}
58+
}
59+
60+
func createCmd() *cli.Command {
61+
return &cli.Command{
1662
Name: "create",
1763
Usage: "create commitlint config, hooks files",
1864
Subcommands: []*cli.Command{
@@ -36,21 +82,10 @@ func New(versionNo, commitHash, builtTime string) *cli.App {
3682
},
3783
},
3884
}
85+
}
3986

40-
initCmd := &cli.Command{
41-
Name: "init",
42-
Usage: "setup commitlint for git repos",
43-
Action: initCallback,
44-
Flags: []cli.Flag{
45-
&cli.BoolFlag{
46-
Name: "global",
47-
Aliases: []string{"g"},
48-
Usage: "sets git hook in global config",
49-
},
50-
},
51-
}
52-
53-
lintCmd := &cli.Command{
87+
func lintCmd() *cli.Command {
88+
return &cli.Command{
5489
Name: "lint",
5590
Usage: "lints commit message",
5691
Action: lintCallback,
@@ -69,17 +104,10 @@ func New(versionNo, commitHash, builtTime string) *cli.App {
69104
},
70105
},
71106
}
107+
}
72108

73-
versionCmd := &cli.Command{
74-
Name: "version",
75-
Usage: "prints commitlint version",
76-
Action: func(c *cli.Context) error {
77-
fmt.Printf(verTmpl, versionNo, commitHash, builtTime)
78-
return nil
79-
},
80-
}
81-
82-
verifyCmd := &cli.Command{
109+
func verifyCmd() *cli.Command {
110+
return &cli.Command{
83111
Name: "verify",
84112
Usage: "verifies commitlint config",
85113
Flags: []cli.Flag{
@@ -92,17 +120,4 @@ func New(versionNo, commitHash, builtTime string) *cli.App {
92120
},
93121
Action: verifyCallback,
94122
}
95-
96-
return &cli.App{
97-
Name: "commitlint",
98-
Usage: "linter for conventional commits",
99-
Action: nil,
100-
Commands: []*cli.Command{
101-
createCmd,
102-
initCmd,
103-
lintCmd,
104-
verifyCmd,
105-
versionCmd,
106-
},
107-
}
108123
}

0 commit comments

Comments
 (0)