Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit981cf8c

Browse files
fix: display the correct response for coder list (#16547)
Closes#16312We intend to modify the behavior of the CLI handler based on thespecified output format. However, the output format is currently onlyaccessible within the `OutputFormatter` structure. Therefore, I proposeextending `OutputFormatter` by introducing a public `FormatID` method,which will allow us to retrieve the format identifier and use it tocustomize the behavior of the CLI handler accordingly.
1 parentd52d239 commit981cf8c

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

‎cli/cliui/output.go

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

86+
// FormatID will return the ID of the format selected by `--output`.
87+
// If no flag is present, it returns the 'default' formatter.
88+
func (f*OutputFormatter)FormatID()string {
89+
returnf.formatID
90+
}
91+
8692
typetableFormatstruct {
8793
defaultColumns []string
8894
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
returnerr
113113
}
114114

115-
iflen(res)==0 {
115+
iflen(res)==0&&formatter.FormatID()!=cliui.JSONFormat().ID(){
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: 26 additions & 0 deletions
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+
defercancelFunc()
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+
varworkspaces []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)

[8]ページ先頭

©2009-2025 Movatter.jp