From a803af7016e370f7f44cb6578a172fcd2ad9a460 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 8 May 2013 14:58:06 -0700 Subject: [PATCH] Move the build command into the actual build command --- command/build/command.go | 35 +++++++++++++++++++++++--- command/build/command_test.go | 46 +++++++++++++++++++++++++++++++++++ packer/build_command.go | 42 -------------------------------- packer/build_command_test.go | 31 ----------------------- 4 files changed, 78 insertions(+), 76 deletions(-) delete mode 100644 packer/build_command.go delete mode 100644 packer/build_command_test.go diff --git a/command/build/command.go b/command/build/command.go index 35bb6331d..35601bed1 100644 --- a/command/build/command.go +++ b/command/build/command.go @@ -1,11 +1,40 @@ package build -import "github.com/mitchellh/packer/packer" +import ( + "github.com/mitchellh/packer/packer" + "io/ioutil" +) type Command byte -func (Command) Run(env packer.Environment, arg []string) int { - env.Ui().Say("YO!") +func (Command) Run(env packer.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 = 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 } diff --git a/command/build/command_test.go b/command/build/command_test.go index e45b793cb..c5230340a 100644 --- a/command/build/command_test.go +++ b/command/build/command_test.go @@ -1,3 +1,49 @@ 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") +} diff --git a/packer/build_command.go b/packer/build_command.go deleted file mode 100644 index 2bfadd2b9..000000000 --- a/packer/build_command.go +++ /dev/null @@ -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" -} diff --git a/packer/build_command_test.go b/packer/build_command_test.go deleted file mode 100644 index 499d21230..000000000 --- a/packer/build_command_test.go +++ /dev/null @@ -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") -}