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

Add JSON option for "plan" and "apply" #275

Open
wants to merge 2 commits 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
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
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
8 changes: 5 additions & 3 deletions tfexec/cmd_default.go
@@ -1,3 +1,4 @@
//go:build !linux
// +build !linux

package tfexec
Expand All @@ -10,9 +11,10 @@ import (

func (tf *Terraform) runTerraformCmd(ctx context.Context, cmd *exec.Cmd) error {
var errBuf strings.Builder

cmd.Stdout = mergeWriters(cmd.Stdout, tf.stdout)
cmd.Stderr = mergeWriters(cmd.Stderr, tf.stderr, &errBuf)
// ensure we don't mix up stdout and stderr
sw := &syncWriter{w: &errBuf}
cmd.Stdout = mergeWriters(cmd.Stdout, tf.stdout, sw)
cmd.Stderr = mergeWriters(cmd.Stderr, tf.stderr, sw)

go func() {
<-ctx.Done()
Expand Down
22 changes: 19 additions & 3 deletions tfexec/cmd_linux.go
Expand Up @@ -2,16 +2,32 @@ package tfexec

import (
"context"
"io"
"os/exec"
"strings"
"sync"
"syscall"
)

// syncWriter is an io.Writer protected by a sync.Mutex.
type syncWriter struct {
sync.Mutex
w io.Writer
}

// Write implements io.Writer.
func (w *syncWriter) Write(p []byte) (int, error) {
w.Lock()
defer w.Unlock()
return w.w.Write(p)
}

func (tf *Terraform) runTerraformCmd(ctx context.Context, cmd *exec.Cmd) error {
var errBuf strings.Builder

cmd.Stdout = mergeWriters(cmd.Stdout, tf.stdout)
cmd.Stderr = mergeWriters(cmd.Stderr, tf.stderr, &errBuf)
// ensure we don't mix up stdout and stderr
sw := &syncWriter{w: &errBuf}
cmd.Stdout = mergeWriters(cmd.Stdout, tf.stdout, sw)
cmd.Stderr = mergeWriters(cmd.Stderr, tf.stderr, sw)

cmd.SysProcAttr = &syscall.SysProcAttr{
// kill children if parent is dead
Expand Down
10 changes: 10 additions & 0 deletions tfexec/options.go
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
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
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