diff --git a/tfexec/force_unlock.go b/tfexec/force_unlock.go index e501baf5..de95f547 100644 --- a/tfexec/force_unlock.go +++ b/tfexec/force_unlock.go @@ -2,6 +2,7 @@ package tfexec import ( "context" + "fmt" "os/exec" ) @@ -21,7 +22,10 @@ func (opt *DirOption) configureForceUnlock(conf *forceUnlockConfig) { // 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...) + unlockCmd, err := tf.forceUnlockCmd(ctx, lockID, opts...) + if err != nil { + return err + } if err := tf.runTerraformCmd(ctx, unlockCmd); err != nil { return err @@ -30,7 +34,7 @@ func (tf *Terraform) ForceUnlock(ctx context.Context, lockID string, opts ...For return nil } -func (tf *Terraform) forceUnlockCmd(ctx context.Context, lockID string, opts ...ForceUnlockOption) *exec.Cmd { +func (tf *Terraform) forceUnlockCmd(ctx context.Context, lockID string, opts ...ForceUnlockOption) (*exec.Cmd, error) { c := defaultForceUnlockOptions for _, o := range opts { @@ -43,8 +47,12 @@ func (tf *Terraform) forceUnlockCmd(ctx context.Context, lockID string, opts ... // 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") + } args = append(args, c.dir) } - return tf.buildTerraformCmd(ctx, nil, args...) + return tf.buildTerraformCmd(ctx, nil, args...), nil } diff --git a/tfexec/force_unlock_test.go b/tfexec/force_unlock_test.go index b5dc1778..b817979b 100644 --- a/tfexec/force_unlock_test.go +++ b/tfexec/force_unlock_test.go @@ -19,7 +19,10 @@ func TestForceUnlockCmd(t *testing.T) { tf.SetEnv(map[string]string{}) t.Run("defaults", func(t *testing.T) { - forceUnlockCmd := tf.forceUnlockCmd(context.Background(), "12345") + forceUnlockCmd, err := tf.forceUnlockCmd(context.Background(), "12345") + if err != nil { + t.Fatal(err) + } assertCmd(t, []string{ "force-unlock", @@ -28,14 +31,32 @@ func TestForceUnlockCmd(t *testing.T) { "12345", }, nil, forceUnlockCmd) }) +} + +// The optional final positional [DIR] argument is available +// until v0.15.0. +func TestForceUnlockCmd_pre015(t *testing.T) { + td := t.TempDir() + + tf, err := NewTerraform(td, tfVersion(t, testutil.Latest014)) + if err != nil { + t.Fatal(err) + } + + // empty env, to avoid environ mismatch in testing + tf.SetEnv(map[string]string{}) t.Run("override all defaults", func(t *testing.T) { - forceUnlockCmd := tf.forceUnlockCmd(context.Background(), "12345", Dir("mydir")) + forceUnlockCmd, err := tf.forceUnlockCmd(context.Background(), "12345", Dir("mydir")) + if err != nil { + t.Fatal(err) + } assertCmd(t, []string{ "force-unlock", "-no-color", "-force", + "12345", "mydir", }, nil, forceUnlockCmd) })