Skip to content

Commit 562d7cf

Browse files
committed
feat(rule): add reference
1 parent 224097b commit 562d7cf

2 files changed

Lines changed: 72 additions & 36 deletions

File tree

main.go

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,42 @@ import (
1717
const (
1818
commitMsgFilePath = ".git/COMMIT_EDITMSG"
1919
formatDoc = "<type>(<scope>): <subject>"
20-
errorTitle = "\033[0;31m============================ Invalid Commit Message ================================\033[0m"
21-
errorTemplate = "\n%s\ncommit message: \033[0;31m%s\033[0m\ncorrect format: \033[0;92m%s\033[0m\n\n%s\n%s\n\n"
22-
footer = "\033[0;31m====================================================================================\033[0m"
20+
scopeDoc = "The <scope> can be empty (e.g. if the change is a global or difficult to assign to a single component), in which case the parentheses are omitted."
21+
styleDoc = "The type and scope should always be lowercase."
2322
)
2423

24+
func textRed(s string) string {
25+
return fmt.Sprintf("\033[0;31m%s\033[0m", s)
26+
}
27+
28+
func textBrightGreen(s string) string {
29+
return fmt.Sprintf("\033[0;92m%s\033[0m", s)
30+
}
31+
32+
func textBrightYellow(s string) string {
33+
return fmt.Sprintf("\033[0;93m%s\033[0m", s)
34+
}
35+
2536
var (
2637
r = flag.String("rule", "", "select rule file path (config.yaml)")
2738

2839
FormatRegularPattern = `([a-zA-Z]+)(\(.*\))?:\s+(.*)`
2940

30-
scopeDoc = "\033[0;93mThe <scope> can be empty (e.g. if the change is a global or difficult to assign to a single component), in which case the parentheses are omitted.\033[0m"
31-
styleDoc = "\033[0;93mThe type and scope should always be lowercase.\033[0m"
41+
errorTitle = "============================ Invalid Message ================================"
42+
errorTemplate = "\n%s\ntitle message: %s\ncorrect format: %s\n\n%s\n\nSee: %s\n"
43+
footer = "============================================================================="
3244

3345
ErrStyle = errors.New("invalid style error")
3446
ErrType = errors.New("invalid type error")
3547
ErrFormat = errors.New("invalid format error")
3648
ErrScope = errors.New("invalid scope error")
3749

38-
DefaultRules = Config{
50+
DefaultConfig = Config{
3951
SkipPrefixes: []string{
4052
"Merge branch ",
53+
"BREAKING: ",
4154
},
55+
Reference: "https://github.com/masahiro331/go-commitlinter#description",
4256
TypeRules: TypeRules{
4357
{
4458
Type: "feat",
@@ -77,6 +91,8 @@ var (
7791
Description: "for updates that do not apply to the above, such as dependency updates.",
7892
},
7993
},
94+
StyleDoc: styleDoc,
95+
ScopeDoc: scopeDoc,
8096
}
8197
)
8298

@@ -88,9 +104,9 @@ type TypeRule struct {
88104
type TypeRules []TypeRule
89105

90106
func (typeRules TypeRules) String() string {
91-
ret := "Allows type values\n"
107+
ret := "Allowed <type> values\n"
92108
for _, tr := range typeRules {
93-
ret += fmt.Sprintf("\033[0;93m%s\033[0m\t%s\n", tr.Type, tr.Description)
109+
ret += fmt.Sprintf("%s\t%s\n", textBrightYellow(tr.Type), tr.Description)
94110
}
95111

96112
return ret
@@ -99,6 +115,9 @@ func (typeRules TypeRules) String() string {
99115
type Config struct {
100116
SkipPrefixes []string `yaml:"skip_prefixes"`
101117
TypeRules TypeRules `yaml:"type_rules"`
118+
Reference string `yaml:"reference"`
119+
StyleDoc string `yaml:"style_doc"`
120+
ScopeDoc string `yaml:"scope_doc"`
102121
}
103122

104123
type Format struct {
@@ -107,14 +126,9 @@ type Format struct {
107126
Subject string
108127
}
109128

110-
type Linter struct {
111-
Conf Config
112-
Format Format
113-
}
114-
115129
func NewConfig(filepath string) (Config, error) {
116130
if filepath == "" {
117-
return DefaultRules, nil
131+
return DefaultConfig, nil
118132
}
119133

120134
f, err := os.Open(filepath)
@@ -222,6 +236,27 @@ func run() (string, Config, error) {
222236
return "", conf, nil
223237
}
224238

239+
func finally(m string, conf Config, err error) {
240+
message := ""
241+
switch err {
242+
case ErrFormat, ErrType:
243+
message = fmt.Sprintf(errorTemplate, textRed(errorTitle), textRed(m), textBrightGreen(formatDoc), conf.TypeRules, textBrightGreen(conf.Reference))
244+
case ErrStyle:
245+
message = fmt.Sprintf(errorTemplate, textRed(errorTitle), textRed(m), textBrightGreen(formatDoc), textBrightYellow(conf.StyleDoc), textBrightGreen(conf.Reference))
246+
case ErrScope:
247+
message = fmt.Sprintf(errorTemplate, textRed(errorTitle), textRed(m), textBrightGreen(formatDoc), textBrightYellow(conf.ScopeDoc), textBrightGreen(conf.Reference))
248+
case nil:
249+
return
250+
default:
251+
log.Fatal(xerrors.Errorf("unspecified error: %w", err))
252+
}
253+
message = fmt.Sprintf("%s\n%s", message, textRed(footer))
254+
if err != nil {
255+
fmt.Println(message)
256+
os.Exit(1)
257+
}
258+
}
259+
225260
func getMessage() (string, error) {
226261
reader := bufio.NewReader(os.Stdin)
227262
b, _, _ := reader.ReadLine()
@@ -243,26 +278,6 @@ func getMessage() (string, error) {
243278
return string(b), nil
244279
}
245280

246-
func finally(m string, conf Config, err error) {
247-
message := ""
248-
switch err {
249-
case ErrFormat, ErrType:
250-
message = fmt.Sprintf(errorTemplate, errorTitle, m, formatDoc, conf.TypeRules, footer)
251-
case ErrStyle:
252-
message = fmt.Sprintf(errorTemplate, errorTitle, m, formatDoc, styleDoc, footer)
253-
case ErrScope:
254-
message = fmt.Sprintf(errorTemplate, errorTitle, m, formatDoc, scopeDoc, footer)
255-
case nil:
256-
return
257-
default:
258-
log.Fatal(xerrors.Errorf("unspecified error: %w", err))
259-
}
260-
if err != nil {
261-
fmt.Println(message)
262-
os.Exit(1)
263-
}
264-
}
265-
266281
func main() {
267282
finally(run())
268-
}
283+
}

rule-sample.yaml

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
1+
2+
skip_prefixes:
3+
- 'Merge branch '
4+
- 'BREAKING: '
15
type_rules:
26
- type: feat
37
description: for a new feature for the user, not a new feature for build script.
48
- type: fix
5-
description: for a bug fix for the user, not a fix to a build script.
9+
description: for a bug fix for the user, not a fix to a build script.
10+
- type: perf
11+
description: for performance improvements.
12+
- type: docs
13+
description: for changes to the documentation.
14+
- type: style
15+
description: for formatting changes, missing semicolons, etc.
16+
- type: refactor
17+
description: for refactoring production code, e.g. renaming a variable.
18+
- type: test
19+
description: for adding missing tests, refactoring tests; no production code change.
20+
- type: build
21+
description: for updating build configuration, development tools or other changes irrelevant to the user.
22+
- type: chore
23+
description: for updates that do not apply to the above, such as dependency updates.
24+
reference: https://github.com/masahiro331/go-commitlinter#description
25+
style_doc: The type and scope should always be lowercase.
26+
scope_doc: The <scope> can be empty (e.g. if the change is a global or difficult to assign to a single component), in which case the parentheses are omitted.

0 commit comments

Comments
 (0)