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 ShowPlanFileRaw #83

Merged
merged 2 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 45 additions & 5 deletions tfexec/show.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tfexec

import (
"bytes"
"context"
"fmt"
"os/exec"
Expand Down Expand Up @@ -45,7 +46,7 @@ func (tf *Terraform) Show(ctx context.Context, opts ...ShowOption) (*tfjson.Stat
mergeEnv[reattachEnvVar] = reattachStr
}

showCmd := tf.showCmd(ctx, mergeEnv)
showCmd := tf.showCmd(ctx, true, mergeEnv)

var ret tfjson.State
err = tf.runTerraformCmdJSON(showCmd, &ret)
Expand Down Expand Up @@ -87,7 +88,7 @@ func (tf *Terraform) ShowStateFile(ctx context.Context, statePath string, opts .
mergeEnv[reattachEnvVar] = reattachStr
}

showCmd := tf.showCmd(ctx, mergeEnv, statePath)
showCmd := tf.showCmd(ctx, true, mergeEnv, statePath)

var ret tfjson.State
err = tf.runTerraformCmdJSON(showCmd, &ret)
Expand Down Expand Up @@ -129,7 +130,7 @@ func (tf *Terraform) ShowPlanFile(ctx context.Context, planPath string, opts ...
mergeEnv[reattachEnvVar] = reattachStr
}

showCmd := tf.showCmd(ctx, mergeEnv, planPath)
showCmd := tf.showCmd(ctx, true, mergeEnv, planPath)

var ret tfjson.Plan
err = tf.runTerraformCmdJSON(showCmd, &ret)
Expand All @@ -146,8 +147,47 @@ func (tf *Terraform) ShowPlanFile(ctx context.Context, planPath string, opts ...

}

func (tf *Terraform) showCmd(ctx context.Context, mergeEnv map[string]string, args ...string) *exec.Cmd {
allArgs := []string{"show", "-json", "-no-color"}
// ShowRawPlanFile reads a given plan file and outputs the plan in a
// human-friendly, opaque format.
func (tf *Terraform) ShowRawPlanFile(ctx context.Context, planPath string, opts ...ShowOption) (string, error) {
if planPath == "" {
return "", fmt.Errorf("planPath cannot be blank: use Show() if not passing planPath")
}

c := defaultShowOptions

for _, o := range opts {
o.configureShow(&c)
}

mergeEnv := map[string]string{}
if c.reattachInfo != nil {
reattachStr, err := c.reattachInfo.marshalString()
if err != nil {
return "", err
}
mergeEnv[reattachEnvVar] = reattachStr
}

showCmd := tf.showCmd(ctx, false, mergeEnv, planPath)

var ret bytes.Buffer
showCmd.Stdout = &ret
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to use mergewriters just in case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mergeWriters is already called by tf.runTerraformCmd, though? In fact, that's the only command that calls it.

err := tf.runTerraformCmd(showCmd)
if err != nil {
return "", err
}

return ret.String(), nil

}

func (tf *Terraform) showCmd(ctx context.Context, jsonOutput bool, mergeEnv map[string]string, args ...string) *exec.Cmd {
allArgs := []string{"show"}
if jsonOutput {
allArgs = append(allArgs, "-json")
}
allArgs = append(allArgs, "-no-color")
allArgs = append(allArgs, args...)

return tf.buildTerraformCmd(ctx, mergeEnv, allArgs...)
Expand Down
6 changes: 3 additions & 3 deletions tfexec/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestShowCmd(t *testing.T) {
tf.SetEnv(map[string]string{})

// defaults
showCmd := tf.showCmd(context.Background(), nil)
showCmd := tf.showCmd(context.Background(), true, nil)

assertCmd(t, []string{
"show",
Expand All @@ -42,7 +42,7 @@ func TestShowStateFileCmd(t *testing.T) {
// empty env, to avoid environ mismatch in testing
tf.SetEnv(map[string]string{})

showCmd := tf.showCmd(context.Background(), nil, "statefilepath")
showCmd := tf.showCmd(context.Background(), true, nil, "statefilepath")

assertCmd(t, []string{
"show",
Expand All @@ -64,7 +64,7 @@ func TestShowPlanFileCmd(t *testing.T) {
// empty env, to avoid environ mismatch in testing
tf.SetEnv(map[string]string{})

showCmd := tf.showCmd(context.Background(), nil, "planfilepath")
showCmd := tf.showCmd(context.Background(), true, nil, "planfilepath")

assertCmd(t, []string{
"show",
Expand Down