diff --git a/tfexec/apply.go b/tfexec/apply.go index 40d9e69b..2c0d08a5 100644 --- a/tfexec/apply.go +++ b/tfexec/apply.go @@ -12,6 +12,7 @@ type applyConfig struct { dirOrPlan string lock bool + json bool // LockTimeout must be a string with time unit, e.g. '10s' lockTimeout string parallelism int @@ -66,6 +67,10 @@ func (opt *VarFileOption) configureApply(conf *applyConfig) { conf.varFiles = append(conf.varFiles, opt.path) } +func (opt *JSONOption) configureApply(conf *applyConfig) { + conf.json = opt.json +} + func (opt *LockOption) configureApply(conf *applyConfig) { conf.lock = opt.lock } @@ -121,6 +126,9 @@ func (tf *Terraform) applyCmd(ctx context.Context, opts ...ApplyOption) (*exec.C if c.stateOut != "" { args = append(args, "-state-out="+c.stateOut) } + if c.json { + args = append(args, "-json") + } for _, vf := range c.varFiles { args = append(args, "-var-file="+vf) } diff --git a/tfexec/apply_test.go b/tfexec/apply_test.go index 1cf2f562..4b0f0bf9 100644 --- a/tfexec/apply_test.go +++ b/tfexec/apply_test.go @@ -26,6 +26,7 @@ func TestApplyCmd(t *testing.T) { StateOut("teststateout"), VarFile("foo.tfvars"), VarFile("bar.tfvars"), + JSON(true), Lock(false), Parallelism(99), Refresh(false), @@ -50,6 +51,7 @@ func TestApplyCmd(t *testing.T) { "-lock-timeout=200s", "-state=teststate", "-state-out=teststateout", + "-json", "-var-file=foo.tfvars", "-var-file=bar.tfvars", "-lock=false", diff --git a/tfexec/options.go b/tfexec/options.go index ad3cc65c..7b52ca4d 100644 --- a/tfexec/options.go +++ b/tfexec/options.go @@ -185,6 +185,16 @@ func GetPlugins(getPlugins bool) *GetPluginsOption { return &GetPluginsOption{getPlugins} } +// JSONOption represents the -json flag. +type JSONOption struct { + json bool +} + +// JSON represents the -json flag. +func JSON(json bool) *JSONOption { + return &JSONOption{json} +} + // LockOption represents the -lock flag. type LockOption struct { lock bool diff --git a/tfexec/plan.go b/tfexec/plan.go index bf41094b..6d6c097a 100644 --- a/tfexec/plan.go +++ b/tfexec/plan.go @@ -11,6 +11,7 @@ type planConfig struct { destroy bool dir string lock bool + json bool lockTimeout string out string parallelism int @@ -76,6 +77,10 @@ func (opt *OutOption) configurePlan(conf *planConfig) { conf.out = opt.path } +func (opt *JSONOption) configurePlan(conf *planConfig) { + conf.json = opt.json +} + func (opt *LockTimeoutOption) configurePlan(conf *planConfig) { conf.lockTimeout = opt.timeout } @@ -127,6 +132,9 @@ func (tf *Terraform) planCmd(ctx context.Context, opts ...PlanOption) (*exec.Cmd if c.state != "" { args = append(args, "-state="+c.state) } + if c.json { + args = append(args, "-json") + } for _, vf := range c.varFiles { args = append(args, "-var-file="+vf) } diff --git a/tfexec/plan_test.go b/tfexec/plan_test.go index 7a467ac3..7a659390 100644 --- a/tfexec/plan_test.go +++ b/tfexec/plan_test.go @@ -39,6 +39,7 @@ func TestPlanCmd(t *testing.T) { t.Run("override all defaults", func(t *testing.T) { planCmd, err := tf.planCmd(context.Background(), Destroy(true), + JSON(true), Lock(false), LockTimeout("22s"), Out("whale"), @@ -65,6 +66,7 @@ func TestPlanCmd(t *testing.T) { "-lock-timeout=22s", "-out=whale", "-state=marvin", + "-json", "-var-file=trillian", "-lock=false", "-parallelism=42",