packer-cn/packer/template.go

73 lines
1.8 KiB
Go
Raw Normal View History

2013-03-25 19:29:26 -04:00
package packer
import "encoding/json"
// The rawTemplate struct represents the structure of a template read
// directly from a file. The builders and other components map just to
// "interface{}" pointers since we actually don't know what their contents
// are until we read the "type" field.
type rawTemplate struct {
Name string
Builders []map[string]interface{}
Provisioners []map[string]interface{}
Outputs []map[string]interface{}
}
type Template struct {
Name string
Builders map[string]rawBuilderConfig
}
// The rawBuilderConfig struct represents a raw, unprocessed builder
// configuration. It contains the name of the builder as well as the
// raw configuration. If requested, this is used to compile into a full
// builder configuration at some point.
type rawBuilderConfig struct {
builderName string
rawConfig interface{}
}
func ParseTemplate(data []byte) (t *Template, err error) {
2013-03-25 19:29:26 -04:00
var rawTpl rawTemplate
err = json.Unmarshal(data, &rawTpl)
2013-03-25 19:29:26 -04:00
if err != nil {
return
2013-03-25 19:29:26 -04:00
}
2013-04-15 20:04:19 -04:00
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
2013-03-25 19:29:26 -04:00
}