-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
57 lines (49 loc) · 1.37 KB
/
parse.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
package aeolic
import (
"bufio"
"bytes"
"fmt"
"io/fs"
"path/filepath"
"strings"
"text/template"
)
func parse(templateName string, templateMap map[string]string, data any) ([]byte, error) {
slackTemplates, ok := templateMap[templateName]
if !ok {
return []byte{}, fmt.Errorf("template %s does not exist", templateName)
}
tmpl, err := template.New(templateName).Option("missingkey=error").Parse(slackTemplates)
if err != nil {
return []byte{}, err
}
var buf bytes.Buffer
wr := bufio.NewWriter(&buf)
if err := tmpl.Execute(wr, data); err != nil {
return nil, fmt.Errorf("%w \n %s", err, slackTemplates)
}
if err := wr.Flush(); err != nil {
return buf.Bytes(), err
}
return buf.Bytes(), nil
}
// withTemplates - load templates by file system and path
func withTemplates(fsys fs.FS, dirPath, fileSuffix string) (map[string]string, error) {
rootTemplates := map[string]string{}
files, err := fs.ReadDir(fsys, dirPath)
if err != nil {
return rootTemplates, err
}
for _, file := range files {
fileLocation := filepath.Join(dirPath, file.Name())
if strings.HasSuffix(file.Name(), fileSuffix) {
data, err := fs.ReadFile(fsys, filepath.Clean(fileLocation))
if err != nil {
return rootTemplates, err
}
stripedFileName := strings.TrimSuffix(file.Name(), fileSuffix)
rootTemplates[stripedFileName] = string(data)
}
}
return rootTemplates, nil
}