Skip to content

Commit a4de84a

Browse files
committed
feat: Added env var support
* to be used like ```hcl var options = []tfexec.PlanOption{ tfexec.EnvVar("SOME_ENV_VAR", "SOME_VALUE"), tfexec.EnvVar("OTHER_ENV_VAR", "OTHER_VALUE"), } ```
1 parent 8d07178 commit a4de84a

File tree

9 files changed

+84
-6
lines changed

9 files changed

+84
-6
lines changed

tfexec/apply.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ type applyConfig struct {
2525
// Vars: each var must be supplied as a single string, e.g. 'foo=bar'
2626
vars []string
2727
varFiles []string
28+
29+
// EnvVars support
30+
envVars map[string]string
2831
}
2932

3033
var defaultApplyOptions = applyConfig{
3134
lock: true,
3235
parallelism: 10,
3336
refresh: true,
37+
envVars: map[string]string{},
3438
}
3539

3640
// ApplyOption represents options used in the Apply method.
@@ -66,6 +70,10 @@ func (opt *VarFileOption) configureApply(conf *applyConfig) {
6670
conf.varFiles = append(conf.varFiles, opt.path)
6771
}
6872

73+
func (opt *EnvVarOption) configureApply(conf *applyConfig) {
74+
conf.envVars[opt.key] = opt.value
75+
}
76+
6977
func (opt *LockOption) configureApply(conf *applyConfig) {
7078
conf.lock = opt.lock
7179
}
@@ -157,6 +165,11 @@ func (tf *Terraform) applyCmd(ctx context.Context, opts ...ApplyOption) (*exec.C
157165
}
158166

159167
mergeEnv := map[string]string{}
168+
169+
for kvar, kvalue := range c.envVars {
170+
mergeEnv[kvar] = kvalue
171+
}
172+
160173
if c.reattachInfo != nil {
161174
reattachStr, err := c.reattachInfo.marshalString()
162175
if err != nil {

tfexec/apply_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ func TestApplyCmd(t *testing.T) {
2626
StateOut("teststateout"),
2727
VarFile("foo.tfvars"),
2828
VarFile("bar.tfvars"),
29+
EnvVar("blah", "diblah"),
30+
EnvVar("other_env_var", "other_value"),
2931
Lock(false),
3032
Parallelism(99),
3133
Refresh(false),
@@ -62,6 +64,6 @@ func TestApplyCmd(t *testing.T) {
6264
"-var", "var1=foo",
6365
"-var", "var2=bar",
6466
"testfile",
65-
}, nil, applyCmd)
67+
}, map[string]string{"blah": "diblah", "other_env_var": "other_value"}, applyCmd)
6668
})
6769
}

tfexec/destroy.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@ type destroyConfig struct {
2424
// Vars: each var must be supplied as a single string, e.g. 'foo=bar'
2525
vars []string
2626
varFiles []string
27+
28+
// EnvVars
29+
envVars map[string]string
2730
}
2831

2932
var defaultDestroyOptions = destroyConfig{
3033
lock: true,
3134
lockTimeout: "0s",
3235
parallelism: 10,
3336
refresh: true,
37+
envVars: map[string]string{},
3438
}
3539

3640
// DestroyOption represents options used in the Destroy method.
@@ -70,6 +74,10 @@ func (opt *VarFileOption) configureDestroy(conf *destroyConfig) {
7074
conf.varFiles = append(conf.varFiles, opt.path)
7175
}
7276

77+
func (opt *EnvVarOption) configureDestroy(conf *destroyConfig) {
78+
conf.envVars[opt.key] = opt.value
79+
}
80+
7381
func (opt *LockOption) configureDestroy(conf *destroyConfig) {
7482
conf.lock = opt.lock
7583
}
@@ -144,6 +152,11 @@ func (tf *Terraform) destroyCmd(ctx context.Context, opts ...DestroyOption) (*ex
144152
}
145153

146154
mergeEnv := map[string]string{}
155+
156+
for kvar, kvalue := range c.envVars {
157+
mergeEnv[kvar] = kvalue
158+
}
159+
147160
if c.reattachInfo != nil {
148161
reattachStr, err := c.reattachInfo.marshalString()
149162
if err != nil {

tfexec/destroy_test.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,22 @@ func TestDestroyCmd(t *testing.T) {
3737
})
3838

3939
t.Run("override all defaults", func(t *testing.T) {
40-
destroyCmd, err := tf.destroyCmd(context.Background(), Backup("testbackup"), LockTimeout("200s"), State("teststate"), StateOut("teststateout"), VarFile("testvarfile"), Lock(false), Parallelism(99), Refresh(false), Target("target1"), Target("target2"), Var("var1=foo"), Var("var2=bar"), Dir("destroydir"))
40+
destroyCmd, err := tf.destroyCmd(context.Background(),
41+
Backup("testbackup"),
42+
LockTimeout("200s"),
43+
State("teststate"),
44+
StateOut("teststateout"),
45+
VarFile("testvarfile"),
46+
Lock(false),
47+
Parallelism(99),
48+
Refresh(false),
49+
Target("target1"),
50+
Target("target2"),
51+
Var("var1=foo"),
52+
Var("var2=bar"),
53+
Dir("destroydir"),
54+
EnvVar("blah", "diblah"),
55+
EnvVar("other_env_var", "other_value"))
4156
if err != nil {
4257
t.Fatal(err)
4358
}
@@ -60,6 +75,6 @@ func TestDestroyCmd(t *testing.T) {
6075
"-var", "var1=foo",
6176
"-var", "var2=bar",
6277
"destroydir",
63-
}, nil, destroyCmd)
78+
}, map[string]string{"blah": "diblah", "other_env_var": "other_value"}, destroyCmd)
6479
})
6580
}

tfexec/import.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ type importConfig struct {
1919
stateOut string
2020
vars []string
2121
varFiles []string
22+
envVars map[string]string
2223
}
2324

2425
var defaultImportOptions = importConfig{
2526
allowMissingConfig: false,
2627
lock: true,
2728
lockTimeout: "0s",
29+
envVars: map[string]string{},
2830
}
2931

3032
// ImportOption represents options used in the Import method.
@@ -72,6 +74,10 @@ func (opt *VarFileOption) configureImport(conf *importConfig) {
7274
conf.varFiles = append(conf.varFiles, opt.path)
7375
}
7476

77+
func (opt *EnvVarOption) configureImport(conf *importConfig) {
78+
conf.envVars[opt.key] = opt.value
79+
}
80+
7581
// Import represents the terraform import subcommand.
7682
func (tf *Terraform) Import(ctx context.Context, address, id string, opts ...ImportOption) error {
7783
cmd, err := tf.importCmd(ctx, address, id, opts...)
@@ -129,6 +135,11 @@ func (tf *Terraform) importCmd(ctx context.Context, address, id string, opts ...
129135
args = append(args, address, id)
130136

131137
mergeEnv := map[string]string{}
138+
139+
for kvar, kvalue := range c.envVars {
140+
mergeEnv[kvar] = kvalue
141+
}
142+
132143
if c.reattachInfo != nil {
133144
reattachStr, err := c.reattachInfo.marshalString()
134145
if err != nil {

tfexec/import_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ func TestImportCmd(t *testing.T) {
4646
Var("var1=foo"),
4747
Var("var2=bar"),
4848
AllowMissingConfig(true),
49+
EnvVar("blah", "diblah"),
50+
EnvVar("other_env_var", "other_value"),
4951
)
5052
if err != nil {
5153
t.Fatal(err)
@@ -66,6 +68,6 @@ func TestImportCmd(t *testing.T) {
6668
"-var", "var2=bar",
6769
"my-addr2",
6870
"my-id2",
69-
}, nil, importCmd)
71+
}, map[string]string{"blah": "diblah", "other_env_var": "other_value"}, importCmd)
7072
})
7173
}

tfexec/options.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ func DryRun(dryRun bool) *DryRunOption {
117117
return &DryRunOption{dryRun}
118118
}
119119

120+
type EnvVarOption struct {
121+
key, value string
122+
}
123+
124+
// EnvVar represents a kv env var
125+
func EnvVar(key, value string) *EnvVarOption {
126+
return &EnvVarOption{key, value}
127+
}
128+
120129
type FSMirrorOption struct {
121130
fsMirror string
122131
}

tfexec/plan.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type planConfig struct {
2121
targets []string
2222
vars []string
2323
varFiles []string
24+
envVars map[string]string
2425
}
2526

2627
var defaultPlanOptions = planConfig{
@@ -29,6 +30,7 @@ var defaultPlanOptions = planConfig{
2930
lockTimeout: "0s",
3031
parallelism: 10,
3132
refresh: true,
33+
envVars: map[string]string{},
3234
}
3335

3436
// PlanOption represents options used in the Plan method.
@@ -44,6 +46,10 @@ func (opt *VarFileOption) configurePlan(conf *planConfig) {
4446
conf.varFiles = append(conf.varFiles, opt.path)
4547
}
4648

49+
func (opt *EnvVarOption) configurePlan(conf *planConfig) {
50+
conf.envVars[opt.key] = opt.value
51+
}
52+
4753
func (opt *VarOption) configurePlan(conf *planConfig) {
4854
conf.vars = append(conf.vars, opt.assignment)
4955
}
@@ -168,6 +174,11 @@ func (tf *Terraform) planCmd(ctx context.Context, opts ...PlanOption) (*exec.Cmd
168174
}
169175

170176
mergeEnv := map[string]string{}
177+
178+
for kvar, kvalue := range c.envVars {
179+
mergeEnv[kvar] = kvalue
180+
}
181+
171182
if c.reattachInfo != nil {
172183
reattachStr, err := c.reattachInfo.marshalString()
173184
if err != nil {

tfexec/plan_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ func TestPlanCmd(t *testing.T) {
5252
Var("android=paranoid"),
5353
Var("brain_size=planet"),
5454
VarFile("trillian"),
55-
Dir("earth"))
55+
Dir("earth"),
56+
EnvVar("blah", "diblah"),
57+
EnvVar("other_env_var", "other_value"))
5658
if err != nil {
5759
t.Fatal(err)
5860
}
@@ -77,6 +79,6 @@ func TestPlanCmd(t *testing.T) {
7779
"-var", "android=paranoid",
7880
"-var", "brain_size=planet",
7981
"earth",
80-
}, nil, planCmd)
82+
}, map[string]string{"blah": "diblah", "other_env_var": "other_value"}, planCmd)
8183
})
8284
}

0 commit comments

Comments
 (0)