Skip to content

Commit

Permalink
compat for flags removed in 0.15
Browse files Browse the repository at this point in the history
  • Loading branch information
kmoe committed Jan 15, 2021
1 parent b0e3f89 commit d6f927e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 16 deletions.
28 changes: 23 additions & 5 deletions tfexec/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ func (tf *Terraform) initCmd(ctx context.Context, opts ...InitOption) (*exec.Cmd
c := defaultInitOptions

for _, o := range opts {
switch o.(type) {
case *LockOption, *LockTimeoutOption, *VerifyPluginsOption, *GetPluginsOption:
err := tf.compatible(ctx, nil, tf0_15_0)
if err != nil {
return nil, fmt.Errorf("-lock, -lock-timeout, -verify-plugins, and -get-plugins options are no longer available as of Terraform 0.15: %w", err)
}
}

o.configureInit(&c)
}

Expand All @@ -114,17 +122,27 @@ func (tf *Terraform) initCmd(ctx context.Context, opts ...InitOption) (*exec.Cmd
if c.fromModule != "" {
args = append(args, "-from-module="+c.fromModule)
}
if c.lockTimeout != "" {
args = append(args, "-lock-timeout="+c.lockTimeout)

// string opts removed in 0.15: pass if set and <0.15
err := tf.compatible(ctx, nil, tf0_15_0)
if err == nil {
if c.lockTimeout != "" {
args = append(args, "-lock-timeout="+c.lockTimeout)
}
}

// boolean opts: always pass
args = append(args, "-backend="+fmt.Sprint(c.backend))
args = append(args, "-get="+fmt.Sprint(c.get))
args = append(args, "-get-plugins="+fmt.Sprint(c.getPlugins))
args = append(args, "-lock="+fmt.Sprint(c.lock))
args = append(args, "-upgrade="+fmt.Sprint(c.upgrade))
args = append(args, "-verify-plugins="+fmt.Sprint(c.verifyPlugins))

// boolean opts removed in 0.15: pass if <0.15
err = tf.compatible(ctx, nil, tf0_15_0)
if err == nil {
args = append(args, "-lock="+fmt.Sprint(c.lock))
args = append(args, "-get-plugins="+fmt.Sprint(c.getPlugins))
args = append(args, "-verify-plugins="+fmt.Sprint(c.verifyPlugins))
}

// unary flags: pass if true
if c.reconfigure {
Expand Down
8 changes: 4 additions & 4 deletions tfexec/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ func TestInitCmd(t *testing.T) {
"-lock-timeout=0s",
"-backend=true",
"-get=true",
"-get-plugins=true",
"-lock=true",
"-upgrade=false",
"-lock=true",
"-get-plugins=true",
"-verify-plugins=true",
}, nil, initCmd)
})
Expand All @@ -55,9 +55,9 @@ func TestInitCmd(t *testing.T) {
"-lock-timeout=999s",
"-backend=false",
"-get=false",
"-get-plugins=false",
"-lock=false",
"-upgrade=true",
"-lock=false",
"-get-plugins=false",
"-verify-plugins=false",
"-reconfigure",
"-backend-config=confpath1",
Expand Down
4 changes: 2 additions & 2 deletions tfexec/internal/e2etest/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ func TestImport(t *testing.T) {
runTest(t, "import", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
ctx := context.Background()

err := tf.Init(ctx, tfexec.Lock(false))
err := tf.Init(ctx)
if err != nil {
t.Fatal(err)
}

// Config is unnecessary here since its already the working dir, but just testing an additional flag
err = tf.Import(ctx, resourceAddress, expectedID, tfexec.DisableBackup(), tfexec.Lock(false), tfexec.Config(tf.WorkingDir()))
err = tf.Import(ctx, resourceAddress, expectedID, tfexec.DisableBackup(), tfexec.Config(tf.WorkingDir()))
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfexec/internal/e2etest/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestVersion(t *testing.T) {
runTest(t, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
ctx := context.Background()

err := tf.Init(ctx, tfexec.Lock(false))
err := tf.Init(ctx)
if err != nil {
t.Fatal(err)
}
Expand Down
8 changes: 4 additions & 4 deletions tfexec/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ func TestCheckpointDisablePropagation(t *testing.T) {
"-lock-timeout=0s",
"-backend=true",
"-get=true",
"-get-plugins=true",
"-lock=true",
"-upgrade=false",
"-lock=true",
"-get-plugins=true",
"-verify-plugins=true",
}, map[string]string{
"CHECKPOINT_DISABLE": "1",
Expand Down Expand Up @@ -129,9 +129,9 @@ func TestCheckpointDisablePropagation(t *testing.T) {
"-lock-timeout=0s",
"-backend=true",
"-get=true",
"-get-plugins=true",
"-lock=true",
"-upgrade=false",
"-lock=true",
"-get-plugins=true",
"-verify-plugins=true",
}, map[string]string{
"CHECKPOINT_DISABLE": "",
Expand Down
1 change: 1 addition & 0 deletions tfexec/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var (
tf0_7_7 = version.Must(version.NewVersion("0.7.7"))
tf0_12_0 = version.Must(version.NewVersion("0.12.0"))
tf0_13_0 = version.Must(version.NewVersion("0.13.0"))
tf0_15_0 = version.Must(version.NewVersion("0.15.0"))
)

// Version returns structured output from the terraform version command including both the Terraform CLI version
Expand Down

0 comments on commit d6f927e

Please sign in to comment.