generated from axetroy/go-cli-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path4_generator.go
228 lines (195 loc) · 5.22 KB
/
4_generator.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package whatchanged
import (
"bytes"
"embed"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"regexp"
sprig "github.com/Masterminds/sprig/v3"
"github.com/pkg/errors"
"github.com/release-lab/whatchanged/internal/client"
)
//go:embed template/*.tpl
var TemplateFS embed.FS
// Get all regex functions which use a compiled regex cache.
func getRegexFuncs(regexCache map[string]*regexp.Regexp) template.FuncMap {
return map[string]interface{}{
// Regex (but cached)
"regexMatch": func(regex string, s string) bool {
r, exists := regexCache[regex]
if !exists {
r = regexp.MustCompile(regex)
regexCache[regex] = r
}
return r.MatchString(s)
},
"mustRegexMatch": func(regex string, s string) (bool, error) {
r, exists := regexCache[regex]
if !exists {
r, err := regexp.Compile(regex)
if err != nil {
return false, errors.WithStack(err)
}
regexCache[regex] = r
}
return r.MatchString(s), nil
},
"regexFindAll": func(regex string, s string, n int) []string {
r, exists := regexCache[regex]
if !exists {
r = regexp.MustCompile(regex)
regexCache[regex] = r
}
return r.FindAllString(s, n)
},
"mustRegexFindAll": func(regex string, s string, n int) ([]string, error) {
r, exists := regexCache[regex]
if !exists {
r, err := regexp.Compile(regex)
if err != nil {
return []string{}, errors.WithStack(err)
}
regexCache[regex] = r
}
return r.FindAllString(s, n), nil
},
"regexFind": func(regex string, s string) string {
r, exists := regexCache[regex]
if !exists {
r = regexp.MustCompile(regex)
regexCache[regex] = r
}
return r.FindString(s)
},
"mustRegexFind": func(regex string, s string) (string, error) {
r, exists := regexCache[regex]
if !exists {
r, err := regexp.Compile(regex)
if err != nil {
return "", errors.WithStack(err)
}
regexCache[regex] = r
}
return r.FindString(s), nil
},
"regexReplaceAll": func(regex string, s string, repl string) string {
r, exists := regexCache[regex]
if !exists {
r = regexp.MustCompile(regex)
regexCache[regex] = r
}
return r.ReplaceAllString(s, repl)
},
"mustRegexReplaceAll": func(regex string, s string, repl string) (string, error) {
r, exists := regexCache[regex]
if !exists {
r, err := regexp.Compile(regex)
if err != nil {
return "", errors.WithStack(err)
}
regexCache[regex] = r
}
return r.ReplaceAllString(s, repl), nil
},
"regexReplaceAllLiteral": func(regex string, s string, repl string) string {
r, exists := regexCache[regex]
if !exists {
r = regexp.MustCompile(regex)
regexCache[regex] = r
}
return r.ReplaceAllLiteralString(s, repl)
},
"mustRegexReplaceAllLiteral": func(regex string, s string, repl string) (string, error) {
r, exists := regexCache[regex]
if !exists {
r, err := regexp.Compile(regex)
if err != nil {
return "", errors.WithStack(err)
}
regexCache[regex] = r
}
return r.ReplaceAllLiteralString(s, repl), nil
},
"regexSplit": func(regex string, s string, n int) []string {
r, exists := regexCache[regex]
if !exists {
r = regexp.MustCompile(regex)
regexCache[regex] = r
}
return r.Split(s, n)
},
"mustRegexSplit": func(regex string, s string, n int) ([]string, error) {
r, exists := regexCache[regex]
if !exists {
r, err := regexp.Compile(regex)
if err != nil {
return []string{}, errors.WithStack(err)
}
regexCache[regex] = r
}
return r.Split(s, n), nil
},
}
}
func GenerateFromContext(g *client.GitClient, contexts []*TemplateContext, format EnumFormat, preset EnumPreset, templateFile string, templateStr string) ([]byte, error) {
switch format {
case FormatJSON:
output, err := json.Marshal(contexts)
if err != nil {
return nil, errors.WithStack(err)
}
return output, nil
case FormatMarkdown:
var output []byte
if templateStr != "" {
// ignore
} else if templateFile == "" {
switch preset {
case PresetDefault:
fallthrough
case PresetFull:
b, err := TemplateFS.ReadFile(fmt.Sprintf("template/%s.tpl", preset))
if err != nil {
return nil, errors.WithStack(err)
}
templateStr = string(b)
default:
return nil, errors.Errorf("invalid preset '%s'", preset)
}
} else {
if b, err := ioutil.ReadFile(templateFile); err != nil {
return nil, errors.WithStack(err)
} else {
templateStr = string(b)
}
}
regexCache := make(map[string]*regexp.Regexp)
for _, ctx := range contexts {
t := template.New(ctx.Version)
// commom function for sprig
t.Funcs(sprig.FuncMap())
// overwrite regex function with cached one for better performance.
t.Funcs(getRegexFuncs(regexCache))
// custom function
t.Funcs(template.FuncMap{
"unescape": func(s string) template.HTML {
return template.HTML(s)
},
})
if t, err := t.Parse(templateStr); err != nil {
return nil, errors.WithStack(err)
} else {
b := bytes.NewBuffer(nil)
if err := t.Execute(b, ctx); err != nil {
return nil, errors.WithStack(err)
}
output = append(output, b.Bytes()...)
}
}
return output, nil
default:
return nil, errors.Errorf("invalid format '%s'", format)
}
}