diff --git a/command/meta.go b/command/meta.go index 4f66f47d5..beb26c63b 100644 --- a/command/meta.go +++ b/command/meta.go @@ -30,6 +30,7 @@ type Meta struct { // Variables for the context (private) autoKey string autoVariables map[string]string + input bool variables map[string]string color bool @@ -105,6 +106,11 @@ func (m *Meta) Context(copts contextOpts) (*terraform.Context, bool, error) { return ctx, false, nil } +// Input returns true if we should ask for input for context. +func (m *Meta) Input() bool { + return m.input && len(m.variables) == 0 +} + // contextOpts returns the options to use to initialize a Terraform // context with the settings from this Meta. func (m *Meta) contextOpts() *terraform.ContextOpts { @@ -127,6 +133,7 @@ func (m *Meta) contextOpts() *terraform.ContextOpts { vs[k] = v } opts.Variables = vs + opts.UIInput = new(UIInput) return &opts } @@ -134,6 +141,7 @@ func (m *Meta) contextOpts() *terraform.ContextOpts { // flags adds the meta flags to the given FlagSet. func (m *Meta) flagSet(n string) *flag.FlagSet { f := flag.NewFlagSet(n, flag.ContinueOnError) + f.BoolVar(&m.input, "input", true, "input") f.Var((*FlagVar)(&m.variables), "var", "variables") f.Var((*FlagVarFile)(&m.variables), "var-file", "variable file") diff --git a/command/meta_test.go b/command/meta_test.go index 8abc92f22..3ba02b20d 100644 --- a/command/meta_test.go +++ b/command/meta_test.go @@ -47,3 +47,45 @@ func TestMetaColorize(t *testing.T) { t.Fatal("should be disabled") } } + +func TestMetaInput(t *testing.T) { + m := new(Meta) + args := []string{} + + fs := m.flagSet("foo") + if err := fs.Parse(args); err != nil { + t.Fatalf("err: %s", err) + } + + if !m.Input() { + t.Fatal("should input") + } +} + +func TestMetaInput_disable(t *testing.T) { + m := new(Meta) + args := []string{"-input=false"} + + fs := m.flagSet("foo") + if err := fs.Parse(args); err != nil { + t.Fatalf("err: %s", err) + } + + if m.Input() { + t.Fatal("should not input") + } +} + +func TestMetaInput_vars(t *testing.T) { + m := new(Meta) + args := []string{"-var", "foo=bar"} + + fs := m.flagSet("foo") + if err := fs.Parse(args); err != nil { + t.Fatalf("err: %s", err) + } + + if m.Input() { + t.Fatal("should not input") + } +} diff --git a/command/plan.go b/command/plan.go index b40fdfdd1..34d96948f 100644 --- a/command/plan.go +++ b/command/plan.go @@ -167,6 +167,8 @@ Options: -destroy If set, a plan will be generated to destroy all resources managed by the given configuration and state. + -input=true Ask for input for variables if not directly set. + -module-depth=n Specifies the depth of modules to show in the output. This does not affect the plan itself, only the output shown. By default, this is zero. -1 will expand all.