simplify err handling and change flags

This commit is contained in:
meiji163 2021-10-12 10:16:14 -07:00
parent 9ea36de269
commit 5d433021bd
3 changed files with 9 additions and 13 deletions

View file

@ -36,7 +36,7 @@ func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Co
Long: `Delete a GitHub repository.
Deletion requires authorization with the "delete_repo" scope.
To authorize, run "gh auth refresh -h github.com -s delete_repo"`,
To authorize, run "gh auth refresh -s delete_repo"`,
Args: cmdutil.ExactArgs(1, "cannot delete: repository argument required"),
RunE: func(cmd *cobra.Command, args []string) error {
opts.RepoArg = args[0]
@ -47,7 +47,7 @@ To authorize, run "gh auth refresh -h github.com -s delete_repo"`,
},
}
cmd.Flags().BoolVar(&opts.Confirmed, "yes", false, "confirm deletion without prompting")
cmd.Flags().BoolVarP(&opts.Confirmed, "confirm", "c", false, "confirm deletion without prompting")
return cmd
}
@ -77,7 +77,7 @@ func deleteRun(opts *DeleteOptions) error {
doPrompt := opts.IO.CanPrompt()
if !opts.Confirmed && !doPrompt {
return errors.New("could not prompt: confirmation with prompt or --yes flag required")
return errors.New("could not prompt: confirmation with prompt or --confirm flag required")
}
if !opts.Confirmed && doPrompt {

View file

@ -27,7 +27,9 @@ func Test_deleteRun(t *testing.T) {
opts: &DeleteOptions{RepoArg: "OWNER/REPO"},
wantStdout: "✓ Deleted repository OWNER/REPO\n",
askStubs: func(q *prompt.AskStubber) {
q.StubOne("NOTOWNER/NOTREPO") // this always passes??
// TODO: survey stubber doesn't have WithValidation support
// so this always passes regardless of prompt input
q.StubOne("OWNER/REPO")
},
httpStubs: func(reg *httpmock.Registry) {
reg.Register(
@ -51,7 +53,7 @@ func Test_deleteRun(t *testing.T) {
name: "no confirmation no tty",
opts: &DeleteOptions{RepoArg: "OWNER/REPO"},
wantErr: true,
errMsg: "could not prompt: confirmation with prompt or --yes flag required",
errMsg: "could not prompt: confirmation with prompt or --confirm flag required",
},
}
for _, tt := range tests {

View file

@ -15,7 +15,6 @@ func deleteRepo(client *http.Client, repo ghrepo.Interface) error {
ghrepo.FullName(repo))
request, err := http.NewRequest("DELETE", url, nil)
request.Header.Set("Accept", "application/vnd.github.v3+json")
if err != nil {
return err
}
@ -26,13 +25,8 @@ func deleteRepo(client *http.Client, repo ghrepo.Interface) error {
}
defer resp.Body.Close()
err = api.HandleHTTPError(resp)
if resp.StatusCode == 403 {
return fmt.Errorf(`%w
Deletion requires authorization with the "delete_repo" scope. To authorize, run "gh auth refresh -s delete_repo"`, err)
} else if resp.StatusCode > 204 {
return err
if resp.StatusCode > 299 {
return api.HandleHTTPError(resp)
}
return nil