X Tutup
Skip to content

Commit 0bfd821

Browse files
Merge pull request cli#144 from github/better-better-better
Better error messages when you don't supply an arg
2 parents 0f970e6 + 2979c5f commit 0bfd821

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

api/queries_pr.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ type PullRequest struct {
4949
}
5050
}
5151

52+
type NotFoundError struct {
53+
error
54+
}
55+
5256
func (pr PullRequest) HeadLabel() string {
5357
if pr.IsCrossRepository {
5458
return fmt.Sprintf("%s:%s", pr.HeadRepositoryOwner.Login, pr.HeadRefName)
@@ -368,7 +372,7 @@ func PullRequestForBranch(client *Client, ghRepo Repo, branch string) (*PullRequ
368372
}
369373
}
370374

371-
return nil, fmt.Errorf("no open pull requests found for branch %q", branch)
375+
return nil, &NotFoundError{fmt.Errorf("no open pull requests found for branch %q", branch)}
372376
}
373377

374378
func CreatePullRequest(client *Client, ghRepo Repo, params map[string]interface{}) (*PullRequest, error) {

command/issue.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,13 @@ var issueStatusCmd = &cobra.Command{
6060
RunE: issueStatus,
6161
}
6262
var issueViewCmd = &cobra.Command{
63-
Use: "view {<number> | <url> | <branch>}",
64-
Args: cobra.MinimumNArgs(1),
63+
Use: "view {<number> | <url> | <branch>}",
64+
Args: func(cmd *cobra.Command, args []string) error {
65+
if len(args) < 1 {
66+
return errors.New("requires an issue number as an argument")
67+
}
68+
return nil
69+
},
6570
Short: "View an issue in the browser",
6671
RunE: issueView,
6772
}
@@ -108,8 +113,8 @@ func issueList(cmd *cobra.Command, args []string) error {
108113
msg := "There are no open issues"
109114

110115
userSetFlags := false
111-
cmd.Flags().VisitAll(func(f *pflag.Flag) {
112-
userSetFlags = f.Changed || userSetFlags
116+
cmd.Flags().Visit(func(f *pflag.Flag) {
117+
userSetFlags = true
113118
})
114119
if userSetFlags {
115120
msg = "No issues match your search"

command/pr.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package command
22

33
import (
4+
"errors"
45
"fmt"
56
"io"
67
"os"
@@ -45,8 +46,13 @@ A pull request can be supplied as argument in any of the following formats:
4546
var prCheckoutCmd = &cobra.Command{
4647
Use: "checkout {<number> | <url> | <branch>}",
4748
Short: "Check out a pull request in Git",
48-
Args: cobra.MinimumNArgs(1),
49-
RunE: prCheckout,
49+
Args: func(cmd *cobra.Command, args []string) error {
50+
if len(args) < 1 {
51+
return errors.New("requires a PR number as an argument")
52+
}
53+
return nil
54+
},
55+
RunE: prCheckout,
5056
}
5157
var prListCmd = &cobra.Command{
5258
Use: "list",
@@ -191,8 +197,8 @@ func prList(cmd *cobra.Command, args []string) error {
191197
msg := "There are no open pull requests"
192198

193199
userSetFlags := false
194-
cmd.Flags().VisitAll(func(f *pflag.Flag) {
195-
userSetFlags = f.Changed || userSetFlags
200+
cmd.Flags().Visit(func(f *pflag.Flag) {
201+
userSetFlags = true
196202
})
197203
if userSetFlags {
198204
msg = "No pull requests match your search"
@@ -263,8 +269,13 @@ func prView(cmd *cobra.Command, args []string) error {
263269
} else {
264270
pr, err := api.PullRequestForBranch(apiClient, baseRepo, branchWithOwner)
265271
if err != nil {
272+
var notFoundErr *api.NotFoundError
273+
if errors.As(err, &notFoundErr) {
274+
return fmt.Errorf("%s. To open a specific pull request use the pull request's number as an argument", err)
275+
}
266276
return err
267277
}
278+
268279
openURL = pr.URL
269280
}
270281
}

command/pr_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func TestPRView_noResultsForBranch(t *testing.T) {
217217
defer restoreCmd()
218218

219219
_, err := RunCommand(prViewCmd, "pr view")
220-
if err == nil || err.Error() != `no open pull requests found for branch "blueberries"` {
220+
if err == nil || err.Error() != `no open pull requests found for branch "blueberries". To open a specific pull request use the pull request's number as an argument` {
221221
t.Errorf("error running command `pr view`: %v", err)
222222
}
223223

0 commit comments

Comments
 (0)
X Tutup