Skip to content

Commit

Permalink
switch GetConfirmation()'s default to no (#4575)
Browse files Browse the repository at this point in the history
client/input.GetConfirmation() returns true if and only if the
user's input is confirmative.

The function is used in places where fat-fingering may cause
financial loss, e.g. gaiacli tx send command. Thus it seems
wiser to provide a conservative default in order to protect
users from accidental mistyping.

Closes: #4564
  • Loading branch information
Alessio Treglia committed Jun 18, 2019
1 parent 36142ab commit 1fcac93
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .pending/improvements/sdk/4564-Allow-empty-ans
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#4564 Allow empty answer to evaluate as 'Y' on tx submission prompt.
#4564 client/input.GetConfirmation()'s default is changed to No.
10 changes: 5 additions & 5 deletions client/input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ func GetCheckPassword(prompt, prompt2 string, buf *bufio.Reader) (string, error)

// GetConfirmation will request user give the confirmation from stdin.
// "y", "Y", "yes", "YES", and "Yes" all count as confirmations.
// If the input is not recognized, it will ask again.
// If the input is not recognized, it returns false and a nil error.
func GetConfirmation(prompt string, buf *bufio.Reader) (bool, error) {
for {
if inputIsTty() {
fmt.Print(fmt.Sprintf("%s [Y/n]: ", prompt))
fmt.Print(fmt.Sprintf("%s [y/N]: ", prompt))
}

response, err := readLineFromBuf(buf)
Expand All @@ -98,11 +98,11 @@ func GetConfirmation(prompt string, buf *bufio.Reader) (bool, error) {
}

response = strings.ToLower(strings.TrimSpace(response))
if response == "y" || response == "yes" || response == "" {
if response[0] == 'y' {
return true, nil
} else if response == "n" || response == "no" {
return false, nil
}

return false, nil
}
}

Expand Down
7 changes: 5 additions & 2 deletions client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,13 @@ func runAddCmd(_ *cobra.Command, args []string) error {
_, err = kb.Get(name)
if err == nil {
// account exists, ask for user confirmation
if response, err2 := input.GetConfirmation(
fmt.Sprintf("override the existing name %s", name), buf); err2 != nil || !response {
response, err2 := input.GetConfirmation(fmt.Sprintf("override the existing name %s", name), buf)
if err2 != nil {
return err2
}
if !response {
return errors.New("aborted")
}
}

multisigKeys := viper.GetStringSlice(flagMultisig)
Expand Down

0 comments on commit 1fcac93

Please sign in to comment.