command/plan: ask for input

This commit is contained in:
Mitchell Hashimoto 2014-09-29 11:24:16 -07:00
parent d9596fa4d0
commit af548c9b53
6 changed files with 30 additions and 1 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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