Skip to content

Commit

Permalink
fix: keys import/export need also to read from the proper reader (#107)
Browse files Browse the repository at this point in the history
* continuation from #106 on other impacted commands:
  *  keys import
  *  keys export
  • Loading branch information
daeMOn63 authored Aug 2, 2021
1 parent 0cdd05e commit ce05d64
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion client/keys/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ FULLY AWARE OF THE RISKS. If you are unsure, you may want to do some research
and export your keys in ASCII-armored encrypted format.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
buf := bufio.NewReader(cmd.InOrStdin())
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
buf := bufio.NewReader(clientCtx.Input)
unarmored, _ := cmd.Flags().GetBool(flagUnarmoredHex)
unsafe, _ := cmd.Flags().GetBool(flagUnsafe)

Expand Down
10 changes: 7 additions & 3 deletions client/keys/export_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keys

import (
"bytes"
"context"
"fmt"
"testing"
Expand All @@ -25,7 +26,7 @@ func Test_runExportCmd(t *testing.T) {
kbHome := t.TempDir()

// create a key
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn)
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, nil)
require.NoError(t, err)
t.Cleanup(func() {
kb.Delete("keyname1") // nolint:errcheck
Expand All @@ -47,7 +48,8 @@ func Test_runExportCmd(t *testing.T) {

clientCtx := client.Context{}.
WithKeyringDir(kbHome).
WithKeyring(kb)
WithKeyring(kb).
WithInput(mockIn)
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)

require.NoError(t, cmd.ExecuteContext(ctx))
Expand All @@ -64,7 +66,9 @@ func Test_runExportCmd(t *testing.T) {
cmd.SetArgs(argsUnsafeUnarmoredHex)
require.Error(t, cmd.ExecuteContext(ctx))

mockIn, mockOut := testutil.ApplyMockIO(cmd)
mockOut := bytes.NewBufferString("")
cmd.SetOut(mockOut)
cmd.SetErr(mockOut)
mockIn.Reset("y\n")
require.NoError(t, cmd.ExecuteContext(ctx))
require.Equal(t, "2485e33678db4175dc0ecef2d6e1fc493d4a0d7f7ce83324b6ed70afe77f3485\n", mockOut.String())
Expand Down
2 changes: 1 addition & 1 deletion client/keys/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func ImportKeyCommand() *cobra.Command {
Long: "Import a ASCII armored private key into the local keybase.",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
buf := bufio.NewReader(cmd.InOrStdin())
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
buf := bufio.NewReader(clientCtx.Input)

bz, err := ioutil.ReadFile(args[1])
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions client/keys/import_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keys

import (
"bufio"
"context"
"fmt"
"io/ioutil"
Expand All @@ -23,11 +24,12 @@ func Test_runImportCmd(t *testing.T) {

// Now add a temporary keybase
kbHome := t.TempDir()
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn)
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, nil)

clientCtx := client.Context{}.
WithKeyringDir(kbHome).
WithKeyring(kb)
WithKeyring(kb).
WithInput(bufio.NewReader(mockIn))
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)

require.NoError(t, err)
Expand Down

0 comments on commit ce05d64

Please sign in to comment.