From 3efa76430576eb11ac33aaadc4719cfc14db4ee4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 23 Feb 2021 20:09:03 +0100 Subject: [PATCH] Avoid the issue/pr recovery mechanism handling Ctrl-C keypress in prompts Either InterruptErr or SilentErr will be present when the user has chosen "Cancel" or pressed Ctrl-C in prompts. We don't want the recovery mechanism to kick in these cases because the cancellation was likely willingly initiated by the user. --- pkg/cmd/issue/create/create.go | 4 ++-- pkg/cmd/pr/create/create.go | 4 ++-- pkg/cmd/pr/shared/preserve.go | 8 ++++++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/issue/create/create.go b/pkg/cmd/issue/create/create.go index 298f3bf97..3d6b5b25c 100644 --- a/pkg/cmd/issue/create/create.go +++ b/pkg/cmd/issue/create/create.go @@ -249,8 +249,8 @@ func createRun(opts *CreateOptions) (err error) { if action == prShared.CancelAction { fmt.Fprintln(opts.IO.ErrOut, "Discarding.") - err = nil // avoid triggering PreserveInput - return cmdutil.SilentError + err = cmdutil.SilentError + return } } else { if tb.Title == "" { diff --git a/pkg/cmd/pr/create/create.go b/pkg/cmd/pr/create/create.go index a56410726..18b8c1e99 100644 --- a/pkg/cmd/pr/create/create.go +++ b/pkg/cmd/pr/create/create.go @@ -299,8 +299,8 @@ func createRun(opts *CreateOptions) (err error) { if action == shared.CancelAction { fmt.Fprintln(opts.IO.ErrOut, "Discarding.") - err = nil // avoid triggering PreserveInput - return cmdutil.SilentError + err = cmdutil.SilentError + return } err = handlePush(*opts, *ctx) diff --git a/pkg/cmd/pr/shared/preserve.go b/pkg/cmd/pr/shared/preserve.go index 4105823cd..44b8fa183 100644 --- a/pkg/cmd/pr/shared/preserve.go +++ b/pkg/cmd/pr/shared/preserve.go @@ -2,9 +2,12 @@ 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" ) @@ -18,6 +21,11 @@ func PreserveInput(io *iostreams.IOStreams, state *IssueMetadataState, createErr return } + if errors.Is(*createErr, cmdutil.SilentError) || errors.Is(*createErr, terminal.InterruptErr) { + // these errors are user-initiated cancellations + return + } + out := io.ErrOut // this extra newline guards against appending to the end of a survey line