Skip to content

Commit 21bb2fb

Browse files
refactor(cmd): move lint utils to lint.go
1 parent 5a8648e commit 21bb2fb

File tree

2 files changed

+72
-67
lines changed

2 files changed

+72
-67
lines changed

cmd/lint.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
6+
"github.com/conventionalcommit/commitlint/config"
7+
"github.com/conventionalcommit/commitlint/lint"
8+
)
9+
10+
func runLint(confFilePath, fileInput string) (lintResult string, hasError bool, err error) {
11+
linter, format, err := getLinter(confFilePath)
12+
if err != nil {
13+
return "", false, err
14+
}
15+
16+
commitMsg, err := getCommitMsg(fileInput)
17+
if err != nil {
18+
return "", false, err
19+
}
20+
21+
res, err := linter.Lint(commitMsg)
22+
if err != nil {
23+
return "", false, err
24+
}
25+
26+
resStr, err := format.Format(res)
27+
if err != nil {
28+
return "", false, err
29+
}
30+
return resStr, res.HasErrors(), nil
31+
}
32+
33+
func getLinter(confFilePath string) (*lint.Linter, lint.Formatter, error) {
34+
conf, err := config.GetConfig(confFilePath)
35+
if err != nil {
36+
return nil, nil, err
37+
}
38+
39+
format, err := config.GetFormatter(conf)
40+
if err != nil {
41+
return nil, nil, err
42+
}
43+
44+
linter, err := config.GetLinter(conf)
45+
if err != nil {
46+
return nil, nil, err
47+
}
48+
49+
return linter, format, nil
50+
}
51+
52+
func getCommitMsg(fileInput string) (string, error) {
53+
commitMsg, err := readStdInPipe()
54+
if err != nil {
55+
return "", err
56+
}
57+
58+
if commitMsg != "" {
59+
return commitMsg, nil
60+
}
61+
62+
// TODO: check if currentDir is inside git repo?
63+
if fileInput == "" {
64+
fileInput = "./.git/COMMIT_EDITMSG"
65+
}
66+
67+
inBytes, err := os.ReadFile(fileInput)
68+
if err != nil {
69+
return "", err
70+
}
71+
return string(inBytes), nil
72+
}

cmd/util.go

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,8 @@ import (
99
"path/filepath"
1010
"runtime/debug"
1111
"strings"
12-
13-
"github.com/conventionalcommit/commitlint/config"
14-
"github.com/conventionalcommit/commitlint/lint"
1512
)
1613

17-
func runLint(confFilePath, fileInput string) (lintResult string, hasError bool, err error) {
18-
linter, format, err := getLinter(confFilePath)
19-
if err != nil {
20-
return "", false, err
21-
}
22-
23-
commitMsg, err := getCommitMsg(fileInput)
24-
if err != nil {
25-
return "", false, err
26-
}
27-
28-
res, err := linter.Lint(commitMsg)
29-
if err != nil {
30-
return "", false, err
31-
}
32-
33-
resStr, err := format.Format(res)
34-
if err != nil {
35-
return "", false, err
36-
}
37-
return resStr, res.HasErrors(), nil
38-
}
39-
40-
func getLinter(confFilePath string) (*lint.Linter, lint.Formatter, error) {
41-
conf, err := config.GetConfig(confFilePath)
42-
if err != nil {
43-
return nil, nil, err
44-
}
45-
46-
format, err := config.GetFormatter(conf)
47-
if err != nil {
48-
return nil, nil, err
49-
}
50-
51-
linter, err := config.GetLinter(conf)
52-
if err != nil {
53-
return nil, nil, err
54-
}
55-
56-
return linter, format, nil
57-
}
58-
5914
func setGitConf(hookDir string, isGlobal bool) error {
6015
var args = []string{"config"}
6116
if isGlobal {
@@ -68,28 +23,6 @@ func setGitConf(hookDir string, isGlobal bool) error {
6823
return cmd.Run()
6924
}
7025

71-
func getCommitMsg(fileInput string) (string, error) {
72-
commitMsg, err := readStdInPipe()
73-
if err != nil {
74-
return "", err
75-
}
76-
77-
if commitMsg != "" {
78-
return commitMsg, nil
79-
}
80-
81-
// TODO: check if currentDir is inside git repo?
82-
if fileInput == "" {
83-
fileInput = "./.git/COMMIT_EDITMSG"
84-
}
85-
86-
inBytes, err := os.ReadFile(fileInput)
87-
if err != nil {
88-
return "", err
89-
}
90-
return string(inBytes), nil
91-
}
92-
9326
func readStdInPipe() (string, error) {
9427
stat, err := os.Stdin.Stat()
9528
if err != nil {

0 commit comments

Comments
 (0)