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

View File

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

View File

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

View File

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

View File

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