/
force_unlock.go
50 lines (37 loc) · 1.06 KB
/
force_unlock.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
package tfexec
import (
"context"
"os/exec"
)
type forceUnlockConfig struct {
dir string
}
var defaultForceUnlockOptions = forceUnlockConfig{}
type ForceUnlockOption interface {
configureForceUnlock(*forceUnlockConfig)
}
func (opt *DirOption) configureForceUnlock(conf *forceUnlockConfig) {
conf.dir = opt.path
}
// ForceUnlock represents the `terraform force-unlock` command
func (tf *Terraform) ForceUnlock(ctx context.Context, lockID string, opts ...ForceUnlockOption) error {
unlockCmd := tf.forceUnlockCmd(ctx, lockID, opts...)
if err := tf.runTerraformCmd(ctx, unlockCmd); err != nil {
return err
}
return nil
}
func (tf *Terraform) forceUnlockCmd(ctx context.Context, lockID string, opts ...ForceUnlockOption) *exec.Cmd {
c := defaultForceUnlockOptions
for _, o := range opts {
o.configureForceUnlock(&c)
}
args := []string{"force-unlock", "-no-color", "-force"}
// positional arguments
args = append(args, lockID)
// optional positional arguments
if c.dir != "" {
args = append(args, c.dir)
}
return tf.buildTerraformCmd(ctx, nil, args...)
}