Skip to content

Commit

Permalink
Add JSON option for "plan" and "apply"
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecarbs authored and rstandt committed Jun 23, 2022
1 parent 219508b commit 9c23709
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tfexec/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions tfexec/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions tfexec/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tfexec/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type planConfig struct {
destroy bool
dir string
lock bool
json bool
lockTimeout string
out string
parallelism int
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions tfexec/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -65,6 +66,7 @@ func TestPlanCmd(t *testing.T) {
"-lock-timeout=22s",
"-out=whale",
"-state=marvin",
"-json",
"-var-file=trillian",
"-lock=false",
"-parallelism=42",
Expand Down

0 comments on commit 9c23709

Please sign in to comment.