diff --git a/tfexec/cmd.go b/tfexec/cmd.go index a496590a..f96e103d 100644 --- a/tfexec/cmd.go +++ b/tfexec/cmd.go @@ -1,7 +1,9 @@ package tfexec import ( + "bytes" "context" + "encoding/json" "fmt" "io" "os" @@ -115,6 +117,25 @@ func (tf *Terraform) buildTerraformCmd(ctx context.Context, args ...string) *exe return cmd } +func (tf *Terraform) runTerraformCmdJSON(cmd *exec.Cmd, v interface{}) error { + var outbuf = bytes.Buffer{} + + { + var stdout io.Writer = &outbuf + if cmd.Stdout != nil { + stdout = io.MultiWriter(cmd.Stdout, stdout) + } + cmd.Stdout = stdout + } + + err := tf.runTerraformCmd(cmd) + if err != nil { + return err + } + + return json.Unmarshal(outbuf.Bytes(), v) +} + func (tf *Terraform) runTerraformCmd(cmd *exec.Cmd) error { var errBuf strings.Builder diff --git a/tfexec/output.go b/tfexec/output.go index 25ab025b..baf5803c 100644 --- a/tfexec/output.go +++ b/tfexec/output.go @@ -1,7 +1,6 @@ package tfexec import ( - "bytes" "context" "encoding/json" "os/exec" @@ -35,17 +34,8 @@ type OutputMeta struct { func (tf *Terraform) Output(ctx context.Context, opts ...OutputOption) (map[string]OutputMeta, error) { outputCmd := tf.outputCmd(ctx, opts...) - var outBuf bytes.Buffer - outputCmd.Stdout = &outBuf - outputs := map[string]OutputMeta{} - - err := tf.runTerraformCmd(outputCmd) - if err != nil { - return nil, err - } - - err = json.Unmarshal(outBuf.Bytes(), &outputs) + err := tf.runTerraformCmdJSON(outputCmd, &outputs) if err != nil { return nil, err } diff --git a/tfexec/providers_schema.go b/tfexec/providers_schema.go index e0d98f67..d92be56b 100644 --- a/tfexec/providers_schema.go +++ b/tfexec/providers_schema.go @@ -1,9 +1,7 @@ package tfexec import ( - "bytes" "context" - "encoding/json" "os/exec" tfjson "github.com/hashicorp/terraform-json" @@ -13,15 +11,7 @@ func (tf *Terraform) ProvidersSchema(ctx context.Context) (*tfjson.ProviderSchem schemaCmd := tf.providersSchemaCmd(ctx) var ret tfjson.ProviderSchemas - var outBuf bytes.Buffer - schemaCmd.Stdout = &outBuf - - err := tf.runTerraformCmd(schemaCmd) - if err != nil { - return nil, err - } - - err = json.Unmarshal(outBuf.Bytes(), &ret) + err := tf.runTerraformCmdJSON(schemaCmd, &ret) if err != nil { return nil, err } diff --git a/tfexec/show.go b/tfexec/show.go index 8c74c4bc..a046b0fa 100644 --- a/tfexec/show.go +++ b/tfexec/show.go @@ -1,9 +1,7 @@ package tfexec import ( - "bytes" "context" - "encoding/json" "fmt" "os/exec" @@ -21,15 +19,7 @@ func (tf *Terraform) Show(ctx context.Context) (*tfjson.State, error) { showCmd := tf.showCmd(ctx) var ret tfjson.State - var outBuf bytes.Buffer - showCmd.Stdout = &outBuf - - err = tf.runTerraformCmd(showCmd) - if err != nil { - return nil, err - } - - err = json.Unmarshal(outBuf.Bytes(), &ret) + err = tf.runTerraformCmdJSON(showCmd, &ret) if err != nil { return nil, err } @@ -56,15 +46,7 @@ func (tf *Terraform) ShowStateFile(ctx context.Context, statePath string) (*tfjs showCmd := tf.showCmd(ctx, statePath) var ret tfjson.State - var outBuf bytes.Buffer - showCmd.Stdout = &outBuf - - err = tf.runTerraformCmd(showCmd) - if err != nil { - return nil, err - } - - err = json.Unmarshal(outBuf.Bytes(), &ret) + err = tf.runTerraformCmdJSON(showCmd, &ret) if err != nil { return nil, err } @@ -91,15 +73,7 @@ func (tf *Terraform) ShowPlanFile(ctx context.Context, planPath string) (*tfjson showCmd := tf.showCmd(ctx, planPath) var ret tfjson.Plan - var outBuf bytes.Buffer - showCmd.Stdout = &outBuf - - err = tf.runTerraformCmd(showCmd) - if err != nil { - return nil, err - } - - err = json.Unmarshal(outBuf.Bytes(), &ret) + err = tf.runTerraformCmdJSON(showCmd, &ret) if err != nil { return nil, err }