From 0ef79972185937f7fd97f554745d402d0a1f6730 Mon Sep 17 00:00:00 2001 From: Karol Pasternak Date: Tue, 31 Aug 2021 09:19:58 -0700 Subject: [PATCH] statePull: tfjson does not support state subcommand, output string instead --- tfexec/state_pull.go | 40 ++++++++++++++++++++++----------------- tfexec/state_pull_test.go | 2 +- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/tfexec/state_pull.go b/tfexec/state_pull.go index 48a97c1b..11b6b9c7 100644 --- a/tfexec/state_pull.go +++ b/tfexec/state_pull.go @@ -1,10 +1,9 @@ package tfexec import ( + "bytes" "context" "os/exec" - - tfjson "github.com/hashicorp/terraform-json" ) type statePullConfig struct { @@ -13,37 +12,44 @@ type statePullConfig struct { var defaultStatePullConfig = statePullConfig{} -func (tf *Terraform) StatePull(ctx context.Context) (*tfjson.State, error) { +type StatePullOption interface { + configureShow(*statePullConfig) +} + +func (opt *ReattachOption) configureStatePull(conf *statePullConfig) { + conf.reattachInfo = opt.info +} + +func (tf *Terraform) StatePull(ctx context.Context, opts ...StatePullOption) (string, error) { c := defaultStatePullConfig + for _, o := range opts { + o.configureShow(&c) + } + mergeEnv := map[string]string{} if c.reattachInfo != nil { reattachStr, err := c.reattachInfo.marshalString() if err != nil { - return nil, err + return "", err } mergeEnv[reattachEnvVar] = reattachStr } - cmd := tf.statePullCmd(ctx) - - var ret tfjson.State - ret.UseJSONNumber(true) - err := tf.runTerraformCmdJSON(ctx, cmd, &ret) - if err != nil { - return nil, err - } + cmd := tf.statePullCmd(ctx, mergeEnv) - err = ret.Validate() + var ret bytes.Buffer + cmd.Stdout = &ret + err := tf.runTerraformCmd(ctx, cmd) if err != nil { - return nil, err + return "", err } - return &ret, nil + return ret.String(), nil } -func (tf *Terraform) statePullCmd(ctx context.Context) *exec.Cmd { +func (tf *Terraform) statePullCmd(ctx context.Context, mergeEnv map[string]string) *exec.Cmd { args := []string{"state", "pull"} - return tf.buildTerraformCmd(ctx, nil, args...) + return tf.buildTerraformCmd(ctx, mergeEnv, args...) } diff --git a/tfexec/state_pull_test.go b/tfexec/state_pull_test.go index 48c347a7..4ef6ef2e 100644 --- a/tfexec/state_pull_test.go +++ b/tfexec/state_pull_test.go @@ -18,7 +18,7 @@ func TestStatePull(t *testing.T) { tf.SetEnv(map[string]string{}) t.Run("tfstate", func(t *testing.T) { - statePullCmd := tf.statePullCmd(context.Background()) + statePullCmd := tf.statePullCmd(context.Background(), nil) assertCmd(t, []string{ "state",