Skip to content

Commit 43ac0f0

Browse files
fix: display the correct response for coder list
1 parent 3590102 commit 43ac0f0

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

cli/cliui/output.go

Lines changed: 4 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7+
"fmt"
78
"testing"
89

910
"github.com/stretchr/testify/assert"
@@ -74,4 +75,33 @@ func TestList(t *testing.T) {
7475
require.NoError(t, json.Unmarshal(out.Bytes(), &workspaces))
7576
require.Len(t, workspaces, 1)
7677
})
78+
79+
t.Run("NoWorkspacesJSON", func(t *testing.T) {
80+
t.Parallel()
81+
client := coderdtest.New(t, nil)
82+
owner := coderdtest.CreateFirstUser(t, client)
83+
member, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
84+
85+
inv, root := clitest.New(t, "list", "--output=json")
86+
clitest.SetupConfig(t, member, root)
87+
88+
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitLong)
89+
defer cancelFunc()
90+
91+
stdout := bytes.NewBuffer(nil)
92+
stderr := bytes.NewBuffer(nil)
93+
inv.Stdout = stdout
94+
inv.Stderr = stderr
95+
err := inv.WithContext(ctx).Run()
96+
require.NoError(t, err)
97+
98+
fmt.Printf("stdout.Bytes: %s\n", stdout.Bytes())
99+
fmt.Printf("stderr.Bytes: %s\n", stderr.Bytes())
100+
101+
var workspaces []codersdk.Workspace
102+
require.NoError(t, json.Unmarshal(stdout.Bytes(), &workspaces))
103+
require.Len(t, workspaces, 0)
104+
105+
require.Len(t, stderr.Bytes(), 0)
106+
})
77107
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy