forked from hashicorp/terraform-exec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkspace_list.go
47 lines (37 loc) · 1 KB
/
workspace_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package tfexec
import (
"bytes"
"context"
"strings"
)
// WorkspaceList represents the workspace list subcommand to the Terraform CLI.
func (tf *Terraform) WorkspaceList(ctx context.Context) ([]string, string, error) {
// TODO: [DIR] param option
wlCmd := tf.buildTerraformCmd(ctx, nil, "workspace", "list", "-no-color")
var outBuf bytes.Buffer
wlCmd.Stdout = &outBuf
err := tf.runTerraformCmd(ctx, wlCmd)
if err != nil {
return nil, "", err
}
ws, current := parseWorkspaceList(outBuf.String())
return ws, current, nil
}
const currentWorkspacePrefix = "* "
func parseWorkspaceList(stdout string) ([]string, string) {
lines := strings.Split(stdout, "\n")
current := ""
workspaces := []string{}
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
if strings.HasPrefix(line, currentWorkspacePrefix) {
line = strings.TrimPrefix(line, currentWorkspacePrefix)
current = line
}
workspaces = append(workspaces, line)
}
return workspaces, current
}