main: instantiate the config and set it up

This commit is contained in:
Mitchell Hashimoto 2014-06-09 21:57:37 -07:00
parent 61f4684931
commit 582229969e
3 changed files with 53 additions and 3 deletions

View File

@ -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.

View File

@ -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)
}
}

12
main.go
View File

@ -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())