From a12f553130a56ec2add6f7b7786b38a642844770 Mon Sep 17 00:00:00 2001 From: evgeniy-scherbina Date: Wed, 12 Feb 2025 16:53:10 -0500 Subject: [PATCH 1/2] fix: display the correct response for coder list --- cli/cliui/output.go | 4 ++++ cli/list.go | 2 +- cli/list_test.go | 26 ++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/cli/cliui/output.go b/cli/cliui/output.go index b875e19d154c3..6d657caec3af3 100644 --- a/cli/cliui/output.go +++ b/cli/cliui/output.go @@ -83,6 +83,10 @@ func (f *OutputFormatter) Format(ctx context.Context, data any) (string, error) return "", xerrors.Errorf("unknown output format %q", f.formatID) } +func (f *OutputFormatter) FormatID() string { + return f.formatID +} + type tableFormat struct { defaultColumns []string allColumns []string diff --git a/cli/list.go b/cli/list.go index 1a578c887371b..083d32c6e8fa1 100644 --- a/cli/list.go +++ b/cli/list.go @@ -112,7 +112,7 @@ func (r *RootCmd) list() *serpent.Command { return err } - if len(res) == 0 { + if len(res) == 0 && formatter.FormatID() != cliui.JSONFormat().ID() { pretty.Fprintf(inv.Stderr, cliui.DefaultStyles.Prompt, "No workspaces found! Create one:\n") _, _ = fmt.Fprintln(inv.Stderr) _, _ = fmt.Fprintln(inv.Stderr, " "+pretty.Sprint(cliui.DefaultStyles.Code, "coder create ")) diff --git a/cli/list_test.go b/cli/list_test.go index 37f2f36f79278..a70c70babf437 100644 --- a/cli/list_test.go +++ b/cli/list_test.go @@ -74,4 +74,30 @@ func TestList(t *testing.T) { require.NoError(t, json.Unmarshal(out.Bytes(), &workspaces)) require.Len(t, workspaces, 1) }) + + t.Run("NoWorkspacesJSON", func(t *testing.T) { + t.Parallel() + client := coderdtest.New(t, nil) + owner := coderdtest.CreateFirstUser(t, client) + member, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID) + + inv, root := clitest.New(t, "list", "--output=json") + clitest.SetupConfig(t, member, root) + + ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitLong) + defer cancelFunc() + + stdout := bytes.NewBuffer(nil) + stderr := bytes.NewBuffer(nil) + inv.Stdout = stdout + inv.Stderr = stderr + err := inv.WithContext(ctx).Run() + require.NoError(t, err) + + var workspaces []codersdk.Workspace + require.NoError(t, json.Unmarshal(stdout.Bytes(), &workspaces)) + require.Len(t, workspaces, 0) + + require.Len(t, stderr.Bytes(), 0) + }) } From 137c97768d0b5a94004d43d7d20acda836690333 Mon Sep 17 00:00:00 2001 From: evgeniy-scherbina Date: Thu, 13 Feb 2025 09:55:09 -0500 Subject: [PATCH 2/2] docs: add documentation for public method --- cli/cliui/output.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cli/cliui/output.go b/cli/cliui/output.go index 6d657caec3af3..65f6171c2c962 100644 --- a/cli/cliui/output.go +++ b/cli/cliui/output.go @@ -83,6 +83,8 @@ func (f *OutputFormatter) Format(ctx context.Context, data any) (string, error) return "", xerrors.Errorf("unknown output format %q", f.formatID) } +// FormatID will return the ID of the format selected by `--output`. +// If no flag is present, it returns the 'default' formatter. func (f *OutputFormatter) FormatID() string { return f.formatID } 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