Move the build command into the actual build command

This commit is contained in:
Mitchell Hashimoto 2013-05-08 14:58:06 -07:00
parent 6633f3df80
commit a803af7016
4 changed files with 78 additions and 76 deletions

View File

@ -1,11 +1,40 @@
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
} }

View File

@ -1,3 +1,49 @@
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")
}

View File

@ -1,42 +0,0 @@
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"
}

View File

@ -1,31 +0,0 @@
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")
}