summaryrefslogtreecommitdiff
path: root/context.go
blob: d84a2967e68bfbf022fd01b33ccf7fbf3160120b (plain)
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
package gomplate

import (
	"context"
	"os"
	"strings"

	"github.com/hairyhenderson/gomplate/v4/internal/datafs"
	"github.com/hairyhenderson/gomplate/v4/internal/parsers"
)

// 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
}

// createTmplContext reads the datasources for the given aliases
func createTmplContext(
	ctx context.Context, aliases []string,
	sr datafs.DataSourceReader,
) (interface{}, error) {
	tctx := &tmplctx{}
	for _, a := range aliases {
		ct, b, err := sr.ReadSource(ctx, a)
		if err != nil {
			return nil, err
		}

		content, err := parsers.ParseData(ct, string(b))
		if err != nil {
			return nil, err
		}

		if a == "." {
			return content, nil
		}

		(*tctx)[a] = content
	}
	return tctx, nil
}