-
Notifications
You must be signed in to change notification settings - Fork 1
/
formatter.go
105 lines (87 loc) · 2.24 KB
/
formatter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package chlog
import (
"fmt"
)
// Formatter interface
type Formatter interface {
// MatchGroup from log msg
MatchGroup(msg string) (group string)
// Format the log item to line
Format(li *LogItem) (group, fmtLine string)
}
// GroupMatcher interface
type GroupMatcher interface {
// Match group from log msg(has been trimmed)
Match(msg string) (group string)
}
// built-in formatters
const (
FormatterSimple = "simple"
FormatterMarkdown = "markdown"
FormatterGhRelease = "ghr"
)
// SimpleFormatter struct
type SimpleFormatter struct {
// GroupMatch group match handler.
GroupMatch GroupMatcher
}
// MatchGroup from log msg
func (f *SimpleFormatter) MatchGroup(msg string) (group string) {
if f.GroupMatch != nil {
return f.GroupMatch.Match(msg)
}
return DefaultMatcher.Match(msg)
}
// Format the log item to line
func (f *SimpleFormatter) Format(li *LogItem) (group, fmtLine string) {
fmtLine = " - "
if li.HashID != "" {
fmtLine += li.AbbrevID() + " "
}
group = f.MatchGroup(li.Msg)
fmtLine += li.Msg
if user := li.Username(); user != "" {
fmtLine += " by(" + user + ")"
}
return
}
// MarkdownFormatter struct
type MarkdownFormatter struct {
SimpleFormatter
// RepoURL git repo remote URL address
RepoURL string
}
// Format the log item to line
func (f *MarkdownFormatter) Format(li *LogItem) (group, fmtLine string) {
group = f.MatchGroup(li.Msg)
if li.HashID != "" {
// full url.
// eg: https://github.com/inhere/kite/commit/ebd90a304755218726df4eb398fd081c08d04b9a
fmtLine = fmt.Sprintf("- %s [%s](%s/commit/%s)", li.Msg, li.AbbrevID(), f.RepoURL, li.HashID)
} else {
fmtLine = " - " + li.Msg
}
if user := li.Username(); user != "" {
fmtLine += " by(" + user + ")"
}
return
}
// GHReleaseFormatter struct
type GHReleaseFormatter struct {
MarkdownFormatter
}
// Format the log item to line
func (f *GHReleaseFormatter) Format(li *LogItem) (group, fmtLine string) {
group = f.MatchGroup(li.Msg)
if li.HashID != "" {
// full url.
// eg: https://github.com/inhere/kite/commit/ebd90a304755218726df4eb398fd081c08d04b9a
fmtLine = fmt.Sprintf("- %s %s/commit/%s", li.Msg, f.RepoURL, li.HashID)
} else {
fmtLine = " - " + li.Msg
}
if user := li.Username(); user != "" {
fmtLine += " by(@" + user + ")"
}
return
}