forked from hairyhenderson/gomplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
47 lines (41 loc) · 895 Bytes
/
context.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
package gomplate
import (
"os"
"strings"
"github.com/hairyhenderson/gomplate/v3/data"
)
// context for templates
type tmplctx map[string]interface{}
// Env - Map environment variables for use in a template
func (c *tmplctx) Env() map[string]string {
env := make(map[string]string)
for _, i := range os.Environ() {
sep := strings.Index(i, "=")
env[i[0:sep]] = i[sep+1:]
}
return env
}
func createTmplContext(contexts []string, d *data.Data) (interface{}, error) {
var err error
tctx := &tmplctx{}
for _, c := range contexts {
a := parseAlias(c)
if a == "." {
return d.Datasource(a)
}
(*tctx)[a], err = d.Datasource(a)
if err != nil {
return nil, err
}
}
return tctx, nil
}
func parseAlias(arg string) string {
parts := strings.SplitN(arg, "=", 2)
switch len(parts) {
case 1:
return strings.SplitN(parts[0], ".", 2)[0]
default:
return parts[0]
}
}