forked from prometheus-community/jiralert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
72 lines (65 loc) · 2.02 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
package jiralert
import (
"bytes"
"regexp"
"strings"
"text/template"
log "github.com/golang/glog"
)
// Template wraps a text template and error, to make it easier to execute multiple templates and only check for errors
// once at the end (assuming one is only interested in the first error, which is usually the case).
type Template struct {
tmpl *template.Template
err error
}
var funcs = template.FuncMap{
"toUpper": strings.ToUpper,
"toLower": strings.ToLower,
"title": strings.Title,
// join is equal to strings.Join but inverts the argument order
// for easier pipelining in templates.
"join": func(sep string, s []string) string {
return strings.Join(s, sep)
},
"reReplaceAll": func(pattern, repl, text string) string {
re := regexp.MustCompile(pattern)
return re.ReplaceAllString(text, repl)
},
}
// LoadTemplate reads and parses all templates defined in the given file and constructs a jiralert.Template.
func LoadTemplate(path string) (*Template, error) {
log.V(1).Infof("Loading templates from %q", path)
tmpl, err := template.New("").Option("missingkey=zero").Funcs(funcs).ParseFiles(path)
if err != nil {
return nil, err
}
return &Template{tmpl: tmpl}, nil
}
// Execute parses the provided text (or returns it unchanged if not a Go template), associates it with the templates
// defined in t.tmpl (so they may be referenced and used) and applies the resulting template to the specified data
// object, returning the output as a string.
func (t *Template) Execute(text string, data interface{}) string {
log.V(2).Infof("Executing template %q...", text)
if !strings.Contains(text, "{{") {
log.V(2).Infof(" returning unchanged.")
return text
}
if t.err != nil {
return ""
}
var tmpl *template.Template
tmpl, t.err = t.tmpl.Clone()
if t.err != nil {
return ""
}
tmpl, t.err = tmpl.New("").Parse(text)
if t.err != nil {
log.V(2).Infof(" parse failed.")
return ""
}
var buf bytes.Buffer
t.err = tmpl.Execute(&buf, data)
ret := buf.String()
log.V(2).Infof(" returning %q.", ret)
return ret
}