diff --git a/command/config.go b/command/config.go new file mode 100644 index 0000000000..c01016df7e --- /dev/null +++ b/command/config.go @@ -0,0 +1,25 @@ +package command + +import ( + "github.com/hashicorp/terraform/terraform" + "github.com/mitchellh/cli" +) + +// Config is a structure used to configure many commands with Terraform +// configurations. +type Config struct { + Hooks []terraform.Hook + Providers map[string]terraform.ResourceProviderFactory + Ui cli.Ui +} + +func (c *Config) ContextOpts() *terraform.ContextOpts { + hooks := make([]terraform.Hook, len(c.Hooks)+1) + copy(hooks, c.Hooks) + hooks[len(c.Hooks)] = &UiHook{Ui: c.Ui} + + return &terraform.ContextOpts{ + Hooks: hooks, + Providers: c.Providers, + } +} diff --git a/commands.go b/commands.go index 2ff36f53df..48d57cbfed 100644 --- a/commands.go +++ b/commands.go @@ -29,30 +29,30 @@ func init() { Commands = map[string]cli.CommandFactory{ "apply": func() (cli.Command, error) { return &command.ApplyCommand{ - ShutdownCh: makeShutdownCh(), - TFConfig: &TFConfig, - Ui: Ui, + ShutdownCh: makeShutdownCh(), + ContextOpts: &ContextOpts, + Ui: Ui, }, nil }, "graph": func() (cli.Command, error) { return &command.GraphCommand{ - TFConfig: &TFConfig, - Ui: Ui, + ContextOpts: &ContextOpts, + Ui: Ui, }, nil }, "plan": func() (cli.Command, error) { return &command.PlanCommand{ - TFConfig: &TFConfig, - Ui: Ui, + ContextOpts: &ContextOpts, + Ui: Ui, }, nil }, "refresh": func() (cli.Command, error) { return &command.RefreshCommand{ - TFConfig: &TFConfig, - Ui: Ui, + ContextOpts: &ContextOpts, + Ui: Ui, }, nil }, diff --git a/config.go b/config.go index 10749aab4a..c3cfb0f9f8 100644 --- a/config.go +++ b/config.go @@ -23,6 +23,9 @@ type Config struct { // can be overridden by user configurations. var BuiltinConfig Config +// ContextOpts are the global ContextOpts we use to initialize the CLI. +var ContextOpts terraform.ContextOpts + // Put the parse flags we use for libucl in a constant so we can get // equally behaving parsing everywhere. const libuclParseFlags = libucl.ParserKeyLowercase diff --git a/main.go b/main.go index 31c0d5aed0..c47ecca550 100644 --- a/main.go +++ b/main.go @@ -84,7 +84,7 @@ func wrappedMain() int { defer plugin.CleanupClients() // Initialize the TFConfig settings for the commands... - TFConfig.Providers = config.ProviderFactories() + ContextOpts.Providers = config.ProviderFactories() // Get the command line args. We shortcut "--version" and "-v" to // just show the version.