Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the workspace show command #245

Merged
merged 2 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion tfexec/internal/e2etest/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const defaultWorkspace = "default"
func TestWorkspace_default_only(t *testing.T) {
runTest(t, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
assertWorkspaceList(t, tf, defaultWorkspace)
assertWorkspaceShow(t, tf, defaultWorkspace)

t.Run("select default when already on default", func(t *testing.T) {
err := tf.WorkspaceSelect(context.Background(), defaultWorkspace)
Expand All @@ -24,6 +25,7 @@ func TestWorkspace_default_only(t *testing.T) {
}

assertWorkspaceList(t, tf, defaultWorkspace)
assertWorkspaceShow(t, tf, defaultWorkspace)
})

t.Run("create new workspace", func(t *testing.T) {
Expand All @@ -34,6 +36,7 @@ func TestWorkspace_default_only(t *testing.T) {
}

assertWorkspaceList(t, tf, newWorkspace, newWorkspace)
assertWorkspaceShow(t, tf, newWorkspace)
})
})
}
Expand Down Expand Up @@ -62,6 +65,7 @@ func TestWorkspace_already_exists(t *testing.T) {
}

assertWorkspaceList(t, tf, newWorkspace, newWorkspace)
assertWorkspaceShow(t, tf, newWorkspace)
})

t.Run("create existing workspace", func(t *testing.T) {
Expand All @@ -81,6 +85,7 @@ func TestWorkspace_already_exists(t *testing.T) {
func TestWorkspace_multiple(t *testing.T) {
runTest(t, "workspaces", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
assertWorkspaceList(t, tf, "foo", "foo")
assertWorkspaceShow(t, tf, "foo")

const newWorkspace = "new1"

Expand All @@ -91,6 +96,7 @@ func TestWorkspace_multiple(t *testing.T) {
}

assertWorkspaceList(t, tf, newWorkspace, "foo", newWorkspace)
assertWorkspaceShow(t, tf, newWorkspace)
})

t.Run("select non-default workspace", func(t *testing.T) {
Expand All @@ -100,13 +106,15 @@ func TestWorkspace_multiple(t *testing.T) {
}

assertWorkspaceList(t, tf, "foo", "foo", newWorkspace)
assertWorkspaceShow(t, tf, "foo")
})
})
}

func TestWorkspace_deletion(t *testing.T) {
runTest(t, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
assertWorkspaceList(t, tf, "default")
assertWorkspaceList(t, tf, defaultWorkspace)
assertWorkspaceShow(t, tf, defaultWorkspace)

const testWorkspace = "testws"

Expand All @@ -117,18 +125,22 @@ func TestWorkspace_deletion(t *testing.T) {
}

assertWorkspaceList(t, tf, testWorkspace, testWorkspace)
assertWorkspaceShow(t, tf, testWorkspace)

err = tf.WorkspaceSelect(context.Background(), defaultWorkspace)
if err != nil {
t.Fatalf("got error selecting workspace: %s", err)
}

assertWorkspaceShow(t, tf, defaultWorkspace)

err = tf.WorkspaceDelete(context.Background(), testWorkspace)
if err != nil {
t.Fatalf("got error deleting workspace: %s", err)
}

assertWorkspaceList(t, tf, defaultWorkspace)
assertWorkspaceShow(t, tf, defaultWorkspace)
})
})
}
Expand All @@ -146,3 +158,13 @@ func assertWorkspaceList(t *testing.T, tf *tfexec.Terraform, expectedCurrent str
t.Fatalf("expected %#v, got %#v", actualWorkspaces, expectedWorkspaces)
}
}

func assertWorkspaceShow(t *testing.T, tf *tfexec.Terraform, expectedWorkspace string) {
actualWorkspace, err := tf.WorkspaceShow(context.Background())
if err != nil {
t.Fatalf("got error querying workspace show: %s", err)
}
if actualWorkspace != expectedWorkspace {
t.Fatalf("expected %q workspace, got %q workspace", expectedWorkspace, actualWorkspace)
}
}
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 @@ import (

var (
tf0_7_7 = version.Must(version.NewVersion("0.7.7"))
tf0_10_0 = version.Must(version.NewVersion("0.10.0"))
tf0_12_0 = version.Must(version.NewVersion("0.12.0"))
tf0_13_0 = version.Must(version.NewVersion("0.13.0"))
tf0_14_0 = version.Must(version.NewVersion("0.14.0"))
Expand Down
35 changes: 35 additions & 0 deletions tfexec/workspace_show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package tfexec

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

// WorkspaceShow represents the workspace show subcommand to the Terraform CLI.
func (tf *Terraform) WorkspaceShow(ctx context.Context) (string, error) {
workspaceShowCmd, err := tf.workspaceShowCmd(ctx)
if err != nil {
return "", err
}

var outBuffer strings.Builder
workspaceShowCmd.Stdout = &outBuffer

err = tf.runTerraformCmd(ctx, workspaceShowCmd)
if err != nil {
return "", err
}

return strings.TrimSpace(outBuffer.String()), nil
}

func (tf *Terraform) workspaceShowCmd(ctx context.Context) (*exec.Cmd, error) {
err := tf.compatible(ctx, tf0_10_0, nil)
if err != nil {
return nil, fmt.Errorf("workspace show was first introduced in Terraform 0.10.0: %w", err)
}

return tf.buildTerraformCmd(ctx, nil, "workspace", "show", "-no-color"), nil
}
31 changes: 31 additions & 0 deletions tfexec/workspace_show_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package tfexec

import (
"context"
"errors"
"testing"
)

func TestWorkspaceShowCmd(t *testing.T) {
td := t.TempDir()

tf, err := NewTerraform(td, tfVersion(t, "0.9.11"))
if err != nil {
t.Fatal(err)
}

// empty env, to avoid environ mismatch in testing
tf.SetEnv(map[string]string{})

t.Run("too old version", func(t *testing.T) {
_, err := tf.workspaceShowCmd(context.Background())
if err == nil {
t.Fatal("expected old version to fail")
}

var expectedErr *ErrVersionMismatch
if !errors.As(err, &expectedErr) {
t.Fatalf("error doesn't match: %#v", err)
}
})
}