Don't error on list commands when no results found (#5479) · cli/cli@13342cb · GitHub
Skip to content

Commit 13342cb

Browse files
rpadakimislav
andauthored
Don't error on list commands when no results found (#5479)
Co-authored-by: Mislav Marohnić <mislav@github.com>
1 parent adc9abd commit 13342cb

33 files changed

Lines changed: 178 additions & 201 deletions

File tree

‎cmd/gh/main.go‎

Lines changed: 7 additions & 0 deletions

‎pkg/cmd/alias/list/list.go‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ func listRun(opts *ListOptions) error {
5454
}
5555

5656
if aliasCfg.Empty() {
57-
if opts.IO.IsStdoutTTY() {
58-
fmt.Fprintf(opts.IO.ErrOut, "no aliases configured\n")
59-
}
60-
return nil
57+
return cmdutil.NewNoResultsError("no aliases configured")
6158
}
6259

6360
tp := utils.NewTablePrinter(opts.IO)

‎pkg/cmd/alias/list/list_test.go‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ func TestAliasList(t *testing.T) {
1818
name string
1919
config string
2020
isTTY bool
21+
wantErr bool
2122
wantStdout string
2223
wantStderr string
2324
}{
2425
{
2526
name: "empty",
2627
config: "",
2728
isTTY: true,
29+
wantErr: true,
2830
wantStdout: "",
29-
wantStderr: "no aliases configured\n",
31+
wantStderr: "",
3032
},
3133
{
3234
name: "some",
@@ -68,7 +70,11 @@ func TestAliasList(t *testing.T) {
6870
cmd.SetErr(ioutil.Discard)
6971

7072
_, err := cmd.ExecuteC()
71-
require.NoError(t, err)
73+
if tt.wantErr {
74+
require.Error(t, err)
75+
} else {
76+
require.NoError(t, err)
77+
}
7278

7379
assert.Equal(t, tt.wantStdout, stdout.String())
7480
assert.Equal(t, tt.wantStderr, stderr.String())

‎pkg/cmd/codespace/list.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ func (a *App) List(ctx context.Context, limit int, exporter cmdutil.Exporter) er
6060
return exporter.Write(a.io, codespaces)
6161
}
6262

63+
if len(codespaces) == 0 {
64+
return cmdutil.NewNoResultsError("no codespaces found")
65+
}
66+
6367
tp := utils.NewTablePrinter(a.io)
6468
if tp.IsTTY() {
6569
tp.AddField("NAME", nil, nil)

‎pkg/cmd/extension/command.go‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ func NewCmdExtension(f *cmdutil.Factory) *cobra.Command {
4747
RunE: func(cmd *cobra.Command, args []string) error {
4848
cmds := m.List()
4949
if len(cmds) == 0 {
50-
cs := io.ColorScheme()
51-
fmt.Fprintf(io.ErrOut, "%s No installed extensions found\n", cs.WarningIcon())
52-
return nil
50+
return cmdutil.NewNoResultsError("no installed extensions found")
5351
}
5452
cs := io.ColorScheme()
5553
t := utils.NewTablePrinter(io)
@@ -175,8 +173,7 @@ func NewCmdExtension(f *cmdutil.Factory) *cobra.Command {
175173
if name != "" {
176174
fmt.Fprintf(io.ErrOut, "%s Failed upgrading extension %s: %s\n", cs.FailureIcon(), name, err)
177175
} else if errors.Is(err, noExtensionsInstalledError) {
178-
fmt.Fprintf(io.ErrOut, "%s No installed extensions found\n", cs.WarningIcon())
179-
return nil
176+
return cmdutil.NewNoResultsError("no installed extensions found")
180177
} else {
181178
fmt.Fprintf(io.ErrOut, "%s Failed upgrading extensions\n", cs.FailureIcon())
182179
}

‎pkg/cmd/extension/command_test.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,9 @@ func TestNewCmdExtension(t *testing.T) {
273273
assert.Equal(t, "", calls[0].Name)
274274
}
275275
},
276-
isTTY: true,
277-
wantStderr: "! No installed extensions found\n",
276+
isTTY: true,
277+
wantErr: true,
278+
errMsg: "no installed extensions found",
278279
},
279280
{
280281
name: "upgrade all notty",

‎pkg/cmd/gist/list/list.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ func listRun(opts *ListOptions) error {
8686
return err
8787
}
8888

89+
if len(gists) == 0 {
90+
return cmdutil.NewNoResultsError("no gists found")
91+
}
92+
8993
if err := opts.IO.StartPager(); err == nil {
9094
defer opts.IO.StopPager()
9195
} else {

‎pkg/cmd/gist/list/list_test.go‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func Test_listRun(t *testing.T) {
106106
tests := []struct {
107107
name string
108108
opts *ListOptions
109+
wantErr bool
109110
wantOut string
110111
stubs func(*httpmock.Registry)
111112
nontty bool
@@ -120,7 +121,7 @@ func Test_listRun(t *testing.T) {
120121
"gists": { "nodes": [] }
121122
} } }`))
122123
},
123-
wantOut: "",
124+
wantErr: true,
124125
},
125126
{
126127
name: "default behavior",
@@ -377,7 +378,11 @@ func Test_listRun(t *testing.T) {
377378
}
378379
t.Run(tt.name, func(t *testing.T) {
379380
err := listRun(tt.opts)
380-
assert.NoError(t, err)
381+
if tt.wantErr {
382+
assert.Error(t, err)
383+
} else {
384+
assert.NoError(t, err)
385+
}
381386

382387
assert.Equal(t, tt.wantOut, stdout.String())
383388
reg.Verify(t)

‎pkg/cmd/gpg-key/list/list.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ func listRun(opts *ListOptions) error {
7070
}
7171

7272
if len(gpgKeys) == 0 {
73-
fmt.Fprintln(opts.IO.ErrOut, "No GPG keys present in GitHub account.")
74-
return cmdutil.SilentError
73+
return cmdutil.NewNoResultsError("no GPG keys present in the GitHub account")
7574
}
7675

7776
t := utils.NewTablePrinter(opts.IO)

‎pkg/cmd/gpg-key/list/list_test.go‎

Lines changed: 2 additions & 1 deletion

0 commit comments

Comments
 (0)