Exit with status code "2" on user cancellation errors

This also stops printing "interrupt" after Ctrl-C is pressed.
This commit is contained in:
Mislav Marohnić 2021-03-02 13:48:44 +01:00
parent 3efa764305
commit 2ebdde1ddd
9 changed files with 26 additions and 15 deletions

View file

@ -148,6 +148,12 @@ func main() {
rootCmd.SetArgs(expandedArgs)
if cmd, err := rootCmd.ExecuteC(); err != nil {
if err == cmdutil.SilentError {
os.Exit(1)
} else if cmdutil.IsUserCancellation(err) {
os.Exit(2)
}
printError(stderr, err, cmd, hasDebug)
var httpErr api.HTTPError
@ -177,10 +183,6 @@ func main() {
}
func printError(out io.Writer, err error, cmd *cobra.Command, debug bool) {
if err == cmdutil.SilentError {
return
}
var dnsError *net.DNSError
if errors.As(err, &dnsError) {
fmt.Fprintf(out, "error connecting to %s\n", dnsError.Name)

View file

@ -177,7 +177,7 @@ func editRun(opts *EditOptions) error {
case "Submit":
stop = true
case "Cancel":
return cmdutil.SilentError
return cmdutil.CancelError
}
if stop {

View file

@ -249,7 +249,7 @@ func createRun(opts *CreateOptions) (err error) {
if action == prShared.CancelAction {
fmt.Fprintln(opts.IO.ErrOut, "Discarding.")
err = cmdutil.SilentError
err = cmdutil.CancelError
return
}
} else {

View file

@ -299,7 +299,7 @@ func createRun(opts *CreateOptions) (err error) {
if action == shared.CancelAction {
fmt.Fprintln(opts.IO.ErrOut, "Discarding.")
err = cmdutil.SilentError
err = cmdutil.CancelError
return
}
@ -542,7 +542,7 @@ func NewCreateContext(opts *CreateOptions) (*CreateContext, error) {
} else if pushOptions[selectedOption] == "Skip pushing the branch" {
isPushEnabled = false
} else if pushOptions[selectedOption] == "Cancel" {
return nil, cmdutil.SilentError
return nil, cmdutil.CancelError
} else {
// "Create a fork of ..."
if baseRepo.IsPrivate {

View file

@ -228,7 +228,7 @@ func mergeRun(opts *MergeOptions) error {
}
if action == shared.CancelAction {
fmt.Fprintln(opts.IO.ErrOut, "Cancelled.")
return cmdutil.SilentError
return cmdutil.CancelError
}
}

View file

@ -2,11 +2,9 @@ package shared
import (
"encoding/json"
"errors"
"fmt"
"os"
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/pkg/iostreams"
)
@ -21,7 +19,7 @@ func PreserveInput(io *iostreams.IOStreams, state *IssueMetadataState, createErr
return
}
if errors.Is(*createErr, cmdutil.SilentError) || errors.Is(*createErr, terminal.InterruptErr) {
if cmdutil.IsUserCancellation(*createErr) {
// these errors are user-initiated cancellations
return
}

View file

@ -262,7 +262,7 @@ func createRun(opts *CreateOptions) error {
case "Save as draft":
opts.Draft = true
case "Cancel":
return cmdutil.SilentError
return cmdutil.CancelError
default:
return fmt.Errorf("invalid action: %v", opts.SubmitAction)
}

View file

@ -78,7 +78,7 @@ func deleteRun(opts *DeleteOptions) error {
}
if !confirmed {
return cmdutil.SilentError
return cmdutil.CancelError
}
}

View file

@ -1,6 +1,10 @@
package cmdutil
import "errors"
import (
"errors"
"github.com/AlecAivazis/survey/v2/terminal"
)
// FlagError is the kind of error raised in flag processing
type FlagError struct {
@ -17,3 +21,10 @@ func (fe FlagError) Unwrap() error {
// SilentError is an error that triggers exit code 1 without any error messaging
var SilentError = errors.New("SilentError")
// CancelError signals user-initiated cancellation
var CancelError = errors.New("CancelError")
func IsUserCancellation(err error) bool {
return errors.Is(err, CancelError) || errors.Is(err, terminal.InterruptErr)
}