Skip to content

Commit

Permalink
Allow state pull to pass any options
Browse files Browse the repository at this point in the history
At time of writing, there is no valid option in Terraform v0.12/v0.13.
It's a room for future extensions not to break the interface.
  • Loading branch information
minamijoyo committed Jul 21, 2020
1 parent f3e6414 commit dd5cc02
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion tfexec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type TerraformCLI interface {
StateList(ctx context.Context, state *State, addresses []string, opts ...string) ([]string, error)

// StatePull returns the current tfstate from remote.
StatePull(ctx context.Context) (*State, error)
StatePull(ctx context.Context, opts ...string) (*State, error)

// StatePush pushs a given State to remote.
StatePush(ctx context.Context, state *State) error
Expand Down
9 changes: 7 additions & 2 deletions tfexec/terraform_state_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ package tfexec
import "context"

// StatePull returns the current tfstate from remote.
func (c *terraformCLI) StatePull(ctx context.Context) (*State, error) {
stdout, _, err := c.Run(ctx, "state", "pull")
func (c *terraformCLI) StatePull(ctx context.Context, opts ...string) (*State, error) {
args := []string{"state", "pull"}
// At time of writing, there is no valid option in Terraform v0.12/v0.13.
// It's a room for future extensions not to break the interface.
args = append(args, opts...)

stdout, _, err := c.Run(ctx, args...)
if err != nil {
return nil, err
}
Expand Down
16 changes: 15 additions & 1 deletion tfexec/terraform_state_pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestTerraformCLIStatePull(t *testing.T) {
cases := []struct {
desc string
mockCommands []*mockCommand
opts []string
want *State
ok bool
}{
Expand All @@ -45,13 +46,26 @@ func TestTerraformCLIStatePull(t *testing.T) {
want: nil,
ok: false,
},
{
desc: "with opts", // there is no valid option for now, just pass a dummy for testing.
mockCommands: []*mockCommand{
{
args: []string{"terraform", "state", "pull", "-foo"},
stdout: stdout,
exitCode: 0,
},
},
opts: []string{"-foo"},
want: NewState([]byte(stdout)),
ok: true,
},
}

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
e := NewMockExecutor(tc.mockCommands)
terraformCLI := NewTerraformCLI(e)
got, err := terraformCLI.StatePull(context.Background())
got, err := terraformCLI.StatePull(context.Background(), tc.opts...)
if tc.ok && err != nil {
t.Fatalf("unexpected err: %s", err)
}
Expand Down

0 comments on commit dd5cc02

Please sign in to comment.