command/plan: ask for input
This commit is contained in:
parent
d9596fa4d0
commit
af548c9b53
|
@ -7,6 +7,9 @@ import (
|
||||||
"github.com/mitchellh/cli"
|
"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.
|
// DefaultStateFilename is the default filename used for the state file.
|
||||||
const DefaultStateFilename = "terraform.tfstate"
|
const DefaultStateFilename = "terraform.tfstate"
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,8 @@ import (
|
||||||
var fixtureDir = "./test-fixtures"
|
var fixtureDir = "./test-fixtures"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
test = true
|
||||||
|
|
||||||
// Expand the fixture dir on init because we change the working
|
// Expand the fixture dir on init because we change the working
|
||||||
// directory in some tests.
|
// directory in some tests.
|
||||||
var err error
|
var err error
|
||||||
|
|
|
@ -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.
|
// Input returns true if we should ask for input for context.
|
||||||
func (m *Meta) Input() bool {
|
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
|
// contextOpts returns the options to use to initialize a Terraform
|
||||||
|
|
|
@ -49,6 +49,9 @@ func TestMetaColorize(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMetaInput(t *testing.T) {
|
func TestMetaInput(t *testing.T) {
|
||||||
|
test = false
|
||||||
|
defer func() { test = true }()
|
||||||
|
|
||||||
m := new(Meta)
|
m := new(Meta)
|
||||||
args := []string{}
|
args := []string{}
|
||||||
|
|
||||||
|
@ -63,6 +66,9 @@ func TestMetaInput(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMetaInput_disable(t *testing.T) {
|
func TestMetaInput_disable(t *testing.T) {
|
||||||
|
test = false
|
||||||
|
defer func() { test = true }()
|
||||||
|
|
||||||
m := new(Meta)
|
m := new(Meta)
|
||||||
args := []string{"-input=false"}
|
args := []string{"-input=false"}
|
||||||
|
|
||||||
|
@ -77,6 +83,9 @@ func TestMetaInput_disable(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMetaInput_vars(t *testing.T) {
|
func TestMetaInput_vars(t *testing.T) {
|
||||||
|
test = false
|
||||||
|
defer func() { test = true }()
|
||||||
|
|
||||||
m := new(Meta)
|
m := new(Meta)
|
||||||
args := []string{"-var", "foo=bar"}
|
args := []string{"-var", "foo=bar"}
|
||||||
|
|
||||||
|
|
|
@ -75,6 +75,12 @@ func (c *PlanCommand) Run(args []string) int {
|
||||||
c.Ui.Error(err.Error())
|
c.Ui.Error(err.Error())
|
||||||
return 1
|
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) {
|
if !validateContext(ctx, c.Ui) {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,9 @@ import (
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var defaultInputReader io.Reader
|
||||||
|
var defaultInputWriter io.Writer
|
||||||
|
|
||||||
// UIInput is an implementation of terraform.UIInput that asks the CLI
|
// UIInput is an implementation of terraform.UIInput that asks the CLI
|
||||||
// for input stdin.
|
// for input stdin.
|
||||||
type UIInput struct {
|
type UIInput struct {
|
||||||
|
@ -27,6 +30,12 @@ type UIInput struct {
|
||||||
func (i *UIInput) Input(opts *terraform.InputOpts) (string, error) {
|
func (i *UIInput) Input(opts *terraform.InputOpts) (string, error) {
|
||||||
r := i.Reader
|
r := i.Reader
|
||||||
w := i.Writer
|
w := i.Writer
|
||||||
|
if r == nil {
|
||||||
|
r = defaultInputReader
|
||||||
|
}
|
||||||
|
if w == nil {
|
||||||
|
w = defaultInputWriter
|
||||||
|
}
|
||||||
if r == nil {
|
if r == nil {
|
||||||
r = os.Stdin
|
r = os.Stdin
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue