terraform-provider-gitea/vendor/github.com/hashicorp/terraform-exec/tfexec/force_unlock.go

59 lines
1.3 KiB
Go
Raw Normal View History

2022-04-03 04:07:16 +00:00
package tfexec
import (
"context"
"fmt"
2022-04-03 04:07:16 +00:00
"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, err := tf.forceUnlockCmd(ctx, lockID, opts...)
if err != nil {
return err
}
2022-04-03 04:07:16 +00:00
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, error) {
2022-04-03 04:07:16 +00:00
c := defaultForceUnlockOptions
for _, o := range opts {
o.configureForceUnlock(&c)
}
args := []string{"force-unlock", "-no-color", "-force"}
2022-04-03 04:07:16 +00:00
// positional arguments
args = append(args, lockID)
// optional positional arguments
if c.dir != "" {
err := tf.compatible(ctx, nil, tf0_15_0)
if err != nil {
return nil, fmt.Errorf("[DIR] option was removed in Terraform v0.15.0")
}
2022-04-03 04:07:16 +00:00
args = append(args, c.dir)
}
return tf.buildTerraformCmd(ctx, nil, args...), nil
2022-04-03 04:07:16 +00:00
}