2
2
package cmd
3
3
4
4
import (
5
- "fmt"
6
- "runtime/debug"
7
-
8
5
"github.com/urfave/cli/v2"
9
6
)
10
7
11
8
// 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 {
13
15
versionInfo := formVersionInfo (versionNo , commitHash , builtTime )
14
16
15
17
cmds := []* cli.Command {
16
- createCmd (),
17
18
initCmd (),
18
19
lintCmd (),
20
+ createCmd (),
19
21
verifyCmd (),
20
- versionCmd (versionInfo ),
21
22
}
22
23
23
24
app := & cli.App {
24
25
Name : "commitlint" ,
25
26
Usage : "linter for conventional commits" ,
26
27
Commands : cmds ,
27
- Action : nil ,
28
+ Version : versionInfo ,
28
29
}
29
30
return app
30
31
}
31
32
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
-
43
33
func initCmd () * cli.Command {
44
34
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" ,
48
37
Flags : []cli.Flag {
49
38
& cli.BoolFlag {
50
39
Name : "global" ,
51
40
Aliases : []string {"g" },
52
41
Usage : "sets git hook in global config" ,
53
42
},
54
43
},
44
+ Action : func (ctx * cli.Context ) error {
45
+ isGlobal := ctx .Bool ("global" )
46
+ return Init (isGlobal )
47
+ },
55
48
}
56
49
}
57
50
58
51
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
+
59
77
return & cli.Command {
60
78
Name : "create" ,
61
79
Usage : "create commitlint config, hooks files" ,
62
80
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 ,
81
83
},
82
84
}
83
85
}
84
86
85
87
func lintCmd () * cli.Command {
86
88
return & cli.Command {
87
- Name : "lint" ,
88
- Usage : "lints commit message" ,
89
- Action : lintCallback ,
89
+ Name : "lint" ,
90
+ Usage : "lints commit message" ,
90
91
Flags : []cli.Flag {
91
92
& cli.StringFlag {
92
93
Name : "config" ,
@@ -101,6 +102,11 @@ func lintCmd() *cli.Command {
101
102
Usage : "path to git commit message `FILE`" ,
102
103
},
103
104
},
105
+ Action : func (ctx * cli.Context ) error {
106
+ confFilePath := ctx .String ("config" )
107
+ fileInput := ctx .String ("message" )
108
+ return Lint (confFilePath , fileInput )
109
+ },
104
110
}
105
111
}
106
112
@@ -116,35 +122,9 @@ func verifyCmd() *cli.Command {
116
122
Usage : "optional config file `conf.yaml`" ,
117
123
},
118
124
},
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
+ },
143
129
}
144
-
145
- versionInfo = info .Main .Version
146
- commitInfo = "(" + "checksum: " + checkSum + ")"
147
- buildTime = "unknown"
148
-
149
- return versionInfo , commitInfo , buildTime
150
130
}
0 commit comments