From 16b023bd2b807cfe1a1fe9dc2998fa23b5d15c15 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 8 Oct 2014 10:29:54 -0700 Subject: [PATCH] command: ask for the proper level of input --- command/apply.go | 10 ++++------ command/meta.go | 19 ++++++++++++++----- command/meta_test.go | 27 +++++++++++++++------------ command/plan.go | 8 +++----- command/refresh.go | 8 +++----- 5 files changed, 39 insertions(+), 33 deletions(-) diff --git a/command/apply.go b/command/apply.go index 1870aad88..c7c189a26 100644 --- a/command/apply.go +++ b/command/apply.go @@ -133,12 +133,10 @@ func (c *ApplyCommand) Run(args []string) int { return 1 } } - if c.InputEnabled() { - if !planned { - if err := ctx.Input(); err != nil { - c.Ui.Error(fmt.Sprintf("Error configuring: %s", err)) - return 1 - } + if !planned { + if err := ctx.Input(c.InputMode()); err != nil { + c.Ui.Error(fmt.Sprintf("Error configuring: %s", err)) + return 1 } } if !validateContext(ctx, c.Ui) { diff --git a/command/meta.go b/command/meta.go index a6bdded50..cb21238b3 100644 --- a/command/meta.go +++ b/command/meta.go @@ -106,11 +106,20 @@ func (m *Meta) Context(copts contextOpts) (*terraform.Context, bool, error) { return ctx, false, nil } -// InputEnabled returns true if we should ask for input for context. -func (m *Meta) InputEnabled() bool { - return !test && m.input && - len(m.variables) == 0 && - m.autoKey == "" +// InputMode returns the type of input we should ask for in the form of +// terraform.InputMode which is passed directly to Context.Input. +func (m *Meta) InputMode() terraform.InputMode { + if test || !m.input { + return 0 + } + + var mode terraform.InputMode + mode |= terraform.InputModeProvider + if len(m.variables) == 0 && m.autoKey == "" { + mode |= terraform.InputModeVar + } + + return mode } // UIInput returns a UIInput object to be used for asking for input. diff --git a/command/meta_test.go b/command/meta_test.go index 15ef86833..ec4840f34 100644 --- a/command/meta_test.go +++ b/command/meta_test.go @@ -6,6 +6,8 @@ import ( "path/filepath" "reflect" "testing" + + "github.com/hashicorp/terraform/terraform" ) func TestMetaColorize(t *testing.T) { @@ -51,7 +53,7 @@ func TestMetaColorize(t *testing.T) { } } -func TestMetaInputEnabled(t *testing.T) { +func TestMetaInputMode(t *testing.T) { test = false defer func() { test = true }() @@ -63,12 +65,12 @@ func TestMetaInputEnabled(t *testing.T) { t.Fatalf("err: %s", err) } - if !m.InputEnabled() { - t.Fatal("should input") + if m.InputMode() != terraform.InputModeStd { + t.Fatalf("bad: %#v", m.InputMode()) } } -func TestMetaInput_disable(t *testing.T) { +func TestMetaInputMode_disable(t *testing.T) { test = false defer func() { test = true }() @@ -80,12 +82,12 @@ func TestMetaInput_disable(t *testing.T) { t.Fatalf("err: %s", err) } - if m.InputEnabled() { - t.Fatal("should not input") + if m.InputMode() > 0 { + t.Fatalf("bad: %#v", m.InputMode()) } } -func TestMetaInput_defaultVars(t *testing.T) { +func TestMetaInputMode_defaultVars(t *testing.T) { test = false defer func() { test = true }() @@ -121,11 +123,12 @@ func TestMetaInput_defaultVars(t *testing.T) { t.Fatalf("err: %s", err) } - if m.InputEnabled() { - t.Fatal("should not input") + if m.InputMode()&terraform.InputModeVar != 0 { + t.Fatalf("bad: %#v", m.InputMode()) } } -func TestMetaInput_vars(t *testing.T) { + +func TestMetaInputMode_vars(t *testing.T) { test = false defer func() { test = true }() @@ -137,7 +140,7 @@ func TestMetaInput_vars(t *testing.T) { t.Fatalf("err: %s", err) } - if m.InputEnabled() { - t.Fatal("should not input") + if m.InputMode()&terraform.InputModeVar != 0 { + t.Fatalf("bad: %#v", m.InputMode()) } } diff --git a/command/plan.go b/command/plan.go index 4fc3938fe..32ad745af 100644 --- a/command/plan.go +++ b/command/plan.go @@ -75,11 +75,9 @@ func (c *PlanCommand) Run(args []string) int { c.Ui.Error(err.Error()) return 1 } - if c.InputEnabled() { - if err := ctx.Input(); err != nil { - c.Ui.Error(fmt.Sprintf("Error configuring: %s", err)) - return 1 - } + if err := ctx.Input(c.InputMode()); err != nil { + c.Ui.Error(fmt.Sprintf("Error configuring: %s", err)) + return 1 } if !validateContext(ctx, c.Ui) { return 1 diff --git a/command/refresh.go b/command/refresh.go index 14ed963fb..6ec2d0f8e 100644 --- a/command/refresh.go +++ b/command/refresh.go @@ -92,11 +92,9 @@ func (c *RefreshCommand) Run(args []string) int { c.Ui.Error(err.Error()) return 1 } - if c.InputEnabled() { - if err := ctx.Input(); err != nil { - c.Ui.Error(fmt.Sprintf("Error configuring: %s", err)) - return 1 - } + if err := ctx.Input(c.InputMode()); err != nil { + c.Ui.Error(fmt.Sprintf("Error configuring: %s", err)) + return 1 } if !validateContext(ctx, c.Ui) { return 1