Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2a5a34b

Browse files
fix: display the correct response for coder list
1 parent 3590102 commit 2a5a34b

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

cli/cliui/output.go

+4
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ func (f *OutputFormatter) Format(ctx context.Context, data any) (string, error)
8383
return "", xerrors.Errorf("unknown output format %q", f.formatID)
8484
}
8585

86+
func (f *OutputFormatter) IsMachineReadableFormat() bool {
87+
return f.formatID == "json"
88+
}
89+
8690
type tableFormat struct {
8791
defaultColumns []string
8892
allColumns []string

cli/list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (r *RootCmd) list() *serpent.Command {
112112
return err
113113
}
114114

115-
if len(res) == 0 {
115+
if len(res) == 0 && !formatter.IsMachineReadableFormat() {
116116
pretty.Fprintf(inv.Stderr, cliui.DefaultStyles.Prompt, "No workspaces found! Create one:\n")
117117
_, _ = fmt.Fprintln(inv.Stderr)
118118
_, _ = fmt.Fprintln(inv.Stderr, " "+pretty.Sprint(cliui.DefaultStyles.Code, "coder create <name>"))

cli/list_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,30 @@ func TestList(t *testing.T) {
7474
require.NoError(t, json.Unmarshal(out.Bytes(), &workspaces))
7575
require.Len(t, workspaces, 1)
7676
})
77+
78+
t.Run("NoWorkspacesJSON", func(t *testing.T) {
79+
t.Parallel()
80+
client := coderdtest.New(t, nil)
81+
owner := coderdtest.CreateFirstUser(t, client)
82+
member, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
83+
84+
inv, root := clitest.New(t, "list", "--output=json")
85+
clitest.SetupConfig(t, member, root)
86+
87+
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitLong)
88+
defer cancelFunc()
89+
90+
stdout := bytes.NewBuffer(nil)
91+
stderr := bytes.NewBuffer(nil)
92+
inv.Stdout = stdout
93+
inv.Stderr = stderr
94+
err := inv.WithContext(ctx).Run()
95+
require.NoError(t, err)
96+
97+
var workspaces []codersdk.Workspace
98+
require.NoError(t, json.Unmarshal(stdout.Bytes(), &workspaces))
99+
require.Len(t, workspaces, 0)
100+
101+
require.Len(t, stderr.Bytes(), 0)
102+
})
77103
}

0 commit comments

Comments
 (0)