command: don't ask for input if terraform.tfvars file given [GH-346]

This commit is contained in:
Mitchell Hashimoto 2014-10-01 08:37:57 -07:00
parent 8008813671
commit 817f0d9f30
5 changed files with 57 additions and 10 deletions

View File

@ -114,7 +114,7 @@ func (c *ApplyCommand) Run(args []string) int {
"Destroy can't be called with a plan file.")) "Destroy can't be called with a plan file."))
return 1 return 1
} }
if c.Input() { if c.InputEnabled() {
if c.Destroy { if c.Destroy {
v, err := c.UIInput().Input(&terraform.InputOpts{ v, err := c.UIInput().Input(&terraform.InputOpts{
Id: "destroy", Id: "destroy",

View File

@ -106,9 +106,11 @@ func (m *Meta) Context(copts contextOpts) (*terraform.Context, bool, error) {
return ctx, false, nil return ctx, false, nil
} }
// Input returns true if we should ask for input for context. // InputEnabled returns true if we should ask for input for context.
func (m *Meta) Input() bool { func (m *Meta) InputEnabled() bool {
return !test && m.input && len(m.variables) == 0 return !test && m.input &&
len(m.variables) == 0 &&
m.autoKey == ""
} }
// UIInput returns a UIInput object to be used for asking for input. // 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 // process will process the meta-parameters out of the arguments. This
// will potentially modify the args in-place. It will return the resulting // will potentially modify the args in-place. It will return the resulting
// slice. // slice.
//
// vars says whether or not we support variables.
func (m *Meta) process(args []string, vars bool) []string { func (m *Meta) process(args []string, vars bool) []string {
// We do this so that we retain the ability to technically call // We do this so that we retain the ability to technically call
// process multiple times, even if we have no plans to do so // process multiple times, even if we have no plans to do so

View File

@ -1,6 +1,9 @@
package command package command
import ( import (
"io/ioutil"
"os"
"path/filepath"
"reflect" "reflect"
"testing" "testing"
) )
@ -48,7 +51,7 @@ func TestMetaColorize(t *testing.T) {
} }
} }
func TestMetaInput(t *testing.T) { func TestMetaInputEnabled(t *testing.T) {
test = false test = false
defer func() { test = true }() defer func() { test = true }()
@ -60,7 +63,7 @@ func TestMetaInput(t *testing.T) {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if !m.Input() { if !m.InputEnabled() {
t.Fatal("should input") t.Fatal("should input")
} }
} }
@ -77,11 +80,51 @@ func TestMetaInput_disable(t *testing.T) {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if m.Input() { if m.InputEnabled() {
t.Fatal("should not input") 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) { func TestMetaInput_vars(t *testing.T) {
test = false test = false
defer func() { test = true }() defer func() { test = true }()
@ -94,7 +137,7 @@ func TestMetaInput_vars(t *testing.T) {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if m.Input() { if m.InputEnabled() {
t.Fatal("should not input") t.Fatal("should not input")
} }
} }

View File

@ -75,7 +75,7 @@ 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 c.InputEnabled() {
if err := ctx.Input(); err != nil { if err := ctx.Input(); err != nil {
c.Ui.Error(fmt.Sprintf("Error configuring: %s", err)) c.Ui.Error(fmt.Sprintf("Error configuring: %s", err))
return 1 return 1

View File

@ -92,7 +92,7 @@ func (c *RefreshCommand) Run(args []string) int {
c.Ui.Error(err.Error()) c.Ui.Error(err.Error())
return 1 return 1
} }
if c.Input() { if c.InputEnabled() {
if err := ctx.Input(); err != nil { if err := ctx.Input(); err != nil {
c.Ui.Error(fmt.Sprintf("Error configuring: %s", err)) c.Ui.Error(fmt.Sprintf("Error configuring: %s", err))
return 1 return 1