Skip to content

Commit 28325db

Browse files
feat: separate cli commands and callbacks
* rename New to NewWith * add New which calls NewWith with empty version info * remove version command, set version info to cli.App
1 parent 020f9a7 commit 28325db

File tree

4 files changed

+94
-99
lines changed

4 files changed

+94
-99
lines changed

cmd/callback.go

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ const (
1818
HookDir = ".commitlint/hooks"
1919
)
2020

21-
func initCallback(ctx *cli.Context) (retErr error) {
22-
isGlobal := ctx.Bool("global")
23-
21+
// Init is the callback function for init command
22+
func Init(isGlobal bool) error {
2423
hookDir, err := getHookDir(isGlobal)
2524
if err != nil {
2625
return err
@@ -46,11 +45,10 @@ func initCallback(ctx *cli.Context) (retErr error) {
4645
return nil
4746
}
4847

49-
func lintCallback(ctx *cli.Context) error {
50-
confFilePath := ctx.String("config")
51-
fileInput := ctx.String("message")
52-
53-
resStr, hasError, err := runLint(confFilePath, fileInput)
48+
// Lint is the callback function for lint command
49+
func Lint(confPath, msgPath string) error {
50+
// NOTE: lint should return with exit code for error case
51+
resStr, hasError, err := runLint(confPath, msgPath)
5452
if err != nil {
5553
return cli.Exit(err, ErrExitCode)
5654
}
@@ -59,33 +57,26 @@ func lintCallback(ctx *cli.Context) error {
5957
return cli.Exit(resStr, ErrExitCode)
6058
}
6159

60+
// print success message
6261
fmt.Println(resStr)
6362
return nil
6463
}
6564

66-
func hookCreateCallback(ctx *cli.Context) (retErr error) {
67-
err := hook.WriteToFile(".")
68-
if err != nil {
69-
return cli.Exit(err, ErrExitCode)
70-
}
71-
return nil
65+
// CreateHook is the callback function for create hook command
66+
func CreateHook() (retErr error) {
67+
return hook.WriteToFile(".")
7268
}
7369

74-
func configCreateCallback(ctx *cli.Context) error {
75-
isOnlyEnabled := ctx.Bool("enabled")
76-
err := config.DefaultConfToFile(isOnlyEnabled)
77-
if err != nil {
78-
return cli.Exit(err, ErrExitCode)
79-
}
80-
return nil
70+
// CreateConfig is the callback function for create config command
71+
func CreateConfig(onlyEnabled bool) error {
72+
return config.DefaultConfToFile(onlyEnabled)
8173
}
8274

83-
func verifyCallback(ctx *cli.Context) error {
84-
confFlag := ctx.String("config")
85-
75+
// VerifyConfig is the callback function for verify command
76+
func VerifyConfig(confFlag string) error {
8677
confPath, useDefault, err := config.GetConfigPath(confFlag)
8778
if err != nil {
88-
return cli.Exit(err, ErrExitCode)
79+
return err
8980
}
9081

9182
if useDefault {
@@ -95,7 +86,7 @@ func verifyCallback(ctx *cli.Context) error {
9586

9687
_, _, err = getLinter(confPath)
9788
if err != nil {
98-
return cli.Exit(err, ErrExitCode)
89+
return err
9990
}
10091

10192
fmt.Printf("%s config is valid\n", confPath)

cmd/cmd.go

Lines changed: 52 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,91 +2,92 @@
22
package cmd
33

44
import (
5-
"fmt"
6-
"runtime/debug"
7-
85
"github.com/urfave/cli/v2"
96
)
107

118
// New returns commitlint cli.App
12-
func New(versionNo, commitHash, builtTime string) *cli.App {
9+
func New() *cli.App {
10+
return NewWith("", "", "")
11+
}
12+
13+
// NewWith returns commitlint cli.App with version info
14+
func NewWith(versionNo, commitHash, builtTime string) *cli.App {
1315
versionInfo := formVersionInfo(versionNo, commitHash, builtTime)
1416

1517
cmds := []*cli.Command{
16-
createCmd(),
1718
initCmd(),
1819
lintCmd(),
20+
createCmd(),
1921
verifyCmd(),
20-
versionCmd(versionInfo),
2122
}
2223

2324
app := &cli.App{
2425
Name: "commitlint",
2526
Usage: "linter for conventional commits",
2627
Commands: cmds,
27-
Action: nil,
28+
Version: versionInfo,
2829
}
2930
return app
3031
}
3132

32-
func versionCmd(versionInfo string) *cli.Command {
33-
return &cli.Command{
34-
Name: "version",
35-
Usage: "prints commitlint version",
36-
Action: func(c *cli.Context) error {
37-
fmt.Printf(versionInfo)
38-
return nil
39-
},
40-
}
41-
}
42-
4333
func initCmd() *cli.Command {
4434
return &cli.Command{
45-
Name: "init",
46-
Usage: "setup commitlint for git repos",
47-
Action: initCallback,
35+
Name: "init",
36+
Usage: "setup commitlint for git repos",
4837
Flags: []cli.Flag{
4938
&cli.BoolFlag{
5039
Name: "global",
5140
Aliases: []string{"g"},
5241
Usage: "sets git hook in global config",
5342
},
5443
},
44+
Action: func(ctx *cli.Context) error {
45+
isGlobal := ctx.Bool("global")
46+
return Init(isGlobal)
47+
},
5548
}
5649
}
5750

5851
func createCmd() *cli.Command {
52+
configCmd := &cli.Command{
53+
Name: "config",
54+
Usage: "creates commitlint.yaml in current directory",
55+
Flags: []cli.Flag{
56+
&cli.BoolFlag{
57+
Name: "enabled",
58+
Aliases: []string{"e"},
59+
Usage: "writes only default enabled rules to file",
60+
Value: false,
61+
},
62+
},
63+
Action: func(ctx *cli.Context) error {
64+
isOnlyEnabled := ctx.Bool("enabled")
65+
return CreateConfig(isOnlyEnabled)
66+
},
67+
}
68+
69+
hookCmd := &cli.Command{
70+
Name: "hook",
71+
Usage: "creates commit-msg file in current directory",
72+
Action: func(ctx *cli.Context) error {
73+
return CreateHook()
74+
},
75+
}
76+
5977
return &cli.Command{
6078
Name: "create",
6179
Usage: "create commitlint config, hooks files",
6280
Subcommands: []*cli.Command{
63-
{
64-
Name: "config",
65-
Usage: "creates commitlint.yaml in current directory",
66-
Action: configCreateCallback,
67-
Flags: []cli.Flag{
68-
&cli.BoolFlag{
69-
Name: "enabled",
70-
Aliases: []string{"e"},
71-
Usage: "writes only default enabled rules to file",
72-
Value: false,
73-
},
74-
},
75-
},
76-
{
77-
Name: "hook",
78-
Usage: "creates commit-msg file in current directory",
79-
Action: hookCreateCallback,
80-
},
81+
configCmd,
82+
hookCmd,
8183
},
8284
}
8385
}
8486

8587
func lintCmd() *cli.Command {
8688
return &cli.Command{
87-
Name: "lint",
88-
Usage: "lints commit message",
89-
Action: lintCallback,
89+
Name: "lint",
90+
Usage: "lints commit message",
9091
Flags: []cli.Flag{
9192
&cli.StringFlag{
9293
Name: "config",
@@ -101,6 +102,11 @@ func lintCmd() *cli.Command {
101102
Usage: "path to git commit message `FILE`",
102103
},
103104
},
105+
Action: func(ctx *cli.Context) error {
106+
confFilePath := ctx.String("config")
107+
fileInput := ctx.String("message")
108+
return Lint(confFilePath, fileInput)
109+
},
104110
}
105111
}
106112

@@ -116,35 +122,9 @@ func verifyCmd() *cli.Command {
116122
Usage: "optional config file `conf.yaml`",
117123
},
118124
},
119-
Action: verifyCallback,
120-
}
121-
}
122-
123-
func formVersionInfo(versionInfo, commitInfo, buildTime string) string {
124-
versionTmpl := `commitlint version %s - built from %s on %s
125-
`
126-
versionInfo, commitInfo, buildTime = getVersionInfo(versionInfo, commitInfo, buildTime)
127-
return fmt.Sprintf(versionTmpl, versionInfo, commitInfo, buildTime)
128-
}
129-
130-
func getVersionInfo(version, commit, build string) (versionInfo, commitInfo, buildTime string) {
131-
if build != "" {
132-
return version, commit, build
133-
}
134-
135-
info, ok := debug.ReadBuildInfo()
136-
if !ok {
137-
return "master", "unknown", "unknown"
138-
}
139-
140-
checkSum := "unknown"
141-
if info.Main.Sum != "" {
142-
checkSum = info.Main.Sum
125+
Action: func(ctx *cli.Context) error {
126+
confFilePath := ctx.String("config")
127+
return VerifyConfig(confFilePath)
128+
},
143129
}
144-
145-
versionInfo = info.Main.Version
146-
commitInfo = "(" + "checksum: " + checkSum + ")"
147-
buildTime = "unknown"
148-
149-
return versionInfo, commitInfo, buildTime
150130
}

cmd/util.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package cmd
22

33
import (
44
"bytes"
5+
"fmt"
56
"io"
67
"os"
78
"os/exec"
89
"path/filepath"
10+
"runtime/debug"
911
"strings"
1012

1113
"github.com/conventionalcommit/commitlint/config"
@@ -150,3 +152,25 @@ func getRepoRootDir() (string, error) {
150152

151153
return gitDir, nil
152154
}
155+
156+
func formVersionInfo(version, commit, build string) string {
157+
versionTmpl := "%s - built from %s on %s"
158+
159+
if build != "" {
160+
return fmt.Sprintf(versionTmpl, version, commit, build)
161+
}
162+
163+
info, ok := debug.ReadBuildInfo()
164+
if !ok {
165+
return fmt.Sprintf(versionTmpl, "master", "unknown", "unknown")
166+
}
167+
168+
var commitInfo string
169+
if info.Main.Sum == "" {
170+
commitInfo = "(" + "checksum: unknown)"
171+
} else {
172+
commitInfo = "(" + "checksum: " + info.Main.Sum + ")"
173+
}
174+
175+
return fmt.Sprintf(versionTmpl, info.Main.Version, commitInfo, "unknown")
176+
}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var (
1616
)
1717

1818
func main() {
19-
app := cmd.New(Version, Commit, BuildTime)
19+
app := cmd.NewWith(Version, Commit, BuildTime)
2020
err := app.Run(os.Args)
2121
if err != nil {
2222
fmt.Println("Error:", err)

0 commit comments

Comments
 (0)