Commit a803af70 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

Move the build command into the actual build command

parent 6633f3df
package build package build
import "github.com/mitchellh/packer/packer" import (
"github.com/mitchellh/packer/packer"
"io/ioutil"
)
type Command byte type Command byte
func (Command) Run(env packer.Environment, arg []string) int { func (Command) Run(env packer.Environment, args []string) int {
env.Ui().Say("YO!") if len(args) != 1 {
// TODO: Error message
return 1
}
// Read the file into a byte array so that we can parse the template
tplData, err := ioutil.ReadFile(args[0])
if err != nil {
// TODO: Error message
return 1
}
// Parse the template into a machine-usable format
_, err = packer.ParseTemplate(tplData)
if err != nil {
// TODO: error message
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 return 0
} }
......
package build package build
import (
"bytes"
"cgl.tideland.biz/asserts"
"github.com/mitchellh/packer/packer"
"testing"
)
func testEnvironment() packer.Environment {
config := packer.DefaultEnvironmentConfig()
config.Ui = &packer.ReaderWriterUi{
new(bytes.Buffer),
new(bytes.Buffer),
}
env, err := packer.NewEnvironment(config)
if err != nil {
panic(err)
}
return env
}
func TestCommand_Run_NoArgs(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
command := new(Command)
result := command.Run(testEnvironment(), make([]string, 0))
assert.Equal(result, 1, "no args should error")
}
func TestCommand_Run_MoreThanOneArg(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
command := new(Command)
args := []string{"one", "two"}
result := command.Run(testEnvironment(), args)
assert.Equal(result, 1, "More than one arg should fail")
}
func TestCommand_Run_MissingFile(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
command := new(Command)
args := []string{"i-better-not-exist"}
result := command.Run(testEnvironment(), args)
assert.Equal(result, 1, "a non-existent file should error")
}
package packer
import (
"io/ioutil"
)
type buildCommand byte
func (buildCommand) Run(env Environment, args []string) int {
if len(args) != 1 {
// TODO: Error message
return 1
}
// Read the file into a byte array so that we can parse the template
tplData, err := ioutil.ReadFile(args[0])
if err != nil {
// TODO: Error message
return 1
}
// Parse the template into a machine-usable format
_, err = ParseTemplate(tplData)
if err != nil {
// TODO: error message
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
}
func (buildCommand) Synopsis() string {
return "build machines images from Packer template"
}
package packer
import (
"cgl.tideland.biz/asserts"
"testing"
)
func TestBuildCommand_Run_NoArgs(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
command := new(buildCommand)
result := command.Run(testEnvironment(), make([]string, 0))
assert.Equal(result, 1, "no args should error")
}
func TestBuildCommand_Run_MoreThanOneArg(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
command := new(buildCommand)
args := []string{"one", "two"}
result := command.Run(testEnvironment(), args)
assert.Equal(result, 1, "More than one arg should fail")
}
func TestBuildCommand_Run_MissingFile(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
command := new(buildCommand)
args := []string{"i-better-not-exist"}
result := command.Run(testEnvironment(), args)
assert.Equal(result, 1, "a non-existent file should error")
}
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