Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 908e9fb

Browse files
author
David Chung
authored
New template improvements (#388)
Signed-off-by: David Chung <[email protected]>
1 parent d4de67a commit 908e9fb

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

dockerfiles/Dockerfile.bundle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,11 @@ FROM alpine:3.5
22

33
RUN apk add --update ca-certificates
44

5+
RUN mkdir -p /infrakit/plugins /infrakit/configs /infrakit/logs
6+
7+
VOLUME /infrakit
8+
9+
ENV INFRAKIT_HOME /infrakit
10+
ENV INFRAKIT_PLUGINS_DIR /infrakit/plugins
11+
512
ADD build/* /usr/local/bin/

pkg/template/funcs.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,40 @@ func IndexOf(srch interface{}, array interface{}, strictOptional ...bool) int {
114114
// DefaultFuncs returns a list of default functions for binding in the template
115115
func (t *Template) DefaultFuncs() []Function {
116116
return []Function{
117+
{
118+
Name: "source",
119+
Description: []string{
120+
"Source / evaluate the template at the input location (as URL).",
121+
"This will make all of the global variables declared there visible in this template's context.",
122+
},
123+
Func: func(p string) (string, error) {
124+
loc := p
125+
if strings.Index(loc, "str://") == -1 {
126+
buff, err := getURL(t.url, p)
127+
if err != nil {
128+
return "", err
129+
}
130+
loc = buff
131+
}
132+
sourced, err := NewTemplate(loc, t.options)
133+
if err != nil {
134+
return "", err
135+
}
136+
// copy the binds in the parent scope into the child
137+
for k, v := range t.binds {
138+
sourced.binds[k] = v
139+
}
140+
// inherit the functions defined for this template
141+
for k, v := range t.funcs {
142+
sourced.AddFunc(k, v)
143+
}
144+
// set this as the parent of the sourced template so its global can mutate the globals in this
145+
sourced.parent = t
146+
147+
// TODO(chungers) -- let the sourced template define new functions that can be called in the parent.
148+
return sourced.Render(nil)
149+
},
150+
},
117151
{
118152
Name: "include",
119153
Description: []string{
@@ -187,7 +221,9 @@ func (t *Template) DefaultFuncs() []Function {
187221
"Global variables are propagated to all templates that are rendered via the 'include' function.",
188222
},
189223
Func: func(name string, v interface{}) interface{} {
190-
t.binds[name] = v
224+
for here := t; here != nil; here = here.parent {
225+
here.updateGlobal(name, v)
226+
}
191227
return ""
192228
},
193229
},

pkg/template/integration_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,13 @@ func TestAddDef(t *testing.T) {
169169
require.NoError(t, err)
170170
require.Equal(t, "hello: x + y = 125", view)
171171
}
172+
173+
func TestSourceAndGlobal(t *testing.T) {
174+
r := `{{ global \"foo\" 100 }}`
175+
s := `{{ source "str://` + r + `" }}foo={{ref "foo"}}`
176+
tt, err := NewTemplate("str://"+s, Options{})
177+
require.NoError(t, err)
178+
view, err := tt.Render(nil)
179+
require.NoError(t, err)
180+
require.Equal(t, "foo=100", view)
181+
}

pkg/template/template.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ type Template struct {
7676

7777
registered []Function
7878
lock sync.Mutex
79+
80+
parent *Template
7981
}
8082

8183
// NewTemplate fetches the content at the url and returns a template. If the string begins
@@ -143,6 +145,12 @@ func (t *Template) AddDef(name string, val interface{}, doc ...string) *Template
143145
return t
144146
}
145147

148+
func (t *Template) updateGlobal(name string, value interface{}) {
149+
t.lock.Lock()
150+
defer t.lock.Unlock()
151+
t.binds[name] = value
152+
}
153+
146154
// Validate parses the template and checks for validity.
147155
func (t *Template) Validate() (*Template, error) {
148156
t.lock.Lock()

0 commit comments

Comments
 (0)