X Tutup
Skip to content

Commit 2ebdde1

Browse files
committed
Exit with status code "2" on user cancellation errors
This also stops printing "interrupt" after Ctrl-C is pressed.
1 parent 3efa764 commit 2ebdde1

File tree

9 files changed

+26
-15
lines changed

9 files changed

+26
-15
lines changed

cmd/gh/main.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ func main() {
148148
rootCmd.SetArgs(expandedArgs)
149149

150150
if cmd, err := rootCmd.ExecuteC(); err != nil {
151+
if err == cmdutil.SilentError {
152+
os.Exit(1)
153+
} else if cmdutil.IsUserCancellation(err) {
154+
os.Exit(2)
155+
}
156+
151157
printError(stderr, err, cmd, hasDebug)
152158

153159
var httpErr api.HTTPError
@@ -177,10 +183,6 @@ func main() {
177183
}
178184

179185
func printError(out io.Writer, err error, cmd *cobra.Command, debug bool) {
180-
if err == cmdutil.SilentError {
181-
return
182-
}
183-
184186
var dnsError *net.DNSError
185187
if errors.As(err, &dnsError) {
186188
fmt.Fprintf(out, "error connecting to %s\n", dnsError.Name)

pkg/cmd/gist/edit/edit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func editRun(opts *EditOptions) error {
177177
case "Submit":
178178
stop = true
179179
case "Cancel":
180-
return cmdutil.SilentError
180+
return cmdutil.CancelError
181181
}
182182

183183
if stop {

pkg/cmd/issue/create/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func createRun(opts *CreateOptions) (err error) {
249249

250250
if action == prShared.CancelAction {
251251
fmt.Fprintln(opts.IO.ErrOut, "Discarding.")
252-
err = cmdutil.SilentError
252+
err = cmdutil.CancelError
253253
return
254254
}
255255
} else {

pkg/cmd/pr/create/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ func createRun(opts *CreateOptions) (err error) {
299299

300300
if action == shared.CancelAction {
301301
fmt.Fprintln(opts.IO.ErrOut, "Discarding.")
302-
err = cmdutil.SilentError
302+
err = cmdutil.CancelError
303303
return
304304
}
305305

@@ -542,7 +542,7 @@ func NewCreateContext(opts *CreateOptions) (*CreateContext, error) {
542542
} else if pushOptions[selectedOption] == "Skip pushing the branch" {
543543
isPushEnabled = false
544544
} else if pushOptions[selectedOption] == "Cancel" {
545-
return nil, cmdutil.SilentError
545+
return nil, cmdutil.CancelError
546546
} else {
547547
// "Create a fork of ..."
548548
if baseRepo.IsPrivate {

pkg/cmd/pr/merge/merge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func mergeRun(opts *MergeOptions) error {
228228
}
229229
if action == shared.CancelAction {
230230
fmt.Fprintln(opts.IO.ErrOut, "Cancelled.")
231-
return cmdutil.SilentError
231+
return cmdutil.CancelError
232232
}
233233
}
234234

pkg/cmd/pr/shared/preserve.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ package shared
22

33
import (
44
"encoding/json"
5-
"errors"
65
"fmt"
76
"os"
87

9-
"github.com/AlecAivazis/survey/v2/terminal"
108
"github.com/cli/cli/pkg/cmdutil"
119
"github.com/cli/cli/pkg/iostreams"
1210
)
@@ -21,7 +19,7 @@ func PreserveInput(io *iostreams.IOStreams, state *IssueMetadataState, createErr
2119
return
2220
}
2321

24-
if errors.Is(*createErr, cmdutil.SilentError) || errors.Is(*createErr, terminal.InterruptErr) {
22+
if cmdutil.IsUserCancellation(*createErr) {
2523
// these errors are user-initiated cancellations
2624
return
2725
}

pkg/cmd/release/create/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func createRun(opts *CreateOptions) error {
262262
case "Save as draft":
263263
opts.Draft = true
264264
case "Cancel":
265-
return cmdutil.SilentError
265+
return cmdutil.CancelError
266266
default:
267267
return fmt.Errorf("invalid action: %v", opts.SubmitAction)
268268
}

pkg/cmd/release/delete/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func deleteRun(opts *DeleteOptions) error {
7878
}
7979

8080
if !confirmed {
81-
return cmdutil.SilentError
81+
return cmdutil.CancelError
8282
}
8383
}
8484

pkg/cmdutil/errors.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package cmdutil
22

3-
import "errors"
3+
import (
4+
"errors"
5+
6+
"github.com/AlecAivazis/survey/v2/terminal"
7+
)
48

59
// FlagError is the kind of error raised in flag processing
610
type FlagError struct {
@@ -17,3 +21,10 @@ func (fe FlagError) Unwrap() error {
1721

1822
// SilentError is an error that triggers exit code 1 without any error messaging
1923
var SilentError = errors.New("SilentError")
24+
25+
// CancelError signals user-initiated cancellation
26+
var CancelError = errors.New("CancelError")
27+
28+
func IsUserCancellation(err error) bool {
29+
return errors.Is(err, CancelError) || errors.Is(err, terminal.InterruptErr)
30+
}

0 commit comments

Comments
 (0)
X Tutup