/
state_mv.go
108 lines (87 loc) · 2.43 KB
/
state_mv.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfexec
import (
"context"
"os/exec"
"strconv"
)
type stateMvConfig struct {
backup string
backupOut string
dryRun bool
lock bool
lockTimeout string
state string
stateOut string
}
var defaultStateMvOptions = stateMvConfig{
lock: true,
lockTimeout: "0s",
}
// StateMvCmdOption represents options used in the Refresh method.
type StateMvCmdOption interface {
configureStateMv(*stateMvConfig)
}
func (opt *BackupOption) configureStateMv(conf *stateMvConfig) {
conf.backup = opt.path
}
func (opt *BackupOutOption) configureStateMv(conf *stateMvConfig) {
conf.backupOut = opt.path
}
func (opt *DryRunOption) configureStateMv(conf *stateMvConfig) {
conf.dryRun = opt.dryRun
}
func (opt *LockOption) configureStateMv(conf *stateMvConfig) {
conf.lock = opt.lock
}
func (opt *LockTimeoutOption) configureStateMv(conf *stateMvConfig) {
conf.lockTimeout = opt.timeout
}
func (opt *StateOption) configureStateMv(conf *stateMvConfig) {
conf.state = opt.path
}
func (opt *StateOutOption) configureStateMv(conf *stateMvConfig) {
conf.stateOut = opt.path
}
// StateMv represents the terraform state mv subcommand.
func (tf *Terraform) StateMv(ctx context.Context, source string, destination string, opts ...StateMvCmdOption) error {
cmd, err := tf.stateMvCmd(ctx, source, destination, opts...)
if err != nil {
return err
}
return tf.runTerraformCmd(ctx, cmd)
}
func (tf *Terraform) stateMvCmd(ctx context.Context, source string, destination string, opts ...StateMvCmdOption) (*exec.Cmd, error) {
c := defaultStateMvOptions
for _, o := range opts {
o.configureStateMv(&c)
}
args := []string{"state", "mv", "-no-color"}
// string opts: only pass if set
if c.backup != "" {
args = append(args, "-backup="+c.backup)
}
if c.backupOut != "" {
args = append(args, "-backup-out="+c.backupOut)
}
if c.lockTimeout != "" {
args = append(args, "-lock-timeout="+c.lockTimeout)
}
if c.state != "" {
args = append(args, "-state="+c.state)
}
if c.stateOut != "" {
args = append(args, "-state-out="+c.stateOut)
}
// boolean and numerical opts: always pass
args = append(args, "-lock="+strconv.FormatBool(c.lock))
// unary flags: pass if true
if c.dryRun {
args = append(args, "-dry-run")
}
// positional arguments
args = append(args, source)
args = append(args, destination)
return tf.buildTerraformCmd(ctx, nil, args...), nil
}