Skip to content

Commit

Permalink
add compatibility code for dir arg
Browse files Browse the repository at this point in the history
  • Loading branch information
kmoe committed Aug 19, 2022
1 parent 23c89bc commit e25be3d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
14 changes: 11 additions & 3 deletions tfexec/force_unlock.go
Expand Up @@ -2,6 +2,7 @@ package tfexec

import (
"context"
"fmt"
"os/exec"
)

Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
}
25 changes: 23 additions & 2 deletions tfexec/force_unlock_test.go
Expand Up @@ -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",
Expand All @@ -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)
})
Expand Down

0 comments on commit e25be3d

Please sign in to comment.