Skip to content

Commit 2578349

Browse files
committed
feat: move lang, lineLimit and bodyRequired into cfg.json
1 parent e3689f8 commit 2578349

3 files changed

Lines changed: 55 additions & 11 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@
1010

1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
13+
14+
# config file
15+
*.cfg.json

commit-msg.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ func checkHeader(header string) {
7878
// subject := groups[5] // TODO: 根据规则对subject检查
7979

8080
length := len(header)
81-
if length > LINE_LIMIT &&
81+
if length > config.LineLimit &&
8282
!(isFixupOrSquash || type_ == "revert" || type_ == "Revert") {
83-
logAndExit(LineOverLong, length, LINE_LIMIT, header)
83+
logAndExit(LineOverLong, length, config.LineLimit, header)
8484
}
8585
}
8686

8787
func checkBody(body string) {
8888
if checkEmpty(body) {
89-
if BODY_REQUIRED {
89+
if config.BodyRequired {
9090
logAndExit(BodyMissing)
9191
} else {
9292
logAndExit(Validated)
@@ -99,8 +99,8 @@ func checkBody(body string) {
9999

100100
for _, line := range strings.Split(body, "\n") {
101101
length := len(line)
102-
if length > LINE_LIMIT {
103-
logAndExit(LineOverLong, length, LINE_LIMIT, line)
102+
if length > config.LineLimit {
103+
logAndExit(LineOverLong, length, config.LineLimit, line)
104104
}
105105
}
106106
}
@@ -125,7 +125,7 @@ func validateMsg(msg string) {
125125

126126
if len(sections) == 2 {
127127
checkBody(sections[1])
128-
} else if BODY_REQUIRED {
128+
} else if config.BodyRequired {
129129
logAndExit(BodyMissing)
130130
}
131131

config.go

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
package main
22

3+
import (
4+
"encoding/json"
5+
"os"
6+
)
7+
38
const (
49
MERGE_PATTERN = `^Merge `
510
HEADER_PATTERN = `^((fixup! |squash! )?(\w+)(?:\(([^\)\s]+)\))?: (.+))(?:\n|$)`
6-
LINE_LIMIT = 80
7-
BODY_REQUIRED = false
11+
CONFIG_FILE = "commit-msg.cfg.json"
812
)
913

14+
type Config struct {
15+
Lang string
16+
BodyRequired bool
17+
LineLimit int
18+
}
19+
1020
type MsgState int
1121

1222
const (
@@ -29,7 +39,7 @@ const (
2939
)
3040

3141
var (
32-
lang = "zh-CN"
42+
config *Config
3343
stateHint []string
3444
ruleHint string
3545
typeList = [...]string{
@@ -102,9 +112,40 @@ if you can not find any error after check, maybe you use Chinese colon, or lack
102112
}
103113
)
104114

115+
func loadConfig() *Config {
116+
f, err := os.Open(CONFIG_FILE)
117+
if err != nil {
118+
return nil
119+
}
120+
defer f.Close()
121+
dec := json.NewDecoder(f)
122+
var cfg Config
123+
if err := dec.Decode(&cfg); err != nil {
124+
return nil
125+
}
126+
return &cfg
127+
}
128+
129+
func initConfig() *Config {
130+
cfg := &Config{"en", false, 80}
131+
f, err := os.Create(CONFIG_FILE)
132+
if err != nil {
133+
return cfg
134+
}
135+
defer f.Close()
136+
enc := json.NewEncoder(f)
137+
enc.SetIndent("", " ")
138+
enc.Encode(cfg)
139+
return cfg
140+
}
141+
105142
func init() {
106-
// lang = "zh-CN"
107-
switch lang {
143+
config = loadConfig()
144+
if config == nil {
145+
config = initConfig()
146+
}
147+
148+
switch config.Lang {
108149
case "zh-CN":
109150
stateHint = zhCnHint[:]
110151
ruleHint = zhCnRule

0 commit comments

Comments
 (0)