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

Commit fd983ac

Browse files
author
David Chung
authored
reduce number of fetches for dynamic commands (#549)
Signed-off-by: David Chung <[email protected]>
1 parent fcc0579 commit fd983ac

File tree

2 files changed

+58
-24
lines changed

2 files changed

+58
-24
lines changed

pkg/cli/context.go

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -428,14 +428,7 @@ func (c *Context) Funcs() []template.Function {
428428

429429
// loadBackend determines the backend to use for executing the rendered template text (e.g. run in shell).
430430
// During this phase, the template delimiters are changed to =% %= so put this in the comment {{/* */}}
431-
func (c *Context) loadBackends() error {
432-
t, err := template.NewTemplate(c.src, template.Options{
433-
DelimLeft: "=%",
434-
DelimRight: "%=",
435-
})
436-
if err != nil {
437-
return err
438-
}
431+
func (c *Context) loadBackends(t *template.Template) error {
439432
t.AddFunc("print",
440433
func() string {
441434
c.run = func(script string) error {
@@ -567,35 +560,61 @@ func (c *Context) loadBackends() error {
567560
return ""
568561
})
569562

570-
_, err = t.Render(c)
563+
_, err := t.Render(c)
564+
565+
// clean up after we rendered... remove the functions
566+
t.RemoveFunc("sh", "print", "instanceProvision", "managerCommit")
571567
return err
572568
}
573569

570+
func (c *Context) getTemplate() (*template.Template, error) {
571+
if c.template == nil {
572+
t, err := template.NewTemplate(c.src, template.Options{})
573+
if err != nil {
574+
return nil, err
575+
}
576+
c.template = t
577+
}
578+
return c.template, nil
579+
}
580+
574581
// BuildFlags from parsing the body which is a template
575-
func (c *Context) BuildFlags() error {
576-
t, err := template.NewTemplate(c.src, template.Options{})
582+
func (c *Context) BuildFlags() (err error) {
583+
var t *template.Template
584+
585+
t, err = c.getTemplate()
577586
if err != nil {
578-
return err
587+
return
579588
}
580-
589+
t.SetOptions(template.Options{})
581590
_, err = configureTemplate(t, c.plugins).Render(c)
582-
return err
591+
return
583592
}
584593

585594
// Execute runs the command
586-
func (c *Context) Execute() error {
595+
func (c *Context) Execute() (err error) {
596+
var t *template.Template
587597

588-
if err := c.loadBackends(); err != nil {
589-
return err
598+
t, err = c.getTemplate()
599+
if err != nil {
600+
return
590601
}
591602

592-
t, err := template.NewTemplate(c.src, template.Options{
593-
Stderr: func() io.Writer { return os.Stderr },
603+
// First pass to get the backends
604+
t.SetOptions(template.Options{
605+
DelimLeft: "=%",
606+
DelimRight: "%=",
594607
})
595-
if err != nil {
608+
609+
if err := c.loadBackends(t); err != nil {
596610
return err
597611
}
598612

613+
// Now regular processing
614+
t.SetOptions(template.Options{
615+
Stderr: func() io.Writer { return os.Stderr },
616+
})
617+
599618
c.exec = true
600619
c.template = t
601620
script, err := configureTemplate(t, c.plugins).Render(c)

pkg/template/template.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ func NewTemplateFromBytes(buff []byte, contextURL string, opt Options) (*Templat
143143
}, nil
144144
}
145145

146+
// NewFromTemplate creates a new template from existing, without any fetches (hence network calls)
147+
func NewFromTemplate(from *Template, opt Options) *Template {
148+
return &Template{
149+
options: opt,
150+
url: from.url,
151+
body: from.body,
152+
}
153+
}
154+
146155
// SetOptions sets the runtime flags for the engine
147156
func (t *Template) SetOptions(opt Options) *Template {
148157
t.lock.Lock()
@@ -167,6 +176,16 @@ func (t *Template) AddFunc(name string, f interface{}) *Template {
167176
return t
168177
}
169178

179+
// RemoveFunc remove the functions
180+
func (t *Template) RemoveFunc(name ...string) *Template {
181+
t.lock.Lock()
182+
defer t.lock.Unlock()
183+
for _, n := range name {
184+
delete(t.funcs, n)
185+
}
186+
return t
187+
}
188+
170189
// Ref returns the value keyed by name in the context of this template. See 'ref' template function.
171190
func (t *Template) Ref(name string) interface{} {
172191
if found, has := t.globals[name]; has {
@@ -275,10 +294,6 @@ func (t *Template) build(context Context) error {
275294
t.lock.Lock()
276295
defer t.lock.Unlock()
277296

278-
if t.parsed != nil {
279-
return nil
280-
}
281-
282297
registered := []Function{}
283298
fm := map[string]interface{}{}
284299

0 commit comments

Comments
 (0)