Skip to content

Commit 2c22a37

Browse files
committed
Add command line option workspace list -json to output workspaces in
json format Signed-off-by: S.Cavallo <[email protected]>
1 parent 296acdd commit 2c22a37

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

Diff for: internal/command/workspace_command_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,57 @@ func TestWorkspace_createAndList(t *testing.T) {
108108
if actual != expected {
109109
t.Fatalf("\nexpected: %q\nactual: %q", expected, actual)
110110
}
111+
112+
// Json without env override
113+
114+
// Make sure an override does not exist
115+
defer os.Unsetenv("TF_WORKSPACE")
116+
if _, ok := os.LookupEnv("TF_WORKSPACE"); ok {
117+
t.Fatalf("TF_WORKSPACE must not be set.")
118+
}
119+
120+
args := []string{
121+
"-json",
122+
}
123+
listCmd = &WorkspaceListCommand{}
124+
ui = new(cli.MockUi)
125+
listCmd.Meta = Meta{Ui: ui}
126+
127+
if code := listCmd.Run(args); code != 0 {
128+
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
129+
}
130+
131+
actual = strings.TrimSpace(ui.OutputWriter.String())
132+
expected = "{\"terraform_version\":\"0.13.0-dev\",\"workspaces\":[{\"name\":\"default\",\"selected\":false},{\"name\":\"test_a\",\"selected\":false},{\"name\":\"test_b\",\"selected\":false},{\"name\":\"test_c\",\"selected\":true}],\"is_overridden\":false,\"overridden_note\":\"\"}"
133+
134+
if actual != expected {
135+
t.Fatalf("\nexpected: %q\nactual: %q", expected, actual)
136+
}
137+
138+
// With workspace override
139+
140+
// Set the workspace override
141+
defer os.Unsetenv("TF_WORKSPACE")
142+
os.Setenv("TF_WORKSPACE", "test_a")
143+
144+
args = []string{
145+
"-json",
146+
}
147+
listCmd = &WorkspaceListCommand{}
148+
ui = new(cli.MockUi)
149+
listCmd.Meta = Meta{Ui: ui}
150+
151+
if code := listCmd.Run(args); code != 0 {
152+
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
153+
}
154+
155+
actual = strings.TrimSpace(ui.OutputWriter.String())
156+
expected = "{\"terraform_version\":\"0.13.0-dev\",\"workspaces\":[{\"name\":\"default\",\"selected\":false},{\"name\":\"test_a\",\"selected\":true},{\"name\":\"test_b\",\"selected\":false},{\"name\":\"test_c\",\"selected\":false}],\"is_overridden\":true,\"overridden_note\":\"\\n\\nThe active workspace is being overridden using the TF_WORKSPACE environment\\nvariable.\\n\"}"
157+
158+
if actual != expected {
159+
t.Fatalf("\nexpected: %q\nactual: %q", expected, actual)
160+
}
161+
111162
}
112163

113164
// Create some workspaces and test the show output.

Diff for: internal/command/workspace_list.go

+46-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package command
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"fmt"
7+
tfversion "github.com/hashicorp/terraform/version"
68
"strings"
79

810
"github.com/hashicorp/terraform/internal/tfdiags"
@@ -14,11 +16,25 @@ type WorkspaceListCommand struct {
1416
LegacyName bool
1517
}
1618

19+
type WorkspaceList struct {
20+
Name string `json:"name"`
21+
Selected bool `json:"selected"`
22+
}
23+
24+
type WorkspaceListOutput struct {
25+
TerraformVersion string `json:"terraform_version"`
26+
WorkspaceList []WorkspaceList `json:"workspaces"`
27+
IsOverridden bool `json:"is_overridden"`
28+
OverriddenNote string `json:"overridden_note"`
29+
}
30+
1731
func (c *WorkspaceListCommand) Run(args []string) int {
1832
args = c.Meta.process(args)
1933
envCommandShowWarning(c.Ui, c.LegacyName)
2034

2135
cmdFlags := c.Meta.defaultFlagSet("workspace list")
36+
var jsonOutput bool
37+
cmdFlags.BoolVar(&jsonOutput, "json", false, "produce JSON output")
2238
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
2339
if err := cmdFlags.Parse(args); err != nil {
2440
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
@@ -62,6 +78,31 @@ func (c *WorkspaceListCommand) Run(args []string) int {
6278

6379
env, isOverridden := c.WorkspaceOverridden()
6480

81+
// If json
82+
if jsonOutput == true {
83+
var wsOutput WorkspaceListOutput
84+
wsOutput.TerraformVersion = tfversion.String()
85+
wsOutput.IsOverridden = isOverridden
86+
if isOverridden {
87+
wsOutput.OverriddenNote = envIsOverriddenNote
88+
}
89+
90+
for _, s := range states {
91+
ws := WorkspaceList{Name: s, Selected: s == env}
92+
wsOutput.WorkspaceList = append(wsOutput.WorkspaceList, ws)
93+
}
94+
95+
jsonOutput, err := json.Marshal(wsOutput)
96+
97+
if err != nil {
98+
c.Ui.Error(fmt.Sprintf("Failed to marshal workspace list to json: %s", err))
99+
return 1
100+
}
101+
c.Ui.Output(string(jsonOutput))
102+
return 0
103+
}
104+
105+
// If not json
65106
var out bytes.Buffer
66107
for _, s := range states {
67108
if s == env {
@@ -91,10 +132,14 @@ func (c *WorkspaceListCommand) AutocompleteFlags() complete.Flags {
91132

92133
func (c *WorkspaceListCommand) Help() string {
93134
helpText := `
94-
Usage: terraform [global options] workspace list
135+
Usage: terraform [global options] workspace list [options]
95136
96137
List Terraform workspaces.
97138
139+
Options:
140+
141+
-json If specified, output to a machine-readable form.
142+
98143
`
99144
return strings.TrimSpace(helpText)
100145
}

0 commit comments

Comments
 (0)