Skip to content

Commit

Permalink
cmd(ticdc): print error msg if an invalid input was read (pingcap#7936)…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot committed Mar 8, 2023
1 parent cc2a701 commit 4b60531
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 30 deletions.
29 changes: 17 additions & 12 deletions pkg/cmd/cli/cli_changefeed_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
}
}
Expand All @@ -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")
}
Expand Down
9 changes: 2 additions & 7 deletions pkg/cmd/cli/cli_changefeed_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package cli

import (
"fmt"
"strings"

"github.com/pingcap/errors"
Expand Down Expand Up @@ -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
}
Expand Down
13 changes: 2 additions & 11 deletions pkg/cmd/cli/cli_unsafe.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
package cli

import (
"fmt"
"strings"

"github.com/pingcap/errors"
"github.com/pingcap/tiflow/pkg/cmd/factory"
"github.com/spf13/cobra"
Expand All @@ -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")
}

Expand Down

0 comments on commit 4b60531

Please sign in to comment.