Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 5b0859b

Browse files
committed
command: Input tells us whether we should ask for input or not
1 parent a918833 commit 5b0859b

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

command/meta.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Meta struct {
3030
// Variables for the context (private)
3131
autoKey string
3232
autoVariables map[string]string
33+
input bool
3334
variables map[string]string
3435

3536
color bool
@@ -105,6 +106,11 @@ func (m *Meta) Context(copts contextOpts) (*terraform.Context, bool, error) {
105106
return ctx, false, nil
106107
}
107108

109+
// Input returns true if we should ask for input for context.
110+
func (m *Meta) Input() bool {
111+
return m.input && len(m.variables) == 0
112+
}
113+
108114
// contextOpts returns the options to use to initialize a Terraform
109115
// context with the settings from this Meta.
110116
func (m *Meta) contextOpts() *terraform.ContextOpts {
@@ -127,13 +133,15 @@ func (m *Meta) contextOpts() *terraform.ContextOpts {
127133
vs[k] = v
128134
}
129135
opts.Variables = vs
136+
opts.UIInput = new(UIInput)
130137

131138
return &opts
132139
}
133140

134141
// flags adds the meta flags to the given FlagSet.
135142
func (m *Meta) flagSet(n string) *flag.FlagSet {
136143
f := flag.NewFlagSet(n, flag.ContinueOnError)
144+
f.BoolVar(&m.input, "input", true, "input")
137145
f.Var((*FlagVar)(&m.variables), "var", "variables")
138146
f.Var((*FlagVarFile)(&m.variables), "var-file", "variable file")
139147

command/meta_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,45 @@ func TestMetaColorize(t *testing.T) {
4747
t.Fatal("should be disabled")
4848
}
4949
}
50+
51+
func TestMetaInput(t *testing.T) {
52+
m := new(Meta)
53+
args := []string{}
54+
55+
fs := m.flagSet("foo")
56+
if err := fs.Parse(args); err != nil {
57+
t.Fatalf("err: %s", err)
58+
}
59+
60+
if !m.Input() {
61+
t.Fatal("should input")
62+
}
63+
}
64+
65+
func TestMetaInput_disable(t *testing.T) {
66+
m := new(Meta)
67+
args := []string{"-input=false"}
68+
69+
fs := m.flagSet("foo")
70+
if err := fs.Parse(args); err != nil {
71+
t.Fatalf("err: %s", err)
72+
}
73+
74+
if m.Input() {
75+
t.Fatal("should not input")
76+
}
77+
}
78+
79+
func TestMetaInput_vars(t *testing.T) {
80+
m := new(Meta)
81+
args := []string{"-var", "foo=bar"}
82+
83+
fs := m.flagSet("foo")
84+
if err := fs.Parse(args); err != nil {
85+
t.Fatalf("err: %s", err)
86+
}
87+
88+
if m.Input() {
89+
t.Fatal("should not input")
90+
}
91+
}

command/plan.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ Options:
167167
-destroy If set, a plan will be generated to destroy all resources
168168
managed by the given configuration and state.
169169
170+
-input=true Ask for input for variables if not directly set.
171+
170172
-module-depth=n Specifies the depth of modules to show in the output.
171173
This does not affect the plan itself, only the output
172174
shown. By default, this is zero. -1 will expand all.

0 commit comments

Comments
 (0)