Skip to content

Commit

Permalink
Remove client/input.{Buffer,Override}Stdin() functions (#4602)
Browse files Browse the repository at this point in the history
Cobra's new release made them redundant.

Thanks: Juan Leni <[email protected]> for the original patch.
  • Loading branch information
Alessio Treglia authored Jun 22, 2019
1 parent 4ffabb6 commit 7b5e6ce
Show file tree
Hide file tree
Showing 21 changed files with 118 additions and 148 deletions.
1 change: 1 addition & 0 deletions .pending/breaking/sdk/4602-client-input-Bu
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#4602 client/input.{Buffer,Override}Stdin() functions are removed. Thanks to cobra's new release they are now redundant.
2 changes: 0 additions & 2 deletions client/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ var (
GetValidators = rpc.GetValidators
ValidatorSetRequestHandlerFn = rpc.ValidatorSetRequestHandlerFn
LatestValidatorSetRequestHandlerFn = rpc.LatestValidatorSetRequestHandlerFn
BufferStdin = input.BufferStdin
OverrideStdin = input.OverrideStdin
GetPassword = input.GetPassword
GetCheckPassword = input.GetCheckPassword
GetConfirmation = input.GetConfirmation
Expand Down
22 changes: 0 additions & 22 deletions client/input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,6 @@ import (
// MinPassLength is the minimum acceptable password length
const MinPassLength = 8

var currentStdin *bufio.Reader

func init() {
currentStdin = bufio.NewReader(os.Stdin)
}

// BufferStdin is used to allow reading prompts for stdin
// multiple times, when we read from non-tty
func BufferStdin() *bufio.Reader {
return currentStdin
}

// OverrideStdin allows to temporarily override stdin
func OverrideStdin(newStdin *bufio.Reader) (cleanUp func()) {
prevStdin := currentStdin
currentStdin = newStdin
cleanUp = func() {
currentStdin = prevStdin
}
return cleanUp
}

// GetPassword will prompt for a password one-time (to sign a tx)
// It enforces the password length
func GetPassword(prompt string, buf *bufio.Reader) (pass string, err error) {
Expand Down
41 changes: 20 additions & 21 deletions client/keys/add.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package keys

import (
"bufio"
"bytes"
"errors"
"fmt"
"os"
"sort"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/input"
"github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"

"errors"

"github.com/spf13/cobra"
"github.com/spf13/viper"

bip39 "github.com/cosmos/go-bip39"
"github.com/cosmos/go-bip39"

"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/multisig"
Expand Down Expand Up @@ -85,12 +84,12 @@ input
output
- armor encrypted private key (saved to file)
*/
func runAddCmd(_ *cobra.Command, args []string) error {
func runAddCmd(cmd *cobra.Command, args []string) error {
var kb keys.Keybase
var err error
var encryptPassword string

buf := input.BufferStdin()
inBuf := bufio.NewReader(cmd.InOrStdin())
name := args[0]

interactive := viper.GetBool(flagInteractive)
Expand All @@ -110,7 +109,7 @@ func runAddCmd(_ *cobra.Command, args []string) error {
_, err = kb.Get(name)
if err == nil {
// account exists, ask for user confirmation
response, err2 := input.GetConfirmation(fmt.Sprintf("override the existing name %s", name), buf)
response, err2 := input.GetConfirmation(fmt.Sprintf("override the existing name %s", name), inBuf)
if err2 != nil {
return err2
}
Expand Down Expand Up @@ -148,15 +147,15 @@ func runAddCmd(_ *cobra.Command, args []string) error {
return err
}

fmt.Fprintf(os.Stderr, "Key %q saved to disk.\n", name)
cmd.PrintErrf("Key %q saved to disk.\n", name)
return nil
}

// ask for a password when generating a local key
if viper.GetString(FlagPublicKey) == "" && !viper.GetBool(flags.FlagUseLedger) {
encryptPassword, err = input.GetCheckPassword(
"Enter a passphrase to encrypt your key to disk:",
"Repeat the passphrase:", buf)
"Repeat the passphrase:", inBuf)
if err != nil {
return err
}
Expand Down Expand Up @@ -186,7 +185,7 @@ func runAddCmd(_ *cobra.Command, args []string) error {
return err
}

return printCreate(info, false, "")
return printCreate(cmd, info, false, "")
}

// Get bip39 mnemonic
Expand All @@ -199,7 +198,7 @@ func runAddCmd(_ *cobra.Command, args []string) error {
bip39Message = "Enter your bip39 mnemonic, or hit enter to generate one."
}

mnemonic, err = input.GetString(bip39Message, buf)
mnemonic, err = input.GetString(bip39Message, inBuf)
if err != nil {
return err
}
Expand All @@ -226,14 +225,14 @@ func runAddCmd(_ *cobra.Command, args []string) error {
if interactive {
bip39Passphrase, err = input.GetString(
"Enter your bip39 passphrase. This is combined with the mnemonic to derive the seed. "+
"Most users should just hit enter to use the default, \"\"", buf)
"Most users should just hit enter to use the default, \"\"", inBuf)
if err != nil {
return err
}

// if they use one, make them re-enter it
if len(bip39Passphrase) != 0 {
p2, err := input.GetString("Repeat the passphrase:", buf)
p2, err := input.GetString("Repeat the passphrase:", inBuf)
if err != nil {
return err
}
Expand All @@ -256,23 +255,23 @@ func runAddCmd(_ *cobra.Command, args []string) error {
mnemonic = ""
}

return printCreate(info, showMnemonic, mnemonic)
return printCreate(cmd, info, showMnemonic, mnemonic)
}

func printCreate(info keys.Info, showMnemonic bool, mnemonic string) error {
func printCreate(cmd *cobra.Command, info keys.Info, showMnemonic bool, mnemonic string) error {
output := viper.Get(cli.OutputFlag)

switch output {
case OutputFormatText:
fmt.Fprintln(os.Stderr)
cmd.PrintErrln()
printKeyInfo(info, keys.Bech32KeyOutput)

// print mnemonic unless requested not to.
if showMnemonic {
fmt.Fprintln(os.Stderr, "\n**Important** write this mnemonic phrase in a safe place.")
fmt.Fprintln(os.Stderr, "It is the only way to recover your account if you ever forget your password.")
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, mnemonic)
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)
}
case OutputFormatJSON:
out, err := keys.Bech32KeyOutput(info)
Expand All @@ -294,7 +293,7 @@ func printCreate(info keys.Info, showMnemonic bool, mnemonic string) error {
if err != nil {
return err
}
fmt.Fprintln(os.Stderr, string(jsonString))
cmd.PrintErrln(string(jsonString))
default:
return fmt.Errorf("I can't speak: %s", output)
}
Expand Down
12 changes: 4 additions & 8 deletions client/keys/add_ledger_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
//+build ledger,test_ledger_mock
//+build ledger test_ledger_mock

package keys

import (
"bufio"
"strings"
"testing"

"github.com/spf13/viper"
Expand All @@ -13,7 +11,6 @@ import (
"github.com/tendermint/tendermint/libs/cli"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/input"
"github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/tests"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -33,10 +30,9 @@ func Test_runAddCmdLedger(t *testing.T) {
/// Test Text
viper.Set(cli.OutputFlag, OutputFormatText)
// Now enter password
cleanUp1 := input.OverrideStdin(bufio.NewReader(strings.NewReader("test1234\ntest1234\n")))
defer cleanUp1()
err := runAddCmd(cmd, []string{"keyname1"})
assert.NoError(t, err)
mockIn, _, _ := tests.ApplyMockIO(cmd)
mockIn.Reset("test1234\ntest1234\n")
assert.NoError(t, runAddCmd(cmd, []string{"keyname1"}))

// Now check that it has been stored properly
kb, err := NewKeyBaseFromHomeFlag()
Expand Down
29 changes: 9 additions & 20 deletions client/keys/add_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package keys

import (
"bufio"
"strings"
"testing"

"github.com/spf13/viper"
Expand All @@ -11,49 +9,40 @@ import (
"github.com/tendermint/tendermint/libs/cli"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/input"
"github.com/cosmos/cosmos-sdk/tests"
)

func Test_runAddCmdBasic(t *testing.T) {
cmd := addKeyCommand()
assert.NotNil(t, cmd)
mockIn, _, _ := tests.ApplyMockIO(cmd)

// Prepare a keybase
kbHome, kbCleanUp := tests.NewTestCaseDir(t)
assert.NotNil(t, kbHome)
defer kbCleanUp()
viper.Set(flags.FlagHome, kbHome)

/// Test Text
viper.Set(cli.OutputFlag, OutputFormatText)
// Now enter password
cleanUp1 := input.OverrideStdin(bufio.NewReader(strings.NewReader("test1234\ntest1234\n")))
defer cleanUp1()

mockIn.Reset("test1234\ntest1234\n")
err := runAddCmd(cmd, []string{"keyname1"})
assert.NoError(t, err)

/// Test Text - Replace? >> FAIL
viper.Set(cli.OutputFlag, OutputFormatText)
// Now enter password
cleanUp2 := input.OverrideStdin(bufio.NewReader(strings.NewReader("test1234\ntest1234\n")))
defer cleanUp2()

mockIn.Reset("test1234\ntest1234\n")
err = runAddCmd(cmd, []string{"keyname1"})
assert.Error(t, err)

/// Test Text - Replace? Answer >> PASS
viper.Set(cli.OutputFlag, OutputFormatText)
// Now enter password
cleanUp3 := input.OverrideStdin(bufio.NewReader(strings.NewReader("y\ntest1234\ntest1234\n")))
defer cleanUp3()

mockIn.Reset("y\ntest1234\ntest1234\n")
err = runAddCmd(cmd, []string{"keyname1"})
assert.NoError(t, err)

// Check JSON
viper.Set(cli.OutputFlag, OutputFormatJSON)
// Now enter password
cleanUp4 := input.OverrideStdin(bufio.NewReader(strings.NewReader("test1234\ntest1234\n")))
defer cleanUp4()

mockIn.Reset("test1234\ntest1234\n")
err = runAddCmd(cmd, []string{"keyname2"})
assert.NoError(t, err)
}
8 changes: 3 additions & 5 deletions client/keys/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package keys
import (
"bufio"
"errors"
"fmt"
"os"

"github.com/spf13/viper"

Expand Down Expand Up @@ -53,7 +51,7 @@ func runDeleteCmd(cmd *cobra.Command, args []string) error {
return err
}

buf := input.BufferStdin()
buf := bufio.NewReader(cmd.InOrStdin())
if info.GetType() == keys.TypeLedger || info.GetType() == keys.TypeOffline {
if !viper.GetBool(flagYes) {
if err := confirmDeletion(buf); err != nil {
Expand All @@ -63,7 +61,7 @@ func runDeleteCmd(cmd *cobra.Command, args []string) error {
if err := kb.Delete(name, "", true); err != nil {
return err
}
fmt.Fprintln(os.Stderr, "Public key reference deleted")
cmd.PrintErrln("Public key reference deleted")
return nil
}

Expand All @@ -81,7 +79,7 @@ func runDeleteCmd(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
fmt.Fprintln(os.Stderr, "Key deleted forever (uh oh!)")
cmd.PrintErrln("Key deleted forever (uh oh!)")
return nil
}

Expand Down
8 changes: 3 additions & 5 deletions client/keys/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/input"
"github.com/cosmos/cosmos-sdk/tests"
)

Expand Down Expand Up @@ -52,10 +51,9 @@ func Test_runDeleteCmd(t *testing.T) {
require.NoError(t, err)

// Now there is a confirmation
cleanUp := input.OverrideStdin(bufio.NewReader(strings.NewReader("y\n")))
defer cleanUp()
err = runDeleteCmd(deleteKeyCommand, []string{fakeKeyName1})
require.NoError(t, err)
mockIn, _, _ := tests.ApplyMockIO(deleteKeyCommand)
mockIn.Reset("y\n")
require.NoError(t, runDeleteCmd(deleteKeyCommand, []string{fakeKeyName1}))

_, err = kb.Get(fakeKeyName1)
require.Error(t, err) // Key1 is gone
Expand Down
8 changes: 4 additions & 4 deletions client/keys/export.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package keys

import (
"fmt"
"bufio"

"github.com/spf13/cobra"

Expand All @@ -19,13 +19,13 @@ func exportKeyCommand() *cobra.Command {
return cmd
}

func runExportCmd(_ *cobra.Command, args []string) error {
func runExportCmd(cmd *cobra.Command, args []string) error {
kb, err := NewKeyBaseFromHomeFlag()
if err != nil {
return err
}

buf := input.BufferStdin()
buf := bufio.NewReader(cmd.InOrStdin())
decryptPassword, err := input.GetPassword("Enter passphrase to decrypt your key:", buf)
if err != nil {
return err
Expand All @@ -40,6 +40,6 @@ func runExportCmd(_ *cobra.Command, args []string) error {
return err
}

fmt.Println(armored)
cmd.Println(armored)
return nil
}
Loading

0 comments on commit 7b5e6ce

Please sign in to comment.