22package cmd
33
44import (
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-
4333func 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
5851func 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
8587func 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}
0 commit comments