Commit 11644251 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

Require configuration to create Environment

parent 12049e3d
......@@ -3,10 +3,16 @@ package main
import (
"github.com/mitchellh/packer/packer"
"fmt"
"os"
)
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:]))
}
......@@ -2,6 +2,7 @@
package packer
import (
"errors"
"fmt"
"os"
"sort"
......@@ -46,33 +47,36 @@ type EnvironmentConfig struct {
Ui Ui
}
// This creates a new environment
func NewEnvironment(config *EnvironmentConfig) *Environment {
env := &Environment{}
env.command = make(map[string]Command)
// DefaultEnvironmentConfig returns a default EnvironmentConfig that can
// be used to create a new enviroment with NewEnvironment with sane defaults.
func DefaultEnvironmentConfig() *EnvironmentConfig {
config := &EnvironmentConfig{}
config.Ui = &ReaderWriterUi{os.Stdin, os.Stdout}
return config
}
if config != nil {
for k, v := range config.Command {
env.command[k] = v
// This creates a new environment
func NewEnvironment(config *EnvironmentConfig) (env *Environment, err error) {
if config == nil {
err = errors.New("config must be given to initialize environment")
return
}
env = &Environment{}
env.builderFactory = config.BuilderFactory
env.command = make(map[string]Command)
env.ui = config.Ui
}
if _, ok := env.command["build"]; !ok {
env.command["build"] = new(buildCommand)
for k, v := range config.Command {
env.command[k] = v
}
// TODO: Should "version" be allowed to be overriden?
if _, ok := env.command["version"]; !ok {
env.command["version"] = new(versionCommand)
}
if env.ui == nil {
env.ui = &ReaderWriterUi{os.Stdin, os.Stdout}
}
return env
return
}
// Executes a command as if it was typed on the command-line interface.
......
......@@ -32,7 +32,32 @@ func testEnvironment() *Environment {
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) {
......@@ -44,7 +69,7 @@ func TestEnvironment_Cli_CallsRun(t *testing.T) {
config.Command = make(map[string]Command)
config.Command["foo"] = command
env := NewEnvironment(config)
env, _ := NewEnvironment(config)
assert.Equal(env.Cli([]string{"foo", "bar", "baz"}), 0, "runs foo command")
assert.True(command.runCalled, "run should've been called")
assert.Equal(command.runEnv, env, "should've ran with env")
......@@ -99,18 +124,6 @@ func TestEnvironment_DefaultCli_Version(t *testing.T) {
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) {
// Just call the function and verify that no panics occur
testEnvironment().PrintHelp()
......@@ -124,7 +137,7 @@ func TestEnvironment_SettingUi(t *testing.T) {
config := &EnvironmentConfig{}
config.Ui = ui
env := NewEnvironment(config)
env, _ := NewEnvironment(config)
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