Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json log for plan and apply #329

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions tfexec/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type applyConfig struct {
backup string
dirOrPlan string
lock bool
json bool

// LockTimeout must be a string with time unit, e.g. '10s'
lockTimeout string
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 @@ -112,6 +117,9 @@ func (tf *Terraform) applyCmd(ctx context.Context, opts ...ApplyOption) (*exec.C
if c.backup != "" {
args = append(args, "-backup="+c.backup)
}
if c.json {
args = append(args, "-json")
}
if c.lockTimeout != "" {
args = append(args, "-lock-timeout="+c.lockTimeout)
}
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 @@ -47,6 +48,7 @@ func TestApplyCmd(t *testing.T) {
"-auto-approve",
"-input=false",
"-backup=testbackup",
"-json",
"-lock-timeout=200s",
"-state=teststate",
"-state-out=teststateout",
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 @@ -121,6 +126,9 @@ func (tf *Terraform) planCmd(ctx context.Context, opts ...PlanOption) (*exec.Cmd
if c.lockTimeout != "" {
args = append(args, "-lock-timeout="+c.lockTimeout)
}
if c.json {
args = append(args, "-json")
}
if c.out != "" {
args = append(args, "-out="+c.out)
}
Expand Down
2 changes: 2 additions & 0 deletions tfexec/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestPlanCmd(t *testing.T) {
planCmd, err := tf.planCmd(context.Background(),
Destroy(true),
Lock(false),
JSON(true),
LockTimeout("22s"),
Out("whale"),
Parallelism(42),
Expand All @@ -63,6 +64,7 @@ func TestPlanCmd(t *testing.T) {
"-input=false",
"-detailed-exitcode",
"-lock-timeout=22s",
"-json",
"-out=whale",
"-state=marvin",
"-var-file=trillian",
Expand Down