This repository has been archived by the owner on Sep 1, 2022. It is now read-only.
forked from hashicorp/terraform-exec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_rm.go
104 lines (84 loc) · 2.28 KB
/
state_rm.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
package tfexec
import (
"context"
"os/exec"
"strconv"
)
type stateRmConfig struct {
backup string
backupOut string
dryRun bool
lock bool
lockTimeout string
state string
stateOut string
}
var defaultStateRmOptions = stateRmConfig{
lock: true,
lockTimeout: "0s",
}
// StateRmCmdOption represents options used in the Refresh method.
type StateRmCmdOption interface {
configureStateRm(*stateRmConfig)
}
func (opt *BackupOption) configureStateRm(conf *stateRmConfig) {
conf.backup = opt.path
}
func (opt *BackupOutOption) configureStateRm(conf *stateRmConfig) {
conf.backupOut = opt.path
}
func (opt *DryRunOption) configureStateRm(conf *stateRmConfig) {
conf.dryRun = opt.dryRun
}
func (opt *LockOption) configureStateRm(conf *stateRmConfig) {
conf.lock = opt.lock
}
func (opt *LockTimeoutOption) configureStateRm(conf *stateRmConfig) {
conf.lockTimeout = opt.timeout
}
func (opt *StateOption) configureStateRm(conf *stateRmConfig) {
conf.state = opt.path
}
func (opt *StateOutOption) configureStateRm(conf *stateRmConfig) {
conf.stateOut = opt.path
}
// StateRm represents the terraform state rm subcommand.
func (tf *Terraform) StateRm(ctx context.Context, address string, opts ...StateRmCmdOption) error {
cmd, err := tf.stateRmCmd(ctx, address, opts...)
if err != nil {
return err
}
return tf.runTerraformCmd(ctx, cmd)
}
func (tf *Terraform) stateRmCmd(ctx context.Context, address string, opts ...StateRmCmdOption) (*exec.Cmd, error) {
c := defaultStateRmOptions
for _, o := range opts {
o.configureStateRm(&c)
}
args := []string{"state", "rm", "-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, address)
return tf.buildTerraformCmd(ctx, nil, args...), nil
}