Lots of template parsing stuff

This commit is contained in:
Mitchell Hashimoto 2013-04-15 17:04:19 -07:00
parent 298a7cdbe4
commit 6bed06e01c
3 changed files with 89 additions and 2 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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")
}