Merge pull request #1756 from hashicorp/f-input-env-var

command: add env var equivalent to -input arg
This commit is contained in:
Paul Hinze 2015-04-30 14:36:59 -05:00
commit 99ce760569
2 changed files with 50 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"github.com/hashicorp/terraform/config/module" "github.com/hashicorp/terraform/config/module"
"github.com/hashicorp/terraform/state" "github.com/hashicorp/terraform/state"
@ -165,6 +166,13 @@ func (m *Meta) DataDir() string {
return dataDir return dataDir
} }
const (
// InputModeEnvVar is the environment variable that, if set to "false" or
// "0", causes terraform commands to behave as if the `-input=false` flag was
// specified.
InputModeEnvVar = "TF_INPUT"
)
// InputMode returns the type of input we should ask for in the form of // InputMode returns the type of input we should ask for in the form of
// terraform.InputMode which is passed directly to Context.Input. // terraform.InputMode which is passed directly to Context.Input.
func (m *Meta) InputMode() terraform.InputMode { func (m *Meta) InputMode() terraform.InputMode {
@ -172,6 +180,14 @@ func (m *Meta) InputMode() terraform.InputMode {
return 0 return 0
} }
if envVar := os.Getenv(InputModeEnvVar); envVar != "" {
if v, err := strconv.ParseBool(envVar); err == nil {
if !v {
return 0
}
}
}
var mode terraform.InputMode var mode terraform.InputMode
mode |= terraform.InputModeProvider mode |= terraform.InputModeProvider
if len(m.variables) == 0 && m.autoKey == "" { if len(m.variables) == 0 && m.autoKey == "" {

View File

@ -70,6 +70,40 @@ func TestMetaInputMode(t *testing.T) {
} }
} }
func TestMetaInputMode_envVar(t *testing.T) {
test = false
defer func() { test = true }()
old := os.Getenv(InputModeEnvVar)
defer os.Setenv(InputModeEnvVar, old)
m := new(Meta)
args := []string{}
fs := m.flagSet("foo")
if err := fs.Parse(args); err != nil {
t.Fatalf("err: %s", err)
}
off := terraform.InputMode(0)
on := terraform.InputModeStd | terraform.InputModeVarUnset
cases := []struct {
EnvVar string
Expected terraform.InputMode
}{
{"false", off},
{"0", off},
{"true", on},
{"1", on},
}
for _, tc := range cases {
os.Setenv(InputModeEnvVar, tc.EnvVar)
if m.InputMode() != tc.Expected {
t.Fatalf("expected InputMode: %#v, got: %#v", tc.Expected, m.InputMode())
}
}
}
func TestMetaInputMode_disable(t *testing.T) { func TestMetaInputMode_disable(t *testing.T) {
test = false test = false
defer func() { test = true }() defer func() { test = true }()