From 817f0d9f30f5cd85be0f44b33efb7e938a150be4 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 1 Oct 2014 08:37:57 -0700 Subject: [PATCH] command: don't ask for input if terraform.tfvars file given [GH-346] --- command/apply.go | 2 +- command/meta.go | 10 ++++++--- command/meta_test.go | 51 ++++++++++++++++++++++++++++++++++++++++---- command/plan.go | 2 +- command/refresh.go | 2 +- 5 files changed, 57 insertions(+), 10 deletions(-) diff --git a/command/apply.go b/command/apply.go index ef57abb17..9732882f4 100644 --- a/command/apply.go +++ b/command/apply.go @@ -114,7 +114,7 @@ func (c *ApplyCommand) Run(args []string) int { "Destroy can't be called with a plan file.")) return 1 } - if c.Input() { + if c.InputEnabled() { if c.Destroy { v, err := c.UIInput().Input(&terraform.InputOpts{ Id: "destroy", diff --git a/command/meta.go b/command/meta.go index 91d34d755..a6bdded50 100644 --- a/command/meta.go +++ b/command/meta.go @@ -106,9 +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 !test && m.input && len(m.variables) == 0 +// 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 == "" } // UIInput returns a UIInput object to be used for asking for input. @@ -186,6 +188,8 @@ func (m *Meta) moduleStorage(root string) module.Storage { // process will process the meta-parameters out of the arguments. This // will potentially modify the args in-place. It will return the resulting // slice. +// +// vars says whether or not we support variables. func (m *Meta) process(args []string, vars bool) []string { // We do this so that we retain the ability to technically call // process multiple times, even if we have no plans to do so diff --git a/command/meta_test.go b/command/meta_test.go index f98521f48..15ef86833 100644 --- a/command/meta_test.go +++ b/command/meta_test.go @@ -1,6 +1,9 @@ package command import ( + "io/ioutil" + "os" + "path/filepath" "reflect" "testing" ) @@ -48,7 +51,7 @@ func TestMetaColorize(t *testing.T) { } } -func TestMetaInput(t *testing.T) { +func TestMetaInputEnabled(t *testing.T) { test = false defer func() { test = true }() @@ -60,7 +63,7 @@ func TestMetaInput(t *testing.T) { t.Fatalf("err: %s", err) } - if !m.Input() { + if !m.InputEnabled() { t.Fatal("should input") } } @@ -77,11 +80,51 @@ func TestMetaInput_disable(t *testing.T) { t.Fatalf("err: %s", err) } - if m.Input() { + if m.InputEnabled() { t.Fatal("should not input") } } +func TestMetaInput_defaultVars(t *testing.T) { + test = false + defer func() { test = true }() + + // Create a temporary directory for our cwd + d := tempDir(t) + if err := os.MkdirAll(d, 0755); err != nil { + t.Fatalf("err: %s", err) + } + cwd, err := os.Getwd() + if err != nil { + t.Fatalf("err: %s", err) + } + if err := os.Chdir(d); err != nil { + t.Fatalf("err: %s", err) + } + defer os.Chdir(cwd) + + // Create the default vars file + err = ioutil.WriteFile( + filepath.Join(d, DefaultVarsFilename), + []byte(""), + 0644) + if err != nil { + t.Fatalf("err: %s", err) + } + + m := new(Meta) + args := []string{} + args = m.process(args, true) + + fs := m.flagSet("foo") + if err := fs.Parse(args); err != nil { + t.Fatalf("err: %s", err) + } + + if m.InputEnabled() { + t.Fatal("should not input") + } +} func TestMetaInput_vars(t *testing.T) { test = false defer func() { test = true }() @@ -94,7 +137,7 @@ func TestMetaInput_vars(t *testing.T) { t.Fatalf("err: %s", err) } - if m.Input() { + if m.InputEnabled() { t.Fatal("should not input") } } diff --git a/command/plan.go b/command/plan.go index 81d8046d7..4fc3938fe 100644 --- a/command/plan.go +++ b/command/plan.go @@ -75,7 +75,7 @@ func (c *PlanCommand) Run(args []string) int { c.Ui.Error(err.Error()) return 1 } - if c.Input() { + if c.InputEnabled() { if err := ctx.Input(); err != nil { c.Ui.Error(fmt.Sprintf("Error configuring: %s", err)) return 1 diff --git a/command/refresh.go b/command/refresh.go index 83b094db9..14ed963fb 100644 --- a/command/refresh.go +++ b/command/refresh.go @@ -92,7 +92,7 @@ func (c *RefreshCommand) Run(args []string) int { c.Ui.Error(err.Error()) return 1 } - if c.Input() { + if c.InputEnabled() { if err := ctx.Input(); err != nil { c.Ui.Error(fmt.Sprintf("Error configuring: %s", err)) return 1