Skip to content
Open
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
17 changes: 17 additions & 0 deletions tfexec/workspace_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package tfexec

import (
"context"
"net/url"
"os/exec"
"strings"
)
Expand Down Expand Up @@ -72,12 +73,28 @@ func parseWorkspaceList(stdout string) ([]string, string) {
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
// results from successive newlines in stdout string; ignore
continue
}

if strings.HasPrefix(line, currentWorkspacePrefix) {
line = strings.TrimPrefix(line, currentWorkspacePrefix)
current = line
}

if line != url.PathEscape(line) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oof, this is precisely why we need JSON output. Kinda regret we even added support for this command here so early. 😂 😭

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW I felt very dirty adding this code 😭 , but I'm working on JSON output!

// If a line isn't URL-escaped then it's not a valid workspace name.
// If we're encountering a processed line string that isn't a valid workspace name it's
// almost certainly due to a warning being sent to stdout after the workspace commands
// were migrated from using cli.Ui to view.View, which causes warnings to begin being sent to
// stdout instead of stderr.
// See: https://github.com/hashicorp/terraform/blob/9fb1177c3566c247c72e6c36ca219463c4b6be2b/internal/command/workspace_command.go#L44-L52
//
// tl;dr this line isn't a workspace name, so lets skip and see if there is a list of
// workspaces later in the output, under any warning text.
continue
}

workspaces = append(workspaces, line)
}

Expand Down
30 changes: 30 additions & 0 deletions tfexec/workspace_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,36 @@ func TestParseWorkspaceList(t *testing.T) {
"foo",
" default\r\n* foo\r\n\r\n",
},
// warning about no workspaces being present is set to stdout and
// should not be parsed as a list.
{
[]string{},
"",
`
Warning: Terraform cannot find any existing workspaces.

The "default" workspace is selected in your working directory. You can create
this workspace by running "terraform init", by using the "terraform workspace
new" subcommand or by including the "-or-create" flag with the "terraform
workspace select" subcommand.

`,
},
// warning present alongside a list of workspaces.
{
[]string{"default", "foo", "bar"},
"foo",
`
Warning: This warning is part of a test!

This warning doesn't exist in Terraform core, but we're seeing if it can be
ignored while parsing the list below:

default
* foo
bar
`,
},
} {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
actualList, actualCurrent := parseWorkspaceList(c.stdout)
Expand Down