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 1 commit
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)
}
}
21 changes: 21 additions & 0 deletions tfexec/workspace_show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tfexec

import (
"context"
"strings"
)

// WorkspaceShow represents the workspace show subcommand to the Terraform CLI.
func (tf *Terraform) WorkspaceShow(ctx context.Context) (string, error) {
workspaceShowCmd := tf.buildTerraformCmd(ctx, nil, "workspace", "show", "-no-color")

var outBuffer strings.Builder
workspaceShowCmd.Stdout = &outBuffer

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

return strings.TrimSpace(outBuffer.String()), nil
}
lafentres marked this conversation as resolved.
Show resolved Hide resolved