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
|
package templates
import (
"bytes"
"fmt"
"io"
"os"
"path"
"text/template"
"github.com/mitchellh/go-homedir"
)
func findTemplate(templateName string, templateDirs []string) (string, error) {
for _, dir := range templateDirs {
templateFile, err := homedir.Expand(path.Join(dir, templateName))
if err != nil {
return "", err
}
if _, err := os.Stat(templateFile); os.IsNotExist(err) {
continue
}
return templateFile, nil
}
return "", fmt.Errorf(
"Can't find template %q in any of %v ", templateName, templateDirs)
}
func ParseTemplateFromFile(templateName string, templateDirs []string, data interface{}) (io.Reader, error) {
templateFile, err := findTemplate(templateName, templateDirs)
if err != nil {
return nil, err
}
emailTemplate, err := template.New(templateName).
Funcs(templateFuncs).ParseFiles(templateFile)
if err != nil {
return nil, err
}
var body bytes.Buffer
if err := emailTemplate.Execute(&body, data); err != nil {
return nil, err
}
return &body, nil
}
func CheckTemplate(templateName string, templateDirs []string) error {
if templateName != "" {
_, err := ParseTemplateFromFile(templateName, templateDirs, DummyData())
return err
}
return nil
}
|