-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
49 lines (43 loc) · 1.04 KB
/
utils.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
package notify
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"text/template"
t "text/template"
)
func loadTemplate(path string) (template *t.Template, err error) {
bytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("Failed to open %s template: %s.", path, err)
}
template, err = t.New(path).Parse(string(bytes))
if err != nil {
return nil, fmt.Errorf("Failed to pass %s Go Template: %s", path, err)
}
return
}
func runTemplate(template *template.Template, object interface{}) (result []byte, err error) {
buf := bytes.Buffer{}
err = template.Execute(&buf, object)
if err != nil {
return
}
return buf.Bytes(), nil
}
func testTemplate(template *t.Template, object interface{}) bool {
result, err := runTemplate(template, object)
if err != nil {
log.Print("Failed to run template:", err)
return false
}
return string(result) == "true"
}
func loadAndTestTemplate(path string, object interface{}) bool {
template, err := loadTemplate(path)
if err != nil {
return false
}
return testTemplate(template, object)
}