packer-cn/command/build/command.go

54 lines
1.3 KiB
Go
Raw Normal View History

2013-05-07 14:39:32 -04:00
package build
import (
"github.com/mitchellh/packer/packer"
"io/ioutil"
2013-05-08 19:59:36 -04:00
"log"
)
2013-05-07 14:39:32 -04:00
type Command byte
func (Command) Run(env packer.Environment, args []string) int {
if len(args) != 1 {
2013-05-08 18:12:48 -04:00
env.Ui().Error("A single template argument is required.\n")
return 1
}
// Read the file into a byte array so that we can parse the template
2013-05-08 19:59:36 -04:00
log.Printf("Reading template: %s\n", args[0])
tplData, err := ioutil.ReadFile(args[0])
if err != nil {
env.Ui().Error("Failed to read template file: %s\n", err.Error())
return 1
}
// Parse the template into a machine-usable format
2013-05-08 19:59:36 -04:00
log.Println("Parsing template...")
tpl, err := packer.ParseTemplate(tplData)
if err != nil {
env.Ui().Error("Failed to parse template: %s\n", err.Error())
return 1
}
// Go through each builder and compile the builds that we care about
2013-05-08 19:59:36 -04:00
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)
if err != nil {
2013-05-08 20:01:57 -04:00
env.Ui().Error("Failed to create build '%s': \n\n%s\n", buildName, err.Error())
2013-05-08 19:59:36 -04:00
return 1
}
builds = append(builds, build)
}
2013-05-08 19:59:36 -04:00
env.Ui().Say("YAY!\n")
return 0
}
func (Command) Synopsis() string {
return "build image(s) from template"
}