diff --git a/packer/build_command.go b/packer/build_command.go index 8cdb00a8f..264552af5 100644 --- a/packer/build_command.go +++ b/packer/build_command.go @@ -26,6 +26,14 @@ func (buildCommand) Run(env *Environment, args []string) int { return 1 } + // Go through each builder and compile the builds that we care about + //builds := make([]Build, 0, len(tpl.Builders)) + //for name, rawConfig := range tpl.Builders { + //builder := env.Builder(name, rawConfig) + //build := env.Build(name, builder) + //builds = append(builds, build) + //} + return 0 } diff --git a/packer/template.go b/packer/template.go index 6f0fd9776..6ab43535a 100644 --- a/packer/template.go +++ b/packer/template.go @@ -34,8 +34,38 @@ func ParseTemplate(data []byte) (t *Template, err error) { return } - t = &Template{ - Name: rawTpl.Name, + t = &Template{} + t.Name = rawTpl.Name + t.Builders = make(map[string]rawBuilderConfig) + + for _, v := range rawTpl.Builders { + rawType, ok := v["type"] + if !ok { + // TODO: Missing type error + return + } + + // Attempt to get the name of the builder. If the "name" key + // missing, use the "type" field, which is guaranteed to exist + // at this point. + rawName, ok := v["name"] + if !ok { + rawName = v["type"] + } + + // TODO: Error checking if we can't convert + name := rawName.(string) + typeName := rawType.(string) + + // Check if we already have a builder with this name and record + // an error. + _, ok = t.Builders[name] + if ok { + // TODO: We already have a builder with this name + return + } + + t.Builders[name] = rawBuilderConfig{typeName, v} } return diff --git a/packer/template_test.go b/packer/template_test.go index 64420c866..b9adbe73b 100644 --- a/packer/template_test.go +++ b/packer/template_test.go @@ -38,3 +38,52 @@ func TestParseTemplate_Invalid(t *testing.T) { assert.NotNil(err, "should have an error") assert.Nil(result, "should have no result") } + +func TestParseTemplate_BuilderWithoutName(t *testing.T) { + assert := asserts.NewTestingAsserts(t, true) + + data := ` + { + "name": "my-image", + "builders": [ + { + "type": "amazon-ebs" + } + ] + } + ` + + result, err := ParseTemplate([]byte(data)) + assert.Nil(err, "should not error") + assert.NotNil(result, "template should not be nil") + assert.Length(result.Builders, 1, "should have one builder") + + builder, ok := result.Builders["amazon-ebs"] + assert.True(ok, "should have amazon-ebs builder") + assert.Equal(builder.builderName, "amazon-ebs", "builder should be amazon-ebs") +} + +func TestParseTemplate_BuilderWithName(t *testing.T) { + assert := asserts.NewTestingAsserts(t, true) + + data := ` + { + "name": "my-image", + "builders": [ + { + "name": "bob", + "type": "amazon-ebs" + } + ] + } + ` + + result, err := ParseTemplate([]byte(data)) + assert.Nil(err, "should not error") + assert.NotNil(result, "template should not be nil") + assert.Length(result.Builders, 1, "should have one builder") + + builder, ok := result.Builders["bob"] + assert.True(ok, "should have bob builder") + assert.Equal(builder.builderName, "amazon-ebs", "builder should be amazon-ebs") +}