From 582229969ed11b974cf400c3088229096078eff9 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 9 Jun 2014 21:57:37 -0700 Subject: [PATCH] main: instantiate the config and set it up --- config.go | 15 +++++++++++++++ config_test.go | 29 +++++++++++++++++++++++++++++ main.go | 12 +++++++++--- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/config.go b/config.go index 8d7a33cf7..d0085ab91 100644 --- a/config.go +++ b/config.go @@ -67,6 +67,21 @@ func LoadConfig(path string) (*Config, error) { return &result, nil } +// Merge merges two configurations and returns a third entirely +// new configuration with the two merged. +func (c1 *Config) Merge(c2 *Config) *Config { + var result Config + result.Providers = make(map[string]string) + for k, v := range c1.Providers { + result.Providers[k] = v + } + for k, v := range c2.Providers { + result.Providers[k] = v + } + + return &result +} + // ProviderFactories returns the mapping of prefixes to // ResourceProviderFactory that can be used to instantiate a // binary-based plugin. diff --git a/config_test.go b/config_test.go index d27efc8d0..a5203283c 100644 --- a/config_test.go +++ b/config_test.go @@ -26,3 +26,32 @@ func TestLoadConfig(t *testing.T) { t.Fatalf("bad: %#v", c) } } + +func TestConfig_Merge(t *testing.T) { + c1 := &Config{ + Providers: map[string]string{ + "foo": "bar", + "bar": "blah", + }, + } + + c2 := &Config{ + Providers: map[string]string{ + "bar": "baz", + "baz": "what", + }, + } + + expected := &Config{ + Providers: map[string]string{ + "foo": "bar", + "bar": "baz", + "baz": "what", + }, + } + + actual := c1.Merge(c2) + if !reflect.DeepEqual(actual, expected) { + t.Fatalf("bad: %#v", actual) + } +} diff --git a/main.go b/main.go index 6d534d43f..f0781bbc3 100644 --- a/main.go +++ b/main.go @@ -91,6 +91,15 @@ func wrappedMain() int { log.SetOutput(logOutput) } + // Load the configuration + config := BuiltinConfig + + // Make sure we clean up any managed plugins at the end of this + defer plugin.CleanupClients() + + // Initialize the TFConfig settings for the commands... + TFConfig.Providers = config.ProviderFactories() + // Get the command line args. We shortcut "--version" and "-v" to // just show the version. args := os.Args[1:] @@ -110,9 +119,6 @@ func wrappedMain() int { HelpFunc: cli.BasicHelpFunc("terraform"), } - // Make sure we clean up any managed plugins at the end of this - defer plugin.CleanupClients() - exitCode, err := cli.Run() if err != nil { fmt.Fprintf(os.Stderr, "Error executing CLI: %s\n", err.Error())