Skip to content

Commit

Permalink
Simplify JSON execs
Browse files Browse the repository at this point in the history
  • Loading branch information
paultyng committed Sep 1, 2020
1 parent 821c81e commit 3a3bbe7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 51 deletions.
21 changes: 21 additions & 0 deletions tfexec/cmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package tfexec

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -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

Expand Down
12 changes: 1 addition & 11 deletions tfexec/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tfexec

import (
"bytes"
"context"
"encoding/json"
"os/exec"
Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 1 addition & 11 deletions tfexec/providers_schema.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package tfexec

import (
"bytes"
"context"
"encoding/json"
"os/exec"

tfjson "github.com/hashicorp/terraform-json"
Expand All @@ -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
}
Expand Down
32 changes: 3 additions & 29 deletions tfexec/show.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package tfexec

import (
"bytes"
"context"
"encoding/json"
"fmt"
"os/exec"

Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit 3a3bbe7

Please sign in to comment.