mirror of
https://github.com/coder/coder.git
synced 2025-07-18 14:17:22 +00:00
fix: display the correct response for coder list (#16547)
Closes https://github.com/coder/coder/issues/16312 We intend to modify the behavior of the CLI handler based on the specified output format. However, the output format is currently only accessible within the `OutputFormatter` structure. Therefore, I propose extending `OutputFormatter` by introducing a public `FormatID` method, which will allow us to retrieve the format identifier and use it to customize the behavior of the CLI handler accordingly.
This commit is contained in:
committed by
GitHub
parent
d52d2397ea
commit
981cf8c333
@ -83,6 +83,12 @@ 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
|
||||
}
|
||||
|
||||
type tableFormat struct {
|
||||
defaultColumns []string
|
||||
allColumns []string
|
||||
|
@ -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 <name>"))
|
||||
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user