-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate.go
59 lines (50 loc) · 984 Bytes
/
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
package main
import (
"bytes"
"io/ioutil"
"os"
"strings"
"text/template"
)
func executeTemplateFile(input string) (string, error) {
var (
err error
outputBytes bytes.Buffer
outputString string
)
inputBytes, err := readInput(input)
if err != nil {
return "", err
}
tmpl := template.New(input)
tmpl.Funcs(getFuncMap())
tmpl, err = tmpl.Parse(string(inputBytes))
if err != nil {
return "", err
}
if Strict == true {
tmpl.Option("missingkey=error")
}
if len(ctx) == 0 {
loadContext()
}
err = tmpl.Execute(&outputBytes, ctx)
if err != nil {
return "", err
}
outputString = outputBytes.String()
outputString = strings.ReplaceAll(outputString, "<no value>", "")
return outputString, nil
}
func readInput(input string) ([]byte, error) {
var (
err error
inputBytes []byte
)
if input == "-" {
inputBytes, err = ioutil.ReadAll(os.Stdin)
} else {
inputBytes, err = ioutil.ReadFile(input)
}
return inputBytes, err
}