Commit 11644251 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

Require configuration to create Environment

parent 12049e3d
...@@ -3,10 +3,16 @@ package main ...@@ -3,10 +3,16 @@ package main
import ( import (
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"fmt"
"os" "os"
) )
func main() { func main() {
env := packer.NewEnvironment(nil) env, err := packer.NewEnvironment(nil)
if err != nil {
fmt.Fprintf(os.Stderr, "Packer initialization error: \n\n%s\n", err)
os.Exit(1)
}
os.Exit(env.Cli(os.Args[1:])) os.Exit(env.Cli(os.Args[1:]))
} }
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
package packer package packer
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"sort" "sort"
...@@ -46,33 +47,36 @@ type EnvironmentConfig struct { ...@@ -46,33 +47,36 @@ type EnvironmentConfig struct {
Ui Ui Ui Ui
} }
// This creates a new environment // DefaultEnvironmentConfig returns a default EnvironmentConfig that can
func NewEnvironment(config *EnvironmentConfig) *Environment { // be used to create a new enviroment with NewEnvironment with sane defaults.
env := &Environment{} func DefaultEnvironmentConfig() *EnvironmentConfig {
env.command = make(map[string]Command) config := &EnvironmentConfig{}
config.Ui = &ReaderWriterUi{os.Stdin, os.Stdout}
if config != nil { return config
for k, v := range config.Command { }
env.command[k] = v
}
env.builderFactory = config.BuilderFactory // This creates a new environment
env.ui = config.Ui func NewEnvironment(config *EnvironmentConfig) (env *Environment, err error) {
if config == nil {
err = errors.New("config must be given to initialize environment")
return
} }
if _, ok := env.command["build"]; !ok { env = &Environment{}
env.command["build"] = new(buildCommand) env.builderFactory = config.BuilderFactory
env.command = make(map[string]Command)
env.ui = config.Ui
for k, v := range config.Command {
env.command[k] = v
} }
// TODO: Should "version" be allowed to be overriden?
if _, ok := env.command["version"]; !ok { if _, ok := env.command["version"]; !ok {
env.command["version"] = new(versionCommand) env.command["version"] = new(versionCommand)
} }
if env.ui == nil { return
env.ui = &ReaderWriterUi{os.Stdin, os.Stdout}
}
return env
} }
// Executes a command as if it was typed on the command-line interface. // Executes a command as if it was typed on the command-line interface.
......
...@@ -32,7 +32,32 @@ func testEnvironment() *Environment { ...@@ -32,7 +32,32 @@ func testEnvironment() *Environment {
new(bytes.Buffer), new(bytes.Buffer),
} }
return NewEnvironment(config) env, err := NewEnvironment(config)
if err != nil {
panic(err)
}
return env
}
func TestEnvironment_DefaultConfig_Ui(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
config := DefaultEnvironmentConfig()
assert.NotNil(config.Ui, "default UI should not be nil")
rwUi, ok := config.Ui.(*ReaderWriterUi)
assert.True(ok, "default UI should be ReaderWriterUi")
assert.Equal(rwUi.Writer, os.Stdout, "default UI should go to stdout")
assert.Equal(rwUi.Reader, os.Stdin, "default UI should read from stdin")
}
func TestNewEnvironment_NoConfig(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
env, err := NewEnvironment(nil)
assert.Nil(env, "env should be nil")
assert.NotNil(err, "should be an error")
} }
func TestEnvironment_Cli_CallsRun(t *testing.T) { func TestEnvironment_Cli_CallsRun(t *testing.T) {
...@@ -44,7 +69,7 @@ func TestEnvironment_Cli_CallsRun(t *testing.T) { ...@@ -44,7 +69,7 @@ func TestEnvironment_Cli_CallsRun(t *testing.T) {
config.Command = make(map[string]Command) config.Command = make(map[string]Command)
config.Command["foo"] = command config.Command["foo"] = command
env := NewEnvironment(config) env, _ := NewEnvironment(config)
assert.Equal(env.Cli([]string{"foo", "bar", "baz"}), 0, "runs foo command") assert.Equal(env.Cli([]string{"foo", "bar", "baz"}), 0, "runs foo command")
assert.True(command.runCalled, "run should've been called") assert.True(command.runCalled, "run should've been called")
assert.Equal(command.runEnv, env, "should've ran with env") assert.Equal(command.runEnv, env, "should've ran with env")
...@@ -99,18 +124,6 @@ func TestEnvironment_DefaultCli_Version(t *testing.T) { ...@@ -99,18 +124,6 @@ func TestEnvironment_DefaultCli_Version(t *testing.T) {
assert.Equal(defaultEnv.Cli([]string{"bad", "version"}), 1, "version should NOT work anywhere") assert.Equal(defaultEnv.Cli([]string{"bad", "version"}), 1, "version should NOT work anywhere")
} }
func TestEnvironment_DefaultUi(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
defaultEnv := NewEnvironment(nil)
assert.NotNil(defaultEnv.Ui(), "default UI should not be nil")
rwUi, ok := defaultEnv.Ui().(*ReaderWriterUi)
assert.True(ok, "default UI should be ReaderWriterUi")
assert.Equal(rwUi.Writer, os.Stdout, "default UI should go to stdout")
assert.Equal(rwUi.Reader, os.Stdin, "default UI should read from stdin")
}
func TestEnvironment_PrintHelp(t *testing.T) { func TestEnvironment_PrintHelp(t *testing.T) {
// Just call the function and verify that no panics occur // Just call the function and verify that no panics occur
testEnvironment().PrintHelp() testEnvironment().PrintHelp()
...@@ -124,7 +137,7 @@ func TestEnvironment_SettingUi(t *testing.T) { ...@@ -124,7 +137,7 @@ func TestEnvironment_SettingUi(t *testing.T) {
config := &EnvironmentConfig{} config := &EnvironmentConfig{}
config.Ui = ui config.Ui = ui
env := NewEnvironment(config) env, _ := NewEnvironment(config)
assert.Equal(env.Ui(), ui, "UIs should be equal") assert.Equal(env.Ui(), ui, "UIs should be equal")
} }
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