From 779f489894dc11ca0e4755a8f782bcb3a18a490c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 11 May 2013 09:56:42 -0700 Subject: [PATCH] packer: Template takes a component finder --- command/build/command.go | 8 +++++++- packer/template.go | 4 ++-- packer/template_test.go | 6 ++++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/command/build/command.go b/command/build/command.go index 85dcfe399..8d4b441b9 100644 --- a/command/build/command.go +++ b/command/build/command.go @@ -31,12 +31,18 @@ func (Command) Run(env packer.Environment, args []string) int { return 1 } + // The component finder for our builds + components := &packer.ComponentFinder{ + Builder: env.Builder, + Hook: env.Hook, + } + // Go through each builder and compile the builds that we care about buildNames := tpl.BuildNames() builds := make([]packer.Build, 0, len(buildNames)) for _, buildName := range buildNames { log.Printf("Creating build: %s\n", buildName) - build, err := tpl.Build(buildName, env.Builder) + build, err := tpl.Build(buildName, components) if err != nil { env.Ui().Error("Failed to create build '%s': \n\n%s\n", buildName, err.Error()) return 1 diff --git a/packer/template.go b/packer/template.go index 1db86166a..c06a6fac8 100644 --- a/packer/template.go +++ b/packer/template.go @@ -96,14 +96,14 @@ func (t *Template) BuildNames() []string { // // If the build does not exist as part of this template, an error is // returned. -func (t *Template) Build(name string, bf BuilderFunc) (b Build, err error) { +func (t *Template) Build(name string, components *ComponentFinder) (b Build, err error) { builderConfig, ok := t.Builders[name] if !ok { err = fmt.Errorf("No such build found in template: %s", name) return } - builder, err := bf(builderConfig.builderName) + builder, err := components.Builder(builderConfig.builderName) if err != nil { return } diff --git a/packer/template_test.go b/packer/template_test.go index 58e950804..9e0f415be 100644 --- a/packer/template_test.go +++ b/packer/template_test.go @@ -181,7 +181,8 @@ func TestTemplate_BuildUnknownBuilder(t *testing.T) { assert.Nil(err, "should not error") builderFactory := func(string) (Builder, error) { return nil, nil } - build, err := template.Build("test1", builderFactory) + components := &ComponentFinder{Builder: builderFactory} + build, err := template.Build("test1", components) assert.Nil(build, "build should be nil") assert.NotNil(err, "should have error") } @@ -215,10 +216,11 @@ func TestTemplate_Build(t *testing.T) { } builderFactory := func(n string) (Builder, error) { return builderMap[n], nil } + components := &ComponentFinder{Builder: builderFactory} // Get the build, verifying we can get it without issue, but also // that the proper builder was looked up and used for the build. - build, err := template.Build("test1", builderFactory) + build, err := template.Build("test1", components) assert.Nil(err, "should not error") build.Prepare()