Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small non-breaking UX changes to keys {list,delete} commands #5366

Merged
merged 3 commits into from
Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ upgrade via: `sudo rm -rf /Library/Developer/CommandLineTools; xcode-select --in
correct version via: `pkgutil --pkg-info=com.apple.pkg.CLTools_Executables`.
* (keys) [\#5097](https://github.com/cosmos/cosmos-sdk/pull/5097) New `keys migrate` command to assist users migrate their keys
to the new keyring.
* (keys) [\#5366](https://github.com/cosmos/cosmos-sdk/pull/5366) `keys list` now accepts a `--list-names` option to list key names only, whilst the `keys delete`
command can delete multiple keys by passing their names as arguments. The aforementioned commands can then be piped together, e.g.
`appcli keys list -n | xargs appcli keys delete`
* (modules) [\#4233](https://github.com/cosmos/cosmos-sdk/pull/4233) Add upgrade module that coordinates software upgrades of live chains.
* [\#4486](https://github.com/cosmos/cosmos-sdk/issues/4486) Introduce new `PeriodicVestingAccount` vesting account type
that allows for arbitrary vesting periods.
Expand Down
44 changes: 23 additions & 21 deletions client/keys/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,59 +18,61 @@ const (

func deleteKeyCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "delete <name>",
Short: "Delete the given key",
Long: `Delete a key from the store.
Use: "delete <name>...",
Short: "Delete the given keys",
Long: `Delete keys from the Keybase backend.

Note that removing offline or ledger keys will remove
only the public key references stored locally, i.e.
private keys stored in a ledger device cannot be deleted with the CLI.
`,
RunE: runDeleteCmd,
Args: cobra.ExactArgs(1),
Args: cobra.MinimumNArgs(1),
}

cmd.Flags().BoolP(flagYes, "y", false,
"Skip confirmation prompt when deleting offline or ledger key references")
cmd.Flags().BoolP(flagForce, "f", false,
"Remove the key unconditionally without asking for the passphrase")
"Remove the key unconditionally without asking for the passphrase. Deprecated.")
return cmd
}

func runDeleteCmd(cmd *cobra.Command, args []string) error {
name := args[0]
buf := bufio.NewReader(cmd.InOrStdin())

kb, err := NewKeyringFromHomeFlag(buf)
if err != nil {
return err
}

info, err := kb.Get(name)
if err != nil {
return err
}
for _, name := range args {
info, err := kb.Get(name)
if err != nil {
return err
}

if info.GetType() == keys.TypeLedger || info.GetType() == keys.TypeOffline {
// confirm deletion, unless -y is passed
if !viper.GetBool(flagYes) {
if err := confirmDeletion(buf); err != nil {
if info.GetType() == keys.TypeLedger || info.GetType() == keys.TypeOffline {
// confirm deletion, unless -y is passed
if !viper.GetBool(flagYes) {
if err := confirmDeletion(buf); err != nil {
return err
}
}

if err := kb.Delete(name, "", true); err != nil {
return err
}
cmd.PrintErrln("Public key reference deleted")
return nil
}

// old password and skip flag arguments are ignored
if err := kb.Delete(name, "", true); err != nil {
return err
}
cmd.PrintErrln("Public key reference deleted")
return nil
cmd.PrintErrln("Key deleted forever (uh oh!)")
}

// old password and skip flag arguments are ignored
if err := kb.Delete(name, "", true); err != nil {
return err
}
cmd.PrintErrln("Key deleted forever (uh oh!)")
return nil
}

Expand Down
21 changes: 18 additions & 3 deletions client/keys/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package keys

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

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

const flagListNames = "list-names"

func listKeysCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Expand All @@ -15,18 +18,30 @@ along with their associated name and address.`,
RunE: runListCmd,
}
cmd.Flags().Bool(flags.FlagIndentResponse, false, "Add indent to JSON response")
cmd.Flags().BoolP(flagListNames, "n", false, "List names only")
return cmd
}

func runListCmd(cmd *cobra.Command, args []string) error {
func runListCmd(cmd *cobra.Command, _ []string) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should consider cleaning this in the future

kb, err := NewKeyringFromHomeFlag(cmd.InOrStdin())
if err != nil {
return err
}

infos, err := kb.List()
if err == nil {
if err != nil {
return err
}

if !viper.GetBool(flagListNames) {
printInfos(infos)
return nil
}

cmd.SetOut(cmd.OutOrStdout())
for _, info := range infos {
cmd.Println(info.GetName())
}
return err

return nil
}
9 changes: 9 additions & 0 deletions client/keys/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,19 @@ func Test_runListCmd(t *testing.T) {
if runningUnattended {
mockIn.Reset("testpass1\ntestpass1\n")
}
viper.Set(flagListNames, false)
viper.Set(flags.FlagHome, tt.kbDir)
if err := runListCmd(tt.args.cmd, tt.args.args); (err != nil) != tt.wantErr {
t.Errorf("runListCmd() error = %v, wantErr %v", err, tt.wantErr)
}

if runningUnattended {
mockIn.Reset("testpass1\ntestpass1\n")
}
viper.Set(flagListNames, true)
if err := runListCmd(tt.args.cmd, tt.args.args); (err != nil) != tt.wantErr {
t.Errorf("runListCmd() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}