From af548c9b53e0ff12dc0271ff2e60cbb496deb629 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 29 Sep 2014 11:24:16 -0700 Subject: [PATCH] command/plan: ask for input --- command/command.go | 3 +++ command/command_test.go | 2 ++ command/meta.go | 2 +- command/meta_test.go | 9 +++++++++ command/plan.go | 6 ++++++ command/ui_input.go | 9 +++++++++ 6 files changed, 30 insertions(+), 1 deletion(-) diff --git a/command/command.go b/command/command.go index 840c2ae1dd..ff39478ecd 100644 --- a/command/command.go +++ b/command/command.go @@ -7,6 +7,9 @@ import ( "github.com/mitchellh/cli" ) +// Set to true when we're testing +var test bool = false + // DefaultStateFilename is the default filename used for the state file. const DefaultStateFilename = "terraform.tfstate" diff --git a/command/command_test.go b/command/command_test.go index a6c2f173df..d3090240ec 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -14,6 +14,8 @@ import ( var fixtureDir = "./test-fixtures" func init() { + test = true + // Expand the fixture dir on init because we change the working // directory in some tests. var err error diff --git a/command/meta.go b/command/meta.go index beb26c63b3..ca1189066e 100644 --- a/command/meta.go +++ b/command/meta.go @@ -108,7 +108,7 @@ func (m *Meta) Context(copts contextOpts) (*terraform.Context, bool, error) { // Input returns true if we should ask for input for context. func (m *Meta) Input() bool { - return m.input && len(m.variables) == 0 + return !test && m.input && len(m.variables) == 0 } // contextOpts returns the options to use to initialize a Terraform diff --git a/command/meta_test.go b/command/meta_test.go index 3ba02b20d4..f98521f483 100644 --- a/command/meta_test.go +++ b/command/meta_test.go @@ -49,6 +49,9 @@ func TestMetaColorize(t *testing.T) { } func TestMetaInput(t *testing.T) { + test = false + defer func() { test = true }() + m := new(Meta) args := []string{} @@ -63,6 +66,9 @@ func TestMetaInput(t *testing.T) { } func TestMetaInput_disable(t *testing.T) { + test = false + defer func() { test = true }() + m := new(Meta) args := []string{"-input=false"} @@ -77,6 +83,9 @@ func TestMetaInput_disable(t *testing.T) { } func TestMetaInput_vars(t *testing.T) { + test = false + defer func() { test = true }() + m := new(Meta) args := []string{"-var", "foo=bar"} diff --git a/command/plan.go b/command/plan.go index 34d96948f5..81d8046d77 100644 --- a/command/plan.go +++ b/command/plan.go @@ -75,6 +75,12 @@ func (c *PlanCommand) Run(args []string) int { c.Ui.Error(err.Error()) return 1 } + if c.Input() { + if err := ctx.Input(); err != nil { + c.Ui.Error(fmt.Sprintf("Error configuring: %s", err)) + return 1 + } + } if !validateContext(ctx, c.Ui) { return 1 } diff --git a/command/ui_input.go b/command/ui_input.go index 1a67cbb3bb..d37bde4427 100644 --- a/command/ui_input.go +++ b/command/ui_input.go @@ -12,6 +12,9 @@ import ( "github.com/hashicorp/terraform/terraform" ) +var defaultInputReader io.Reader +var defaultInputWriter io.Writer + // UIInput is an implementation of terraform.UIInput that asks the CLI // for input stdin. type UIInput struct { @@ -27,6 +30,12 @@ type UIInput struct { func (i *UIInput) Input(opts *terraform.InputOpts) (string, error) { r := i.Reader w := i.Writer + if r == nil { + r = defaultInputReader + } + if w == nil { + w = defaultInputWriter + } if r == nil { r = os.Stdin }