Commit 1e745d95 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

template/interpolate: user variables

parent a4b5e08f
...@@ -94,9 +94,13 @@ func funcGenTimestamp(ctx *Context) interface{} { ...@@ -94,9 +94,13 @@ func funcGenTimestamp(ctx *Context) interface{} {
} }
func funcGenUser(ctx *Context) interface{} { func funcGenUser(ctx *Context) interface{} {
return func() string { return func(k string) string {
if ctx == nil || ctx.UserVariables == nil {
return "" return ""
} }
return ctx.UserVariables[k]
}
} }
func funcGenUuid(ctx *Context) interface{} { func funcGenUuid(ctx *Context) interface{} {
......
...@@ -142,3 +142,37 @@ func TestFuncTimestamp(t *testing.T) { ...@@ -142,3 +142,37 @@ func TestFuncTimestamp(t *testing.T) {
} }
} }
} }
func TestFuncUser(t *testing.T) {
cases := []struct {
Input string
Output string
}{
{
`{{user "foo"}}`,
`foo`,
},
{
`{{user "what"}}`,
``,
},
}
ctx := &Context{
UserVariables: map[string]string{
"foo": "foo",
},
}
for _, tc := range cases {
i := &I{Value: tc.Input}
result, err := i.Render(ctx)
if err != nil {
t.Fatalf("Input: %s\n\nerr: %s", tc.Input, err)
}
if result != tc.Output {
t.Fatalf("Input: %s\n\nGot: %s", tc.Input, result)
}
}
}
...@@ -11,6 +11,10 @@ type Context struct { ...@@ -11,6 +11,10 @@ type Context struct {
// Data is the data for the template that is available // Data is the data for the template that is available
Data interface{} Data interface{}
// UserVariables is the mapping of user variables that the
// "user" function reads from.
UserVariables map[string]string
// DisableEnv disables the env function // DisableEnv disables the env function
DisableEnv bool DisableEnv bool
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment