-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
56 lines (50 loc) · 1.15 KB
/
file.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
package gotemplate
import (
"io/ioutil"
"os"
"github.com/spf13/afero"
)
var fs afero.Fs
// RegisterFS (afero) virtual filesystem for ProcessTemplateFile
func RegisterFS(filesystem afero.Fs) {
fs = filesystem
}
// ProcessTemplateFile processes golang template file
func ProcessTemplateFile(template string, bundle interface{}) ([]byte, error) {
var byteValue []byte
var err error
if fs == nil {
tf, err := os.Open(template)
if err != nil {
return nil, err
}
defer tf.Close()
byteValue, err = ioutil.ReadAll(tf)
} else {
tf, err := fs.Open(template)
if err != nil {
return nil, err
}
byteValue, err = ioutil.ReadAll(tf)
defer tf.Close()
}
if err != nil {
return nil, err
}
output, err := Template(string(byteValue), bundle)
if err != nil {
return []byte{}, err
}
return []byte(output), nil
}
// MustProcessTemplateFile processes golang template file otherwise panics
func MustProcessTemplateFile(template string, bundle interface{}) string {
tf, err := os.Open(template)
if err != nil {
panic(err)
}
byteValue, _ := ioutil.ReadAll(tf)
output, _ := Template(string(byteValue), bundle)
defer tf.Close()
return output
}