Skip to content

Commit

Permalink
client/keys: make add command's output parseable (#6603)
Browse files Browse the repository at this point in the history
client/input/input.go: GetConfirmation() should communicate
with the user via the io.Writer instance passed in as
argument.

client/keys/add.go: replace cmd.PrintErrln() calls with
fmt.Fprintln(cmd.ErrOrStderr(), ...) because of cobra's
PrintErr* functions broken behaviour. For more information
please see spf13/cobra#894

Closes: #6601
Thanks: @noandrea for pointing this out.
  • Loading branch information
Alessio Treglia authored and daeMOn63 committed May 4, 2021
1 parent ee2d539 commit a266847
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion client/input/input.go
Expand Up @@ -41,7 +41,7 @@ func GetPassword(prompt string, buf *bufio.Reader) (pass string, err error) {
// If the input is not recognized, it returns false and a nil error.
func GetConfirmation(prompt string, r *bufio.Reader, w io.Writer) (bool, error) {
if inputIsTty() {
fmt.Printf("%s [y/N]: ", prompt)
fmt.Fprintf(w, "%s [y/N]: ", prompt)
}

response, err := readLineFromBuf(r)
Expand Down
15 changes: 9 additions & 6 deletions client/keys/add.go
Expand Up @@ -80,6 +80,9 @@ the flag --nosort is set.
cmd.Flags().Uint32(flagIndex, 0, "Address index number for HD derivation")
cmd.Flags().String(flagKeyAlgo, string(hd.Secp256k1Type), "Key signing algorithm to generate keys for")

cmd.SetOut(cmd.OutOrStdout())
cmd.SetErr(cmd.ErrOrStderr())

return cmd
}

Expand Down Expand Up @@ -115,7 +118,6 @@ func RunAddCmd(cmd *cobra.Command, args []string, kb keyring.Keyring, inBuf *buf
var err error

name := args[0]

interactive := viper.GetBool(flagInteractive)
showMnemonic := !viper.GetBool(flagNoBackup)

Expand Down Expand Up @@ -295,10 +297,11 @@ func printCreate(cmd *cobra.Command, info keyring.Info, showMnemonic bool, mnemo

// print mnemonic unless requested not to.
if showMnemonic {
cmd.PrintErrln("\n**Important** write this mnemonic phrase in a safe place.")
cmd.PrintErrln("It is the only way to recover your account if you ever forget your password.")
cmd.PrintErrln("")
cmd.PrintErrln(mnemonic)
fmt.Fprintln(cmd.ErrOrStderr(), "\n**Important** write this mnemonic phrase in a safe place.")
fmt.Fprintln(cmd.ErrOrStderr(), "\n**Important** write this mnemonic phrase in a safe place.")
fmt.Fprintln(cmd.ErrOrStderr(), "It is the only way to recover your account if you ever forget your password.")
fmt.Fprintln(cmd.ErrOrStderr(), "")
fmt.Fprintln(cmd.ErrOrStderr(), mnemonic)
}
case OutputFormatJSON:
out, err := keyring.Bech32KeyOutput(info)
Expand All @@ -315,7 +318,7 @@ func printCreate(cmd *cobra.Command, info keyring.Info, showMnemonic bool, mnemo
return err
}

cmd.PrintErrln(string(jsonString))
cmd.Println(string(jsonString))

default:
return fmt.Errorf("invalid output format %s", output)
Expand Down

0 comments on commit a266847

Please sign in to comment.