command: ask for the proper level of input

This commit is contained in:
Mitchell Hashimoto 2014-10-08 10:29:54 -07:00
parent 8b5c120ecf
commit 16b023bd2b
5 changed files with 39 additions and 33 deletions

View File

@ -133,12 +133,10 @@ func (c *ApplyCommand) Run(args []string) int {
return 1 return 1
} }
} }
if c.InputEnabled() { if !planned {
if !planned { if err := ctx.Input(c.InputMode()); err != nil {
if err := ctx.Input(); err != nil { c.Ui.Error(fmt.Sprintf("Error configuring: %s", err))
c.Ui.Error(fmt.Sprintf("Error configuring: %s", err)) return 1
return 1
}
} }
} }
if !validateContext(ctx, c.Ui) { if !validateContext(ctx, c.Ui) {

View File

@ -106,11 +106,20 @@ func (m *Meta) Context(copts contextOpts) (*terraform.Context, bool, error) {
return ctx, false, nil return ctx, false, nil
} }
// InputEnabled returns true if we should ask for input for context. // InputMode returns the type of input we should ask for in the form of
func (m *Meta) InputEnabled() bool { // terraform.InputMode which is passed directly to Context.Input.
return !test && m.input && func (m *Meta) InputMode() terraform.InputMode {
len(m.variables) == 0 && if test || !m.input {
m.autoKey == "" 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. // UIInput returns a UIInput object to be used for asking for input.

View File

@ -6,6 +6,8 @@ import (
"path/filepath" "path/filepath"
"reflect" "reflect"
"testing" "testing"
"github.com/hashicorp/terraform/terraform"
) )
func TestMetaColorize(t *testing.T) { 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 test = false
defer func() { test = true }() defer func() { test = true }()
@ -63,12 +65,12 @@ func TestMetaInputEnabled(t *testing.T) {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if !m.InputEnabled() { if m.InputMode() != terraform.InputModeStd {
t.Fatal("should input") t.Fatalf("bad: %#v", m.InputMode())
} }
} }
func TestMetaInput_disable(t *testing.T) { func TestMetaInputMode_disable(t *testing.T) {
test = false test = false
defer func() { test = true }() defer func() { test = true }()
@ -80,12 +82,12 @@ func TestMetaInput_disable(t *testing.T) {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if m.InputEnabled() { if m.InputMode() > 0 {
t.Fatal("should not input") t.Fatalf("bad: %#v", m.InputMode())
} }
} }
func TestMetaInput_defaultVars(t *testing.T) { func TestMetaInputMode_defaultVars(t *testing.T) {
test = false test = false
defer func() { test = true }() defer func() { test = true }()
@ -121,11 +123,12 @@ func TestMetaInput_defaultVars(t *testing.T) {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if m.InputEnabled() { if m.InputMode()&terraform.InputModeVar != 0 {
t.Fatal("should not input") t.Fatalf("bad: %#v", m.InputMode())
} }
} }
func TestMetaInput_vars(t *testing.T) {
func TestMetaInputMode_vars(t *testing.T) {
test = false test = false
defer func() { test = true }() defer func() { test = true }()
@ -137,7 +140,7 @@ func TestMetaInput_vars(t *testing.T) {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if m.InputEnabled() { if m.InputMode()&terraform.InputModeVar != 0 {
t.Fatal("should not input") t.Fatalf("bad: %#v", m.InputMode())
} }
} }

View File

@ -75,11 +75,9 @@ func (c *PlanCommand) Run(args []string) int {
c.Ui.Error(err.Error()) c.Ui.Error(err.Error())
return 1 return 1
} }
if c.InputEnabled() { if err := ctx.Input(c.InputMode()); err != nil {
if err := ctx.Input(); err != nil { c.Ui.Error(fmt.Sprintf("Error configuring: %s", err))
c.Ui.Error(fmt.Sprintf("Error configuring: %s", err)) return 1
return 1
}
} }
if !validateContext(ctx, c.Ui) { if !validateContext(ctx, c.Ui) {
return 1 return 1

View File

@ -92,11 +92,9 @@ func (c *RefreshCommand) Run(args []string) int {
c.Ui.Error(err.Error()) c.Ui.Error(err.Error())
return 1 return 1
} }
if c.InputEnabled() { if err := ctx.Input(c.InputMode()); err != nil {
if err := ctx.Input(); err != nil { c.Ui.Error(fmt.Sprintf("Error configuring: %s", err))
c.Ui.Error(fmt.Sprintf("Error configuring: %s", err)) return 1
return 1
}
} }
if !validateContext(ctx, c.Ui) { if !validateContext(ctx, c.Ui) {
return 1 return 1