-
Notifications
You must be signed in to change notification settings - Fork 109
/
state_push.go
70 lines (53 loc) · 1.46 KB
/
state_push.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
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfexec
import (
"context"
"os/exec"
"strconv"
)
type statePushConfig struct {
force bool
lock bool
lockTimeout string
}
var defaultStatePushOptions = statePushConfig{
lock: false,
lockTimeout: "0s",
}
// StatePushCmdOption represents options used in the Refresh method.
type StatePushCmdOption interface {
configureStatePush(*statePushConfig)
}
func (opt *ForceOption) configureStatePush(conf *statePushConfig) {
conf.force = opt.force
}
func (opt *LockOption) configureStatePush(conf *statePushConfig) {
conf.lock = opt.lock
}
func (opt *LockTimeoutOption) configureStatePush(conf *statePushConfig) {
conf.lockTimeout = opt.timeout
}
func (tf *Terraform) StatePush(ctx context.Context, path string, opts ...StatePushCmdOption) error {
cmd, err := tf.statePushCmd(ctx, path, opts...)
if err != nil {
return err
}
return tf.runTerraformCmd(ctx, cmd)
}
func (tf *Terraform) statePushCmd(ctx context.Context, path string, opts ...StatePushCmdOption) (*exec.Cmd, error) {
c := defaultStatePushOptions
for _, o := range opts {
o.configureStatePush(&c)
}
args := []string{"state", "push"}
if c.force {
args = append(args, "-force")
}
args = append(args, "-lock="+strconv.FormatBool(c.lock))
if c.lockTimeout != "" {
args = append(args, "-lock-timeout="+c.lockTimeout)
}
args = append(args, path)
return tf.buildTerraformCmd(ctx, nil, args...), nil
}