-
Notifications
You must be signed in to change notification settings - Fork 49
/
sigil.go
140 lines (125 loc) · 2.67 KB
/
sigil.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package sigil
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/mgood/go-posix"
)
var (
TemplatePath []string
PosixPreprocess bool
)
var leftDelim = "{{"
var rightDelim = "}}"
var fnMap = template.FuncMap{}
func init() {
delims := os.Getenv("SIGIL_DELIMS")
if delims != "" {
d := strings.Split(delims, ",")
leftDelim = d[0]
rightDelim = d[1]
}
}
type NamedReader struct {
io.Reader
Name string
}
func String(in interface{}) (string, string, bool) {
switch obj := in.(type) {
case string:
return obj, "", true
case NamedReader:
data, err := io.ReadAll(obj)
if err != nil {
// TODO: better overall error/panic handling
panic(err)
}
return string(data), obj.Name, true
case fmt.Stringer:
return obj.String(), "", true
default:
return "", "", false
}
}
func Register(fm template.FuncMap) {
for k, v := range fm {
fnMap[k] = v
}
}
func PushPath(path string) {
TemplatePath = append([]string{path}, TemplatePath...)
}
func PopPath() {
_, TemplatePath = TemplatePath[0], TemplatePath[1:]
}
func LookPath(file string) (string, error) {
if strings.HasPrefix(file, "/") {
if fileExists(file) {
return file, nil
}
} else {
cwd, _ := os.Getwd()
search := append([]string{cwd}, TemplatePath...)
for _, path := range search {
filepath := filepath.Join(path, file)
if fileExists(filepath) {
return filepath, nil
}
}
}
return "", fmt.Errorf("Not found in path: %s %v", file, TemplatePath)
}
func fileExists(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}
func restoreEnv(env []string) {
os.Clearenv()
for _, kvp := range env {
kv := strings.SplitN(kvp, "=", 2)
os.Setenv(kv[0], kv[1])
}
}
func Execute(input []byte, vars map[string]interface{}, name string) (bytes.Buffer, error) {
var tmplVars string
var err error
defer restoreEnv(os.Environ())
for k, iv := range vars {
if v, ok := iv.(string); ok {
err := os.Setenv(k, v)
if err != nil {
return bytes.Buffer{}, err
}
}
tmplVars = tmplVars + fmt.Sprintf("%s $%s := .%s %s", leftDelim, k, k, rightDelim)
}
inputStr := string(input)
if PosixPreprocess {
inputStr, err = posix.ExpandEnv(inputStr)
if err != nil {
return bytes.Buffer{}, err
}
}
inputStr = strings.Replace(
inputStr,
fmt.Sprintf("\\%s\n%s", rightDelim, leftDelim),
fmt.Sprintf("%s%s", rightDelim, leftDelim),
-1,
)
tmpl, err := template.New(name).Funcs(fnMap).Delims(leftDelim, rightDelim).Parse(tmplVars + inputStr)
if err != nil {
return bytes.Buffer{}, err
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, vars)
if err != nil {
return bytes.Buffer{}, err
}
return buf, nil
}