Skip to content

Commit b2beda2

Browse files
committed
refactor(state): decoupling state and lang, prepare for module separation
1 parent 9053416 commit b2beda2

File tree

3 files changed

+66
-49
lines changed

3 files changed

+66
-49
lines changed

state/lang.go

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,48 @@
11
package state
22

3-
// LangPack ...
4-
type LangPack struct {
3+
import "fmt"
4+
5+
func LoadLanguage(language string) LangPack {
6+
if l, ok := langs[language]; ok {
7+
return l
8+
}
9+
return langEn
10+
}
11+
12+
// langPack ...
13+
type langPack struct {
514
Hints map[State]string
615
Rule string
716
}
817

18+
func (l *langPack) GetHint(state State, v ...interface{}) string {
19+
return fmt.Sprintf(l.Hints[state], v...)
20+
}
21+
22+
func (l *langPack) GetRule(types string) string {
23+
return fmt.Sprintf(l.Rule, types)
24+
}
25+
926
var (
10-
langEn = &LangPack{
27+
langEn = &langPack{
1128
Hints: map[State]string{
12-
Validated: "Validated: commit message meet the rule.\n",
13-
Merge: "Merge: merge commit detected,skip check.\n",
14-
ArgumentMissing: "Error ArgumentMissing: commit message file argument missing.\n",
15-
FileMissing: "Error FileMissing: file %s not exists.\n",
16-
ReadError: "Error ReadError: read file %s error.\n",
17-
EmptyMessage: "Error EmptyMessage: commit message has no content except whitespaces.\n",
18-
EmptyHeader: "Error EmptyHeader: header (first line) has no content except whitespaces.\n",
29+
Validated: "Validated: commit message meet the rule.",
30+
Merge: "Merge: merge commit detected,skip check.",
31+
ArgumentMissing: "Error ArgumentMissing: commit message file argument missing.",
32+
FileMissing: "Error FileMissing: file %s not exists.",
33+
ReadError: "Error ReadError: read file %s error.",
34+
EmptyMessage: "Error EmptyMessage: commit message has no content except whitespaces.",
35+
EmptyHeader: "Error EmptyHeader: header (first line) has no content except whitespaces.",
1936
BadHeaderFormat: `Error BadHeaderFormat: header (first line) not following the rule:
2037
%s
2138
if you can not find any error after check, maybe you use Chinese colon, or lack of whitespace after the colon.`,
22-
WrongType: "Error WrongType: %s, type should be one of the keywords:\n%s\n",
23-
ScopeMissing: "Error ScopeMissing: (scope) is required right after type.\n",
24-
WrongScope: "Error WrongScope: %s, scope should be one of the keywords:\n%s\n",
25-
BodyMissing: "Error BodyMissing: body has no content except whitespaces.\n",
26-
NoBlankLineBeforeBody: "Error NoBlankLineBeforeBody: no empty line between header and body.\n",
27-
LineOverLong: "Error LineOverLong: the length of line is %d, exceed %d:\n%s\n",
28-
UndefindedError: "Error UndefindedError: unexpected error occurs, please raise an issue.\n",
39+
WrongType: "Error WrongType: %s, type should be one of the keywords:\n%s",
40+
ScopeMissing: "Error ScopeMissing: (scope) is required right after type.",
41+
WrongScope: "Error WrongScope: %s, scope should be one of the keywords:\n%s",
42+
BodyMissing: "Error BodyMissing: body has no content except whitespaces.",
43+
NoBlankLineBeforeBody: "Error NoBlankLineBeforeBody: no empty line between header and body.",
44+
LineOverLong: "Error LineOverLong: the length of line is %d, exceed %d:\n%s",
45+
UndefindedError: "Error UndefindedError: unexpected error occurs, please raise an issue.",
2946
},
3047
Rule: `Commit message rule as follow:
3148
<type>(<scope>): <subject>
@@ -39,25 +56,25 @@ var (
3956
more specific instructions, please refer to: https://github.com/JayceChant/commit-msg`,
4057
}
4158

42-
langZhCn = &LangPack{
59+
langZhCn = &langPack{
4360
Hints: map[State]string{
44-
Validated: "Validated: 提交信息符合规范。\n",
45-
Merge: "Merge: 合并提交,跳过规范检查。\n",
46-
ArgumentMissing: "Error ArgumentMissing: 缺少文件参数。\n",
47-
FileMissing: "Error FileMissing: 文件 %s 不存在。\n",
48-
ReadError: "Error ReadError: 读取 %s 错误。\n",
49-
EmptyMessage: "Error EmptyMessage: 提交信息没有内容(不包括空白字符)。\n",
50-
EmptyHeader: "Error EmptyHeader: 标题(第一行)没有内容(不包括空白字符)。\n",
61+
Validated: "Validated: 提交信息符合规范。",
62+
Merge: "Merge: 合并提交,跳过规范检查。",
63+
ArgumentMissing: "Error ArgumentMissing: 缺少文件参数。",
64+
FileMissing: "Error FileMissing: 文件 %s 不存在。",
65+
ReadError: "Error ReadError: 读取 %s 错误。",
66+
EmptyMessage: "Error EmptyMessage: 提交信息没有内容(不包括空白字符)。",
67+
EmptyHeader: "Error EmptyHeader: 标题(第一行)没有内容(不包括空白字符)。",
5168
BadHeaderFormat: `Error BadHeaderFormat: 标题(第一行)不符合规范:
5269
%s
5370
如果您无法发现错误,请注意是否使用了中文冒号,或者冒号后面缺少空格。`,
54-
WrongType: "Error WrongType: %s, 类型关键字应为以下选项中的一个:\n%s\n",
55-
ScopeMissing: "Error ScopeMissing: 类型后面缺少'(scope)'。\n",
56-
WrongScope: "Error WrongScope: %s, 范围关键字应为以下选项中的一个:\n%s\n",
57-
BodyMissing: "Error BodyMissing: 消息体没有内容(不包括空白字符)。\n",
58-
NoBlankLineBeforeBody: "Error NoBlankLineBeforeBody: 标题和消息体之间缺少空行。\n",
59-
LineOverLong: "Error LineOverLong: 该行长度为 %d, 超出了 %d 的限制:\n%s\n",
60-
UndefindedError: "Error UndefindedError: 没有预料到的错误,请提交一个错误报告。\n",
71+
WrongType: "Error WrongType: %s, 类型关键字应为以下选项中的一个:\n%s",
72+
ScopeMissing: "Error ScopeMissing: 类型后面缺少'(scope)'。",
73+
WrongScope: "Error WrongScope: %s, 范围关键字应为以下选项中的一个:\n%s",
74+
BodyMissing: "Error BodyMissing: 消息体没有内容(不包括空白字符)。",
75+
NoBlankLineBeforeBody: "Error NoBlankLineBeforeBody: 标题和消息体之间缺少空行。",
76+
LineOverLong: "Error LineOverLong: 该行长度为 %d, 超出了 %d 的限制:\n%s",
77+
UndefindedError: "Error UndefindedError: 没有预料到的错误,请提交一个错误报告。",
6178
},
6279
Rule: `提交信息规范如下:
6380
<type>(<scope>): <subject>
@@ -71,7 +88,7 @@ var (
7188
更多信息,请参考项目主页: https://github.com/JayceChant/commit-msg`,
7289
}
7390

74-
langs = map[string]*LangPack{
91+
langs = map[string]*langPack{
7592
"en": langEn,
7693
"zh": langZhCn,
7794
"zh-CN": langZhCn,

state/state.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ import (
66
)
77

88
var (
9-
lang *LangPack
9+
lang LangPack
1010
types string
11-
// type check
12-
_ error = State(0)
1311
)
1412

1513
// Init ...
16-
func Init(l string, t string) {
17-
var ok bool
18-
if lang, ok = langs[l]; !ok {
19-
lang = langEn
20-
}
21-
types = t
14+
func Init(language LangPack, typeStr string) {
15+
lang = language
16+
types = typeStr
17+
}
18+
19+
type LangPack interface {
20+
GetHint(state State, v ...interface{}) string
21+
GetRule(types string) string
2222
}
2323

2424
// State indicate the state of a commit message
25-
type State int
25+
type State int8
2626

2727
// message states
2828
const (
@@ -47,20 +47,20 @@ const (
4747
)
4848

4949
// Error ...
50-
func (state State) Error() string {
51-
return lang.Hints[state]
52-
}
50+
// func (state State) Error() string {
51+
// return lang.Hints[state]
52+
// }
5353

5454
// LogAndExit ...
5555
func (state State) LogAndExit(v ...interface{}) {
56-
log.Printf(state.Error(), v...)
56+
log.Println(lang.GetHint(state, v...))
5757

5858
if state.IsNormal() {
5959
os.Exit(0)
6060
}
6161

6262
if state.IsFormatError() {
63-
log.Printf(lang.Rule, types)
63+
log.Println(lang.GetRule(types))
6464
}
6565

6666
os.Exit(int(state))

validator/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,5 @@ func init() {
117117
}
118118
TypesStr = strings.Join(types, ", ")
119119

120-
state.Init(Config.Lang, TypesStr)
120+
state.Init(state.LoadLanguage(Config.Lang), TypesStr)
121121
}

0 commit comments

Comments
 (0)