-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
147 lines (128 loc) · 3.13 KB
/
config.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package chlog
import (
"github.com/gookit/goutil/maputil"
"github.com/gookit/goutil/strutil"
)
// Config struct
type Config struct {
// Title string for formatted text. eg: "## Change Log"
Title string `json:"title" yaml:"title"`
// RepoURL repo URL address
RepoURL string `json:"repo_url" yaml:"repo_url"`
// Style name. allow: simple, markdown, ghr
Style string `json:"style" yaml:"style"`
// LogFormat built-in log format string.
//
// use on the `git log --pretty="format:%H"`.
//
// see consts LogFmt*, eg: LogFmtHs
LogFormat string `json:"log_format" yaml:"log_format"`
// GroupPrefix string. eg: '### '
GroupPrefix string `yaml:"group_prefix"`
// GroupPrefix string.
GroupSuffix string `yaml:"group_suffix"`
// NoGroup Not output group name line.
NoGroup bool `yaml:"no_group"`
// RmRepeat remove repeated log by message
RmRepeat bool `json:"rm_repeat" yaml:"rm_repeat"`
// Verbose show more information
Verbose bool `json:"verbose" yaml:"verbose"`
// Names define group names and sort
Names []string `json:"names" yaml:"names"`
// Rules for match group
Rules []Rule `json:"rules" yaml:"rules"`
// Filters for filtering
Filters []maputil.Data `json:"filters" yaml:"filters"`
}
// NewDefaultConfig instance
func NewDefaultConfig() *Config {
return &Config{
Title: "## Change Log",
RmRepeat: true,
LogFormat: LogFmtHs,
GroupPrefix: "\n### ",
GroupSuffix: "\n",
}
}
// Create Changelog
func (c *Config) Create() *Changelog {
cl := NewWithConfig(c)
return cl
}
// CreateFilters for Changelog
func (c *Config) CreateFilters() []ItemFilter {
if len(c.Filters) == 0 {
return nil
}
fls := make([]ItemFilter, 0, len(c.Filters))
/*
- name: keywords
keywords: ['format code']
exclude: true
*/
for _, rule := range c.Filters {
name := rule["name"]
if name == "" {
continue
}
switch name {
case FilterMsgLen:
ln := rule.Int("min_len")
if ln <= 0 {
continue
}
fls = append(fls, MsgLenFilter(ln))
case FilterWordsLen:
ln := rule.Int("min_len")
if ln <= 0 {
continue
}
fls = append(fls, WordsLenFilter(ln))
case FilterKeyword:
str := rule.Str("keyword")
if len(str) <= 0 {
continue
}
fls = append(fls, KeywordFilter(str, rule.Bool("exclude")))
case FilterKeywords:
str := rule.Str("keywords")
ss := strutil.Split(str, ",")
if len(ss) <= 0 {
continue
}
fls = append(fls, KeywordsFilter(ss, rule.Bool("exclude")))
}
}
return fls
}
// CreateFormatter for Changelog
func (c *Config) CreateFormatter() Formatter {
sf := &SimpleFormatter{}
ns := c.Names
matcher := NewDefaultMatcher()
if len(c.Rules) > 0 {
if len(c.Names) == 0 {
ns = maputil.Keys(c.Rules)
}
matcher = &RuleMatcher{Rules: c.Rules}
}
if len(ns) > 0 {
matcher.Names = ns
}
c.Names = matcher.Names
sf.GroupMatch = matcher
switch c.Style {
case FormatterMarkdown, "mkdown", "mkDown", "mkd":
return &MarkdownFormatter{
RepoURL: c.RepoURL,
SimpleFormatter: *sf,
}
case FormatterGhRelease, "gh-release", "ghRelease":
f := &GHReleaseFormatter{}
f.RepoURL = c.RepoURL
f.SimpleFormatter = *sf
return f
default:
return sf
}
}