-
Notifications
You must be signed in to change notification settings - Fork 3
/
template.go
617 lines (508 loc) · 13.3 KB
/
template.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
package render
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"html"
htmlTemplate "html/template"
"io/ioutil"
"net"
"net/http"
"os"
"regexp"
"strconv"
"strings"
txtTemplate "text/template"
"time"
"github.com/blues/jsonata-go"
"github.com/tidwall/gjson"
"github.com/Masterminds/sprig/v3"
"github.com/devopsext/tools/common"
"github.com/devopsext/tools/vendors"
utils "github.com/devopsext/utils"
)
type TemplateOptions struct {
Name string
Object string
Content string
Files []string
TimeFormat string
Pattern string
}
type Template struct {
options TemplateOptions
logger common.Logger
}
type TextTemplate struct {
Template
template *txtTemplate.Template
}
type HtmlTemplate struct {
Template
template *htmlTemplate.Template
}
// put errors to logger
func (tpl *Template) fLogError(obj interface{}, args ...interface{}) (string, error) {
if tpl.logger == nil {
return "", nil
}
tpl.logger.Error(obj, args...)
return "", nil
}
// put warnings to logger
func (tpl *Template) fLogWarn(obj interface{}, args ...interface{}) (string, error) {
if tpl.logger == nil {
return "", nil
}
tpl.logger.Warn(obj, args...)
return "", nil
}
// put warnings to logger
func (tpl *Template) fLogDebug(obj interface{}, args ...interface{}) (string, error) {
if tpl.logger == nil {
return "", nil
}
tpl.logger.Debug(obj, args...)
return "", nil
}
// put information to logger
func (tpl *Template) fLogInfo(obj interface{}, args ...interface{}) (string, error) {
if tpl.logger == nil {
return "", nil
}
tpl.logger.Info(obj, args...)
return "", nil
}
// replaceAll replaces all occurrences of a value in a string with the given
// replacement value.
func (tpl *Template) fReplaceAll(f, t, s string) (string, error) {
return strings.Replace(s, f, t, -1), nil
}
// regexReplaceAll replaces all occurrences of a regular expression with
// the given replacement value.
func (tpl *Template) fRegexReplaceAll(re, pl, s string) (string, error) {
compiled, err := regexp.Compile(re)
if err != nil {
return "", err
}
return compiled.ReplaceAllString(s, pl), nil
}
// regexMatch returns true or false if the string matches
// the given regular expression
func (tpl *Template) fRegexMatch(re, s string) (bool, error) {
compiled, err := regexp.Compile(re)
if err != nil {
return false, err
}
return compiled.MatchString(s), nil
}
// toLower converts the given string (usually by a pipe) to lowercase.
func (tpl *Template) fToLower(s string) (string, error) {
return strings.ToLower(s), nil
}
// toTitle converts the given string (usually by a pipe) to titlecase.
func (tpl *Template) fToTitle(s string) (string, error) {
return strings.Title(s), nil
}
// toUpper converts the given string (usually by a pipe) to uppercase.
func (tpl *Template) fToUpper(s string) (string, error) {
return strings.ToUpper(s), nil
}
// toJSON converts the given structure into a deeply nested JSON string.
func (tpl *Template) fToJSON(i interface{}) (string, error) {
result, err := json.Marshal(i)
if err != nil {
return "", err
}
return string(bytes.TrimSpace(result)), err
}
// split is a version of strings.Split that can be piped
func (tpl *Template) fSplit(sep, s string) ([]string, error) {
s = strings.TrimSpace(s)
if s == "" {
return []string{}, nil
}
return strings.Split(s, sep), nil
}
// join is a version of strings.Join that can be piped
func (tpl *Template) fJoin(sep string, a []string) (string, error) {
return strings.Join(a, sep), nil
}
func (tpl *Template) fIsEmpty(s string) (bool, error) {
s1 := strings.TrimSpace(s)
return len(s1) == 0, nil
}
func (tpl *Template) fEnv(key string) (string, error) {
return utils.EnvGet(key, "").(string), nil
}
func (tpl *Template) fTimeFormat(s string, format string) (string, error) {
t, err := time.Parse(tpl.options.TimeFormat, s)
if err != nil {
return s, err
}
return t.Format(format), nil
}
func (tpl *Template) fTimeNano(s string) (string, error) {
t1, err := time.Parse(time.RFC3339Nano, s)
if err != nil {
return "", err
}
return strconv.FormatInt(t1.UnixNano(), 10), nil
}
func (tpl *Template) fJsonEscape(s string) (string, error) {
bytes, err := json.Marshal(s)
if err != nil {
return "", err
}
return string(bytes), nil
}
// toString converts the given value to string
func (tpl *Template) fToString(i interface{}) (string, error) {
if i != nil {
return fmt.Sprintf("%v", i), nil
}
return "", nil
}
func (tpl *Template) fEscapeString(s string) (string, error) {
return html.EscapeString(s), nil
}
func (tpl *Template) fUnescapeString(s string) (string, error) {
return html.UnescapeString(s), nil
}
func (tpl *Template) fJsonata(data interface{}, query string) (string, error) {
if utils.IsEmpty(query) {
return "", errors.New("query is empty")
}
if _, err := os.Stat(query); err == nil {
content, err := ioutil.ReadFile(query)
if err != nil {
return "", err
}
query = string(content)
}
e, err := jsonata.Compile(query)
if err != nil {
return "", err
}
s, ok := data.(string) // possibly json as string
if ok {
var v interface{}
err = json.Unmarshal([]byte(s), &v)
if err != nil {
return "", err
}
data = v
}
m, err := e.Eval(data)
if err != nil {
return "", err
}
ret := ""
_, ok = m.(map[string]interface{}) // could be as object
if ok {
b, err := common.JsonMarshal(m)
if err != nil {
return "", err
}
ret = strings.TrimSpace(string(b)) // issue with adding new line
} else {
ret = fmt.Sprintf("%v", m)
}
return ret, nil
}
func (tpl *Template) fGjson(obj interface{}, path string) (string, error) {
if utils.IsEmpty(path) {
err := errors.New("path is empty")
return "", err
}
if obj == nil {
err := errors.New("object is not defined")
return "", err
}
bytes, err := common.JsonMarshal(obj)
if err != nil {
return "", err
}
value := gjson.Get(string(bytes), path)
return value.String(), nil
}
func (tpl *Template) fIfDef(i interface{}, def string) (string, error) {
if utils.IsEmpty(i) {
return def, nil
}
return tpl.fToString(i)
}
func (tpl *Template) fContent(s string) (string, error) {
if utils.IsEmpty(s) {
return "", nil
}
bytes, err := utils.Content(s)
if err != nil {
return "", err
}
return string(bytes), nil
}
func (tpl *Template) tryToWaitUntil(t time.Time, timeout time.Duration) {
t2 := time.Now()
diff := t2.Sub(t)
if diff < timeout {
time.Sleep(timeout - diff)
}
}
func (tpl *Template) fURLWait(url string, timeout, retry int, size int64) []byte {
if utils.IsEmpty(url) {
return nil
}
if retry <= 0 {
retry = 1
}
tpl.fLogInfo("fURLWait url => %s [%d, %d, %d]", url, timeout, retry, size)
var transport = &http.Transport{
Dial: (&net.Dialer{Timeout: time.Duration(timeout) * time.Second}).Dial,
TLSHandshakeTimeout: time.Duration(timeout) * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := http.Client{
Timeout: time.Duration(timeout) * time.Second,
Transport: transport,
}
for i := 0; i < retry; i++ {
t1 := time.Now()
tpl.fLogInfo("fURLWait(%d) get %s...", i, url)
data, err := common.HttpGetRaw(&client, url, "", "")
if err != nil {
tpl.fLogInfo("fURLWait(%d) get %s err => %s", i, url, err.Error())
tpl.tryToWaitUntil(t1, client.Timeout)
continue
}
l := int64(len(data))
tpl.fLogInfo("fURLWait(%d) %s len(data) = %d", i, url, l)
if l < size {
tpl.tryToWaitUntil(t1, client.Timeout)
continue
} else if l >= size {
return data
}
}
return nil
}
func (tpl *Template) fGitlabPipelineVars(URL string, token string, projectID int, query string, limit int) string {
gitlabOptions := vendors.GitlabOptions{
Timeout: 30,
Insecure: false,
URL: URL,
Token: token,
}
gitlab, err := vendors.NewGitlab(gitlabOptions)
if err != nil {
tpl.fLogInfo("fGitlabPipelineVars err => %s", err.Error())
return ""
}
if limit <= 0 {
limit = 100
}
pipelineOptions := vendors.GitlabPipelineOptions{
ProjectID: projectID,
Scope: "finished",
OrderBy: "updated_at",
Sort: "desc",
Limit: limit,
}
pipelineGetVariablesOptions := vendors.GitlabPipelineGetVariablesOptions{
Query: strings.Split(query, ","),
}
b, err := gitlab.PipelineGetVariables(pipelineOptions, pipelineGetVariablesOptions)
if err != nil {
tpl.fLogInfo("fGitlabPipelineVars err => %s", err.Error())
return ""
}
return string(b)
}
func (tpl *Template) fTagExists(s, key string) (bool, error) {
// DataDog tags
tags := strings.Split(s, ",")
if len(tags) > 0 {
for _, tag := range tags {
kv := strings.Split(tag, ":")
k := ""
if len(kv) > 0 {
k = kv[0]
}
if strings.TrimSpace(k) == strings.TrimSpace(key) {
return true, nil
}
}
}
return false, nil
}
func (tpl *Template) fTagValue(s, key string) (string, error) {
// DataDog tags
tags := strings.Split(s, ",")
if len(tags) > 0 {
for _, tag := range tags {
kv := strings.Split(tag, ":")
k := ""
v := ""
if len(kv) > 0 {
k = kv[0]
}
if len(kv) > 1 {
v = kv[1]
}
if strings.TrimSpace(k) == strings.TrimSpace(key) {
return v, nil
}
}
}
return s, nil
}
func (tpl *Template) setTemplateFuncs(funcs map[string]interface{}) {
funcs["logError"] = tpl.fLogError
funcs["logWarn"] = tpl.fLogWarn
funcs["logDebug"] = tpl.fLogDebug
funcs["logInfo"] = tpl.fLogInfo
funcs["regexReplaceAll"] = tpl.fRegexReplaceAll
funcs["regexMatch"] = tpl.fRegexMatch
funcs["replaceAll"] = tpl.fReplaceAll
funcs["toLower"] = tpl.fToLower
funcs["toTitle"] = tpl.fToTitle
funcs["toUpper"] = tpl.fToUpper
funcs["toJSON"] = tpl.fToJSON
funcs["split"] = tpl.fSplit
funcs["join"] = tpl.fJoin
funcs["isEmpty"] = tpl.fIsEmpty
funcs["env"] = tpl.fEnv
funcs["getEnv"] = tpl.fEnv
funcs["timeFormat"] = tpl.fTimeFormat
funcs["timeNano"] = tpl.fTimeNano
funcs["jsonEscape"] = tpl.fJsonEscape
funcs["toString"] = tpl.fToString
funcs["escapeString"] = tpl.fEscapeString
funcs["unescapeString"] = tpl.fUnescapeString
funcs["jsonata"] = tpl.fJsonata
funcs["gjson"] = tpl.fGjson
funcs["ifDef"] = tpl.fIfDef
funcs["content"] = tpl.fContent
funcs["urlWait"] = tpl.fURLWait
funcs["gitlabPipelineVars"] = tpl.fGitlabPipelineVars
funcs["tagExists"] = tpl.fTagExists
funcs["tagValue"] = tpl.fTagValue
}
func (tpl *TextTemplate) customRender(name string, obj interface{}) ([]byte, error) {
var b bytes.Buffer
var err error
if empty, _ := tpl.fIsEmpty(name); empty {
err = tpl.template.Execute(&b, obj)
} else {
err = tpl.template.ExecuteTemplate(&b, name, obj)
}
if err != nil {
return nil, err
}
return b.Bytes(), nil
}
func (tpl *TextTemplate) CustomRenderWithOptions(opts TemplateOptions) ([]byte, error) {
var obj interface{}
if !utils.IsEmpty(opts.Object) {
err := json.Unmarshal([]byte(opts.Object), &obj)
if err != nil {
return nil, err
}
}
return tpl.customRender(tpl.options.Name, obj)
}
func (tpl *TextTemplate) Render() ([]byte, error) {
return tpl.CustomRenderWithOptions(tpl.options)
}
func (tpl *TextTemplate) RenderObject(obj interface{}) ([]byte, error) {
return tpl.customRender(tpl.options.Name, obj)
}
func NewTextTemplate(options TemplateOptions, logger common.Logger) (*TextTemplate, error) {
if utils.IsEmpty(options.Content) {
return nil, errors.New("no content")
}
var tpl = TextTemplate{}
var t *txtTemplate.Template
funcs := sprig.TxtFuncMap()
tpl.setTemplateFuncs(funcs)
t, err := txtTemplate.New(options.Name).Funcs(funcs).Parse(options.Content)
if err != nil {
return nil, err
}
if !utils.IsEmpty(options.Files) {
t, err = t.ParseFiles(options.Files...)
if err != nil {
return nil, err
}
}
if !utils.IsEmpty(options.Pattern) {
t, err = t.ParseGlob(options.Pattern)
if err != nil {
return nil, err
}
}
tpl.template = t
tpl.options = options
tpl.logger = logger
return &tpl, nil
}
func (tpl *HtmlTemplate) customRender(name string, obj interface{}) ([]byte, error) {
var b bytes.Buffer
var err error
if empty, _ := tpl.fIsEmpty(name); empty {
err = tpl.template.Execute(&b, obj)
} else {
err = tpl.template.ExecuteTemplate(&b, name, obj)
}
if err != nil {
return nil, err
}
return b.Bytes(), nil
}
func (tpl *HtmlTemplate) CustomRenderWithOptions(opts TemplateOptions) ([]byte, error) {
var obj interface{}
if !utils.IsEmpty(opts.Object) {
err := json.Unmarshal([]byte(opts.Object), &obj)
if err != nil {
return nil, err
}
}
return tpl.customRender(tpl.options.Name, obj)
}
func (tpl *HtmlTemplate) Render() ([]byte, error) {
return tpl.CustomRenderWithOptions(tpl.options)
}
func (tpl *HtmlTemplate) RenderObject(obj interface{}) ([]byte, error) {
return tpl.customRender(tpl.options.Name, obj)
}
func NewHtmlTemplate(options TemplateOptions, logger common.Logger) (*HtmlTemplate, error) {
if utils.IsEmpty(options.Content) {
return nil, errors.New("no content")
}
var tpl = HtmlTemplate{}
var t *htmlTemplate.Template
funcs := sprig.HtmlFuncMap()
tpl.setTemplateFuncs(funcs)
t, err := htmlTemplate.New(options.Name).Funcs(funcs).Parse(options.Content)
if err != nil {
return nil, err
}
if !utils.IsEmpty(options.Files) {
t, err = t.ParseFiles(options.Files...)
if err != nil {
return nil, err
}
}
if !utils.IsEmpty(options.Pattern) {
t, err = t.ParseGlob(options.Pattern)
if err != nil {
return nil, err
}
}
tpl.template = t
tpl.options = options
tpl.logger = logger
return &tpl, nil
}