/
untaint.go
78 lines (62 loc) · 1.76 KB
/
untaint.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
package tfexec
import (
"context"
"fmt"
"os/exec"
"strconv"
)
type untaintConfig struct {
state string
allowMissing bool
lock bool
lockTimeout string
}
var defaultUntaintOptions = untaintConfig{
allowMissing: false,
lock: true,
}
// OutputOption represents options used in the Output method.
type UntaintOption interface {
configureUntaint(*untaintConfig)
}
func (opt *StateOption) configureUntaint(conf *untaintConfig) {
conf.state = opt.path
}
func (opt *AllowMissingOption) configureUntaint(conf *untaintConfig) {
conf.allowMissing = opt.allowMissing
}
func (opt *LockOption) configureUntaint(conf *untaintConfig) {
conf.lock = opt.lock
}
func (opt *LockTimeoutOption) configureUntaint(conf *untaintConfig) {
conf.lockTimeout = opt.timeout
}
// Untaint represents the terraform untaint subcommand.
func (tf *Terraform) Untaint(ctx context.Context, address string, opts ...UntaintOption) error {
err := tf.compatible(ctx, tf0_6_13, nil)
if err != nil {
return fmt.Errorf("untaint was first introduced in Terraform 0.6.13: %w", err)
}
untaintCmd := tf.untaintCmd(ctx, address, opts...)
return tf.runTerraformCmd(ctx, untaintCmd)
}
func (tf *Terraform) untaintCmd(ctx context.Context, address string, opts ...UntaintOption) *exec.Cmd {
c := defaultUntaintOptions
for _, o := range opts {
o.configureUntaint(&c)
}
args := []string{"untaint", "-no-color"}
if c.lockTimeout != "" {
args = append(args, "-lock-timeout="+c.lockTimeout)
}
// string opts: only pass if set
if c.state != "" {
args = append(args, "-state="+c.state)
}
args = append(args, "-lock="+strconv.FormatBool(c.lock))
if c.allowMissing {
args = append(args, "-allow-missing")
}
args = append(args, address)
return tf.buildTerraformCmd(ctx, nil, args...)
}