From 5f8330ecc256264cf9087aa8b2c74b1734f29cfc Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 8 May 2013 18:13:15 -0700 Subject: [PATCH] Parse/load configs --- config.go | 38 +++++++++++++++++++++----------------- config_test.go | 32 ++++++++++++++++++++++++++++++++ packer.go | 9 +++++++-- 3 files changed, 60 insertions(+), 19 deletions(-) create mode 100644 config_test.go diff --git a/config.go b/config.go index b2378fd3a..4899a3037 100644 --- a/config.go +++ b/config.go @@ -1,43 +1,47 @@ package main import ( - "fmt" + "github.com/BurntSushi/toml" "github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer/plugin" "log" "os/exec" ) +// This is the default, built-in configuration that ships with +// Packer. +const defaultConfig = ` +[commands] +build = "packer-command-build" +` + type config struct { - builds map[string]string - commands map[string]string + Builders map[string]string + Commands map[string]string } -func defaultConfig() (result *config) { - commands := []string{"build"} - +// Parses a configuration file and returns a proper configuration +// struct. +func parseConfig(data string) (result *config, err error) { result = new(config) - result.builds = make(map[string]string) - result.commands = make(map[string]string) - - for _, name := range commands { - result.commands[name] = fmt.Sprintf("packer-command-%s", name) - } - + _, err = toml.Decode(data, &result) return } -func (c *config) Commands() (result []string) { - result = make([]string, 0, len(c.commands)) - for name, _ := range c.commands { +// Returns an array of defined command names. +func (c *config) CommandNames() (result []string) { + result = make([]string, 0, len(c.Commands)) + for name, _ := range c.Commands { result = append(result, name) } return } +// This is a proper packer.CommandFunc that can be used to load packer.Command +// implementations from the defined plugins. func (c *config) LoadCommand(name string) (packer.Command, error) { log.Printf("Loading command: %s\n", name) - commandBin, ok := c.commands[name] + commandBin, ok := c.Commands[name] if !ok { log.Printf("Command not found: %s\n", name) return nil, nil diff --git a/config_test.go b/config_test.go new file mode 100644 index 000000000..08a319724 --- /dev/null +++ b/config_test.go @@ -0,0 +1,32 @@ +package main + +import ( + "cgl.tideland.biz/asserts" + "testing" +) + +func TestConfig_ParseConfig_Bad(t *testing.T) { + assert := asserts.NewTestingAsserts(t, true) + + data := ` + [commands] + foo = bar + ` + + _, err := parseConfig(data) + assert.NotNil(err, "should have an error") +} + +func TestConfig_ParseConfig_Good(t *testing.T) { + assert := asserts.NewTestingAsserts(t, true) + + data := ` + [commands] + foo = "bar" + ` + + c, err := parseConfig(data) + assert.Nil(err, "should not have an error") + assert.Equal(c.CommandNames(), []string{"foo"}, "should have correct command names") + assert.Equal(c.Commands["foo"], "bar", "should have the command") +} diff --git a/packer.go b/packer.go index b48f108d8..9a5220321 100644 --- a/packer.go +++ b/packer.go @@ -21,9 +21,14 @@ func main() { defer plugin.CleanupClients() - config := defaultConfig() + config, err := parseConfig(defaultConfig) + if err != nil { + fmt.Fprintf(os.Stderr, "Error loading global Packer configuration: \n\n%s\n", err) + os.Exit(1) + } + envConfig := packer.DefaultEnvironmentConfig() - envConfig.Commands = config.Commands() + envConfig.Commands = config.CommandNames() envConfig.CommandFunc = config.LoadCommand env, err := packer.NewEnvironment(envConfig)