From 4b6053134df46045ae8aa00a7f5f95857b517d9d Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Wed, 8 Mar 2023 11:07:12 +0800 Subject: [PATCH] cmd(ticdc): print error msg if an invalid input was read (#7936) (#8442) close pingcap/tiflow#7903 --- pkg/cmd/cli/cli_changefeed_helper.go | 29 ++++++++++++++++------------ pkg/cmd/cli/cli_changefeed_update.go | 9 ++------- pkg/cmd/cli/cli_unsafe.go | 13 ++----------- 3 files changed, 21 insertions(+), 30 deletions(-) diff --git a/pkg/cmd/cli/cli_changefeed_helper.go b/pkg/cmd/cli/cli_changefeed_helper.go index 530f73c141b..68e9a2168b0 100644 --- a/pkg/cmd/cli/cli_changefeed_helper.go +++ b/pkg/cmd/cli/cli_changefeed_helper.go @@ -39,6 +39,19 @@ const ( tsGapWarning = 86400 * 1000 ) +func readInput(cmd *cobra.Command) bool { + var yOrN string + _, err := fmt.Scan(&yOrN) + if err != nil { + cmd.Printf("Received invalid input: %s, abort the command.\n", err.Error()) + return false + } + if strings.ToLower(strings.TrimSpace(yOrN)) != "y" { + return false + } + return true +} + // confirmLargeDataGap checks if a large data gap is used. func confirmLargeDataGap(cmd *cobra.Command, currentPhysical int64, startTs uint64) error { tsGap := currentPhysical - oracle.ExtractPhysical(startTs) @@ -48,12 +61,8 @@ func confirmLargeDataGap(cmd *cobra.Command, currentPhysical int64, startTs uint "large data may cause OOM, confirm to continue at your own risk [Y/N]\n", time.Duration(tsGap)*time.Millisecond, ) - var yOrN string - _, err := fmt.Scan(&yOrN) - if err != nil { - return err - } - if strings.ToLower(strings.TrimSpace(yOrN)) != "y" { + confirmed := readInput(cmd) + if !confirmed { return errors.NewNoStackError("abort changefeed create or resume") } } @@ -64,12 +73,8 @@ func confirmLargeDataGap(cmd *cobra.Command, currentPhysical int64, startTs uint // confirmIgnoreIneligibleTables confirm if user need to ignore ineligible tables. func confirmIgnoreIneligibleTables(cmd *cobra.Command) error { cmd.Printf("Could you agree to ignore those tables, and continue to replicate [Y/N]\n") - var yOrN string - _, err := fmt.Scan(&yOrN) - if err != nil { - return err - } - if strings.ToLower(strings.TrimSpace(yOrN)) != "y" { + confirmed := readInput(cmd) + if !confirmed { cmd.Printf("No changefeed is created because you don't want to ignore some tables.\n") return errors.NewNoStackError("abort changefeed create or resume") } diff --git a/pkg/cmd/cli/cli_changefeed_update.go b/pkg/cmd/cli/cli_changefeed_update.go index 77152fb449b..348524dd864 100644 --- a/pkg/cmd/cli/cli_changefeed_update.go +++ b/pkg/cmd/cli/cli_changefeed_update.go @@ -14,7 +14,6 @@ package cli import ( - "fmt" "strings" "github.com/pingcap/errors" @@ -117,12 +116,8 @@ func (o *updateChangefeedOptions) run(cmd *cobra.Command) error { if !o.commonChangefeedOptions.noConfirm { cmd.Printf("Could you agree to apply changes above to changefeed [Y/N]\n") - var yOrN string - _, err = fmt.Scan(&yOrN) - if err != nil { - return err - } - if strings.ToLower(strings.TrimSpace(yOrN)) != "y" { + confirmed := readInput(cmd) + if !confirmed { cmd.Printf("No update to changefeed.\n") return nil } diff --git a/pkg/cmd/cli/cli_unsafe.go b/pkg/cmd/cli/cli_unsafe.go index 4c0fe3e47a6..d9a60fcd905 100644 --- a/pkg/cmd/cli/cli_unsafe.go +++ b/pkg/cmd/cli/cli_unsafe.go @@ -14,9 +14,6 @@ package cli import ( - "fmt" - "strings" - "github.com/pingcap/errors" "github.com/pingcap/tiflow/pkg/cmd/factory" "github.com/spf13/cobra" @@ -39,14 +36,8 @@ func (o *unsafeCommonOptions) confirmMetaDelete(cmd *cobra.Command) error { } cmd.Printf("Confirm that you know what this command will do and use it at your own risk [Y/N]\n") - - var yOrN string - _, err := fmt.Scan(&yOrN) - if err != nil { - return err - } - - if strings.ToLower(strings.TrimSpace(yOrN)) != "y" { + confirmed := readInput(cmd) + if !confirmed { return errors.NewNoStackError("abort meta command") }