Skip to content

Commit

Permalink
Remove a positional dir parameter from TerraformCLI.Init()
Browse files Browse the repository at this point in the history
The positional `dir` parameter is no longer available in Terraform v0.15.0.
https://github.com/hashicorp/terraform/blob/v0.15/CHANGELOG.md#0150-april-14-2021

It was replaced by `-chdir` flag, but the current implementation actually
doesn't seem to use it. So we can remove it.
  • Loading branch information
minamijoyo committed Oct 21, 2021
1 parent acfd20f commit 8b9521b
Show file tree
Hide file tree
Showing 17 changed files with 25 additions and 52 deletions.
8 changes: 4 additions & 4 deletions tfexec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ type TerraformCLI interface {
// Verison returns a version number of Terraform.
Version(ctx context.Context) (string, error)

// Init initializes a given work directory.
Init(ctx context.Context, dir string, opts ...string) error
// Init initializes the current work directory.
Init(ctx context.Context, opts ...string) error

// Plan computes expected changes.
// If a state is given, use it for the input state.
Expand Down Expand Up @@ -221,7 +221,7 @@ terraform {
}

log.Printf("[INFO] [executor@%s] switch backend to local\n", c.Dir())
err := c.Init(ctx, "", "-input=false", "-no-color", "-reconfigure")
err := c.Init(ctx, "-input=false", "-no-color", "-reconfigure")
if err != nil {
// remove the override file before return an error.
os.Remove(path)
Expand Down Expand Up @@ -253,7 +253,7 @@ terraform {
log.Printf("[ERROR] [executor@%s] please remove the local workspace directory(%s) and re-run terraform init -reconfigure\n", c.Dir(), workspacePath)
}
log.Printf("[INFO] [executor@%s] switch back to remote\n", c.Dir())
err = c.Init(ctx, "", "-input=false", "-no-color", "-reconfigure")
err = c.Init(ctx, "-input=false", "-no-color", "-reconfigure")
if err != nil {
// we cannot return error here.
log.Printf("[ERROR] [executor@%s] failed to switch back to remote: %s\n", c.Dir(), err)
Expand Down
2 changes: 1 addition & 1 deletion tfexec/terraform_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestAccTerraformCLIApply(t *testing.T) {
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "", "-input=false", "-no-color")
err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfexec/terraform_destroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestAccTerraformCLIDestroy(t *testing.T) {
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "", "-input=false", "-no-color")
err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfexec/terraform_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func TestAccTerraformCLIImport(t *testing.T) {
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "", "-input=false", "-no-color")
err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
7 changes: 2 additions & 5 deletions tfexec/terraform_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package tfexec

import "context"

// Init initializes a given work directory.
func (c *terraformCLI) Init(ctx context.Context, dir string, opts ...string) error {
// Init initializes the current work directory.
func (c *terraformCLI) Init(ctx context.Context, opts ...string) error {
args := []string{"init"}
args = append(args, opts...)
if len(dir) > 0 {
args = append(args, dir)
}
_, _, err := c.Run(ctx, args...)
return err
}
30 changes: 3 additions & 27 deletions tfexec/terraform_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ func TestTerraformCLIInit(t *testing.T) {
cases := []struct {
desc string
mockCommands []*mockCommand
dir string
opts []string
ok bool
}{
{
desc: "no dir and no opts",
desc: "no opts",
mockCommands: []*mockCommand{
{
args: []string{"terraform", "init"},
Expand All @@ -35,17 +34,6 @@ func TestTerraformCLIInit(t *testing.T) {
},
ok: false,
},
{
desc: "with dir",
mockCommands: []*mockCommand{
{
args: []string{"terraform", "init", "foo"},
exitCode: 0,
},
},
dir: "foo",
ok: true,
},
{
desc: "with opts",
mockCommands: []*mockCommand{
Expand All @@ -57,25 +45,13 @@ func TestTerraformCLIInit(t *testing.T) {
opts: []string{"-input=false", "-no-color"},
ok: true,
},
{
desc: "with dir and opts",
mockCommands: []*mockCommand{
{
args: []string{"terraform", "init", "-input=false", "-no-color", "foo"},
exitCode: 0,
},
},
dir: "foo",
opts: []string{"-input=false", "-no-color"},
ok: true,
},
}

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
e := NewMockExecutor(tc.mockCommands)
terraformCLI := NewTerraformCLI(e)
err := terraformCLI.Init(context.Background(), tc.dir, tc.opts...)
err := terraformCLI.Init(context.Background(), tc.opts...)
if tc.ok && err != nil {
t.Fatalf("unexpected err: %s", err)
}
Expand All @@ -93,7 +69,7 @@ func TestAccTerraformCLIInit(t *testing.T) {
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "", "-input=false", "-no-color")
err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions tfexec/terraform_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func TestAccTerraformCLIPlan(t *testing.T) {
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "", "-input=false", "-no-color")
err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand All @@ -190,7 +190,7 @@ func TestAccTerraformCLIPlanWithOut(t *testing.T) {
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "", "-input=false", "-no-color")
err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfexec/terraform_state_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ resource "null_resource" "bar" {}
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "", "-input=false", "-no-color")
err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions tfexec/terraform_state_mv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ resource "null_resource" "bar" {}
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "", "-input=false", "-no-color")
err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down Expand Up @@ -283,7 +283,7 @@ resource "null_resource" "bar" {}
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "", "-input=false", "-no-color")
err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfexec/terraform_state_pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestAccTerraformCLIStatePull(t *testing.T) {
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "", "-input=false", "-no-color")
err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfexec/terraform_state_push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestAccTerraformCLIStatePush(t *testing.T) {
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "", "-input=false", "-no-color")
err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfexec/terraform_state_rm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ resource "null_resource" "bar" {}
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "", "-input=false", "-no-color")
err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfexec/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func TestAccTerraformCLIPlanHasChange(t *testing.T) {
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "", "-input=false", "-no-color")
err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfexec/terraform_workspace_new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestAccTerraformCLIWorkspaceNew(t *testing.T) {
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "", "-input=false", "-no-color")
err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfexec/terraform_workspace_select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestAccTerraformCLIWorkspaceSelect(t *testing.T) {
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "", "-input=false", "-no-color")
err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfexec/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func SetupTestAccWithApply(t *testing.T, workspace string, source string) Terraf
tf := NewTerraformCLI(e)
ctx := context.Background()

err := tf.Init(ctx, "", "-input=false", "-no-color")
err := tf.Init(ctx, "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfmigrate/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func setupWorkDir(ctx context.Context, tf tfexec.TerraformCLI, workspace string)

// init folder
log.Printf("[INFO] [migrator@%s] initialize work dir\n", tf.Dir())
err = tf.Init(ctx, "", "-input=false", "-no-color")
err = tf.Init(ctx, "-input=false", "-no-color")
if err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit 8b9521b

Please sign in to comment.