summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2019-10-26 14:11:19 -0400
committerDave Henderson <dhenderson@gmail.com>2019-10-26 14:17:22 -0400
commit1c1f0f5be4436a6454e17e166f0e950ff30b48c3 (patch)
tree2b1cd8f7e063345235a35d11a93706f7773dd6a3
parent3dc820699130f73dd086469f1a385b60164ead33 (diff)
Refactor context naming to reduce confusion
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
-rw-r--r--context.go16
-rw-r--r--context_test.go14
-rw-r--r--gomplate.go26
-rw-r--r--plugins.go4
-rw-r--r--template.go2
-rw-r--r--tmpl/tmpl.go6
6 files changed, 34 insertions, 34 deletions
diff --git a/context.go b/context.go
index ab50a86a..6bc70f2e 100644
--- a/context.go
+++ b/context.go
@@ -8,10 +8,10 @@ import (
)
// context for templates
-type context map[string]interface{}
+type tmplctx map[string]interface{}
// Env - Map environment variables for use in a template
-func (c *context) Env() map[string]string {
+func (c *tmplctx) Env() map[string]string {
env := make(map[string]string)
for _, i := range os.Environ() {
sep := strings.Index(i, "=")
@@ -20,20 +20,20 @@ func (c *context) Env() map[string]string {
return env
}
-func createContext(contexts []string, d *data.Data) (interface{}, error) {
+func createTmplContext(contexts []string, d *data.Data) (interface{}, error) {
var err error
- ctx := &context{}
- for _, context := range contexts {
- a := parseAlias(context)
+ tctx := &tmplctx{}
+ for _, c := range contexts {
+ a := parseAlias(c)
if a == "." {
return d.Datasource(a)
}
- (*ctx)[a], err = d.Datasource(a)
+ (*tctx)[a], err = d.Datasource(a)
if err != nil {
return nil, err
}
}
- return ctx, nil
+ return tctx, nil
}
func parseAlias(arg string) string {
diff --git a/context_test.go b/context_test.go
index f2780356..37362104 100644
--- a/context_test.go
+++ b/context_test.go
@@ -11,20 +11,20 @@ import (
)
func TestEnvMapifiesEnvironment(t *testing.T) {
- c := &context{}
+ c := &tmplctx{}
env := c.Env()
assert.Equal(t, env["USER"], os.Getenv("USER"))
}
func TestEnvGetsUpdatedEnvironment(t *testing.T) {
- c := &context{}
+ c := &tmplctx{}
assert.Empty(t, c.Env()["FOO"])
assert.NoError(t, os.Setenv("FOO", "foo"))
assert.Equal(t, c.Env()["FOO"], "foo")
}
func TestCreateContext(t *testing.T) {
- c, err := createContext(nil, nil)
+ c, err := createTmplContext(nil, nil)
assert.NoError(t, err)
assert.Empty(t, c)
@@ -40,16 +40,16 @@ func TestCreateContext(t *testing.T) {
}
os.Setenv("foo", "foo: bar")
defer os.Unsetenv("foo")
- c, err = createContext([]string{"foo=" + fooURL}, d)
+ c, err = createTmplContext([]string{"foo=" + fooURL}, d)
assert.NoError(t, err)
- assert.IsType(t, &context{}, c)
- ctx := c.(*context)
+ assert.IsType(t, &tmplctx{}, c)
+ ctx := c.(*tmplctx)
ds := ((*ctx)["foo"]).(map[string]interface{})
assert.Equal(t, "bar", ds["foo"])
os.Setenv("bar", "bar: baz")
defer os.Unsetenv("bar")
- c, err = createContext([]string{".=" + barURL}, d)
+ c, err = createTmplContext([]string{".=" + barURL}, d)
assert.NoError(t, err)
assert.IsType(t, map[string]interface{}{}, c)
ds = c.(map[string]interface{})
diff --git a/gomplate.go b/gomplate.go
index ec3a62ad..a7dcb19d 100644
--- a/gomplate.go
+++ b/gomplate.go
@@ -24,7 +24,7 @@ type gomplate struct {
rightDelim string
nestedTemplates templateAliases
rootTemplate *template.Template
- context interface{}
+ tmplctx interface{}
}
// runTemplate -
@@ -42,20 +42,20 @@ func (g *gomplate) runTemplate(t *tplate) error {
defer t.target.(io.Closer).Close()
}
}
- err = tmpl.Execute(t.target, g.context)
+ err = tmpl.Execute(t.target, g.tmplctx)
return err
}
type templateAliases map[string]string
// newGomplate -
-func newGomplate(funcMap template.FuncMap, leftDelim, rightDelim string, nested templateAliases, context interface{}) *gomplate {
+func newGomplate(funcMap template.FuncMap, leftDelim, rightDelim string, nested templateAliases, tctx interface{}) *gomplate {
return &gomplate{
leftDelim: leftDelim,
rightDelim: rightDelim,
funcMap: funcMap,
nestedTemplates: nested,
- context: context,
+ tmplctx: tctx,
}
}
@@ -122,7 +122,7 @@ func RunTemplates(o *Config) error {
if err != nil {
return err
}
- c, err := createContext(o.Contexts, d)
+ c, err := createTmplContext(o.Contexts, d)
if err != nil {
return err
}
@@ -186,22 +186,22 @@ func mappingNamer(outMap string, g *gomplate) func(string) (string, error) {
if err != nil {
return "", err
}
- ctx := &context{}
+ tctx := &tmplctx{}
// nolint: gocritic
- switch c := g.context.(type) {
- case *context:
+ switch c := g.tmplctx.(type) {
+ case *tmplctx:
for k, v := range *c {
if k != "in" && k != "ctx" {
- (*ctx)[k] = v
+ (*tctx)[k] = v
}
}
}
- (*ctx)["ctx"] = g.context
- (*ctx)["in"] = inPath
+ (*tctx)["ctx"] = g.tmplctx
+ (*tctx)["in"] = inPath
- err = tpl.Execute(t.target, ctx)
+ err = tpl.Execute(t.target, tctx)
if err != nil {
- return "", errors.Wrapf(err, "failed to render outputMap with ctx %+v and inPath %s", ctx, inPath)
+ return "", errors.Wrapf(err, "failed to render outputMap with ctx %+v and inPath %s", tctx, inPath)
}
return filepath.Clean(strings.TrimSpace(out.String())), nil
diff --git a/plugins.go b/plugins.go
index 9300afba..d009353a 100644
--- a/plugins.go
+++ b/plugins.go
@@ -2,7 +2,7 @@ package gomplate
import (
"bytes"
- gcontext "context"
+ "context"
"errors"
"fmt"
"os"
@@ -90,7 +90,7 @@ func (p *plugin) run(args ...interface{}) (interface{}, error) {
return nil, err
}
- ctx, cancel := gcontext.WithTimeout(gcontext.Background(), t)
+ ctx, cancel := context.WithTimeout(context.Background(), t)
defer cancel()
c := exec.CommandContext(ctx, name, a...)
c.Stdin = nil
diff --git a/template.go b/template.go
index df7d604f..d6452b7f 100644
--- a/template.go
+++ b/template.go
@@ -55,7 +55,7 @@ func (t *tplate) toGoTemplate(g *gomplate) (tmpl *template.Template, err error)
}
tmpl.Option("missingkey=error")
// the "tmpl" funcs get added here because they need access to the root template and context
- addTmplFuncs(g.funcMap, g.rootTemplate, g.context)
+ addTmplFuncs(g.funcMap, g.rootTemplate, g.tmplctx)
tmpl.Funcs(g.funcMap)
tmpl.Delims(g.leftDelim, g.rightDelim)
_, err = tmpl.Parse(t.contents)
diff --git a/tmpl/tmpl.go b/tmpl/tmpl.go
index cf57cee1..e0c46c20 100644
--- a/tmpl/tmpl.go
+++ b/tmpl/tmpl.go
@@ -43,10 +43,10 @@ func (t *Template) inline(name, in string, ctx interface{}) (string, error) {
}
// Exec - execute (render) a template - this is the built-in `template` action, except with output...
-func (t *Template) Exec(name string, context ...interface{}) (string, error) {
+func (t *Template) Exec(name string, tmplcontext ...interface{}) (string, error) {
ctx := t.defaultCtx
- if len(context) == 1 {
- ctx = context[0]
+ if len(tmplcontext) == 1 {
+ ctx = tmplcontext[0]
}
tmpl := t.root.Lookup(name)
if tmpl == nil {