Commit 6bed06e0 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

Lots of template parsing stuff

parent 298a7cdb
......@@ -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
}
......
......@@ -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
......
......@@ -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")
}
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