forked from hashicorp/terraform-exec
-
Notifications
You must be signed in to change notification settings - Fork 6
/
state_pull.go
58 lines (44 loc) · 1.13 KB
/
state_pull.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfexec
import (
"bytes"
"context"
"os/exec"
)
type statePullConfig struct {
reattachInfo ReattachInfo
}
var defaultStatePullConfig = statePullConfig{}
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 "", err
}
mergeEnv[reattachEnvVar] = reattachStr
}
cmd := tf.statePullCmd(ctx, mergeEnv)
var ret bytes.Buffer
cmd.Stdout = &ret
err := tf.runTerraformCmd(ctx, cmd)
if err != nil {
return "", err
}
return ret.String(), nil
}
func (tf *Terraform) statePullCmd(ctx context.Context, mergeEnv map[string]string) *exec.Cmd {
args := []string{"state", "pull"}
return tf.buildTerraformCmd(ctx, mergeEnv, args...)
}