Skip to content

Commit d043997

Browse files
feat!: change to new design
* main package is moved to root * lint contains linter * message contains commit message and parser wrap * config contains helpers for linter, parsing config file, defaults * rule contains linter rules * formatter contains linter result formatting * cmd contains cli
1 parent fc978e0 commit d043997

40 files changed

+1611
-777
lines changed

cmd/callback.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/urfave/cli/v2"
9+
10+
"github.com/conventionalcommit/commitlint/config"
11+
"github.com/conventionalcommit/commitlint/hook"
12+
)
13+
14+
const (
15+
// ErrExitCode represent error exit code
16+
ErrExitCode = 1
17+
18+
// HookDir represent default hook directory
19+
HookDir = ".commitlint/hooks"
20+
)
21+
22+
// VersionCallback is the version command callback
23+
var VersionCallback = func() error {
24+
fmt.Println("commitlint")
25+
return nil
26+
}
27+
28+
func initCallback(ctx *cli.Context) (retErr error) {
29+
// get user home dir
30+
homeDir, err := os.UserHomeDir()
31+
if err != nil {
32+
return err
33+
}
34+
35+
// create hooks dir
36+
hookDir := filepath.Join(homeDir, filepath.Clean(HookDir))
37+
err = os.MkdirAll(hookDir, os.ModePerm)
38+
if err != nil {
39+
return err
40+
}
41+
42+
// create hook file
43+
err = hook.WriteToFile(hookDir)
44+
if err != nil {
45+
return err
46+
}
47+
48+
isGlobal := ctx.Bool("global")
49+
return setGitConf(hookDir, isGlobal)
50+
}
51+
52+
func lintCallback(ctx *cli.Context) error {
53+
confFilePath := ctx.String("config")
54+
fileInput := ctx.String("message")
55+
56+
resStr, hasError, err := runLint(confFilePath, fileInput)
57+
if err != nil {
58+
return cli.Exit(err, ErrExitCode)
59+
}
60+
61+
if hasError {
62+
return cli.Exit(resStr, ErrExitCode)
63+
}
64+
65+
fmt.Println(resStr)
66+
return nil
67+
}
68+
69+
func hookCreateCallback(ctx *cli.Context) (retErr error) {
70+
err := hook.WriteToFile(".")
71+
if err != nil {
72+
return cli.Exit(err, ErrExitCode)
73+
}
74+
return nil
75+
}
76+
77+
func configCreateCallback(ctx *cli.Context) error {
78+
isOnlyEnabled := ctx.Bool("enabled")
79+
err := config.DefaultConfToFile(isOnlyEnabled)
80+
if err != nil {
81+
return cli.Exit(err, ErrExitCode)
82+
}
83+
return nil
84+
}

cmd/commitlint/cmd.go renamed to cmd/cmd.go

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,33 @@
1-
package main
1+
// Package cmd contains commitlint cli
2+
package cmd
23

34
import (
4-
"fmt"
5-
65
"github.com/urfave/cli/v2"
7-
8-
"github.com/conventionalcommit/commitlint"
96
)
107

11-
func getApp() *cli.App {
8+
// GetApp returns commitlint cli.App
9+
func GetApp() *cli.App {
1210
createCmd := &cli.Command{
1311
Name: "create",
14-
Usage: "for creating commitlint conf, hooks",
12+
Usage: "create commitlint config, hooks files",
1513
Subcommands: []*cli.Command{
1614
{
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
15+
Name: "config",
16+
Usage: "creates commitlint.yaml in current directory",
17+
Action: configCreateCallback,
18+
Flags: []cli.Flag{
19+
&cli.BoolFlag{
20+
Name: "enabled",
21+
Aliases: []string{"e"},
22+
Usage: "writes only default enabled rules to file",
23+
Value: false,
24+
},
2525
},
2626
},
2727
{
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
36-
},
28+
Name: "hook",
29+
Usage: "creates commit-msg file in current directory",
30+
Action: hookCreateCallback,
3731
},
3832
},
3933
}
@@ -46,7 +40,7 @@ func getApp() *cli.App {
4640
&cli.BoolFlag{
4741
Name: "global",
4842
Aliases: []string{"g"},
49-
Usage: "sets git hook config in global",
43+
Usage: "sets git hook in global config",
5044
},
5145
},
5246
}
@@ -75,8 +69,7 @@ func getApp() *cli.App {
7569
Name: "version",
7670
Usage: "prints commitlint version",
7771
Action: func(c *cli.Context) error {
78-
fmt.Printf("commitlint - %s\n", commitlint.Version)
79-
return nil
72+
return VersionCallback()
8073
},
8174
}
8275

cmd/commitlint/main.go

Lines changed: 0 additions & 161 deletions
This file was deleted.

0 commit comments

Comments
 (0)