Skip to content

Commit

Permalink
feat(cmd/wallet): Support removal of multiple accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
amela committed Apr 22, 2024
1 parent 9073cc1 commit 51d0b13
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
44 changes: 32 additions & 12 deletions cmd/wallet/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package wallet

import (
"fmt"
"strings"

"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
Expand All @@ -10,26 +11,43 @@ import (
"github.com/oasisprotocol/cli/config"
)

func unique(stringSlice []string) []string {
keys := make(map[string]bool)
list := []string{}
for _, entry := range stringSlice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}

var rmCmd = &cobra.Command{
Use: "remove <name>",
Use: "remove <name> [name ...]",
Aliases: []string{"rm"},
Short: "Remove an existing account",
Args: cobra.ExactArgs(1),
Short: "Remove existing account(s)",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
cfg := config.Global()
name := args[0]

// Early check for whether the wallet exists so that we don't ask for confirmation first.
if _, exists := cfg.Wallet.All[name]; !exists {
cobra.CheckErr(fmt.Errorf("account '%s' does not exist", name))
// Remove duplicated arguments
uniqueArgs := unique(args)

for _, name := range uniqueArgs {
// Early check for whether the accounts exist so that we don't ask for
// confirmation first or delete only part of the list.
if _, exists := cfg.Wallet.All[name]; !exists {
cobra.CheckErr(fmt.Errorf("account '%s' does not exist", name))
}
}

if !common.GetAnswerYes() {
fmt.Printf("WARNING: Removing the account will ERASE secret key material!\n")
fmt.Printf("WARNING: THIS ACTION IS IRREVERSIBLE!\n")

var result string
confirmText := fmt.Sprintf("I really want to remove account %s", name)
confirmText := fmt.Sprintf("I really want to remove account(s): %s", strings.Join(uniqueArgs, ", "))
prompt := &survey.Input{
Message: fmt.Sprintf("Enter '%s' (without quotes) to confirm removal:", confirmText),
}
Expand All @@ -41,11 +59,13 @@ var rmCmd = &cobra.Command{
}
}

err := cfg.Wallet.Remove(name)
cobra.CheckErr(err)
for _, name := range uniqueArgs {
err := cfg.Wallet.Remove(name)
cobra.CheckErr(err)

err = cfg.Save()
cobra.CheckErr(err)
err = cfg.Save()
cobra.CheckErr(err)
}
},
}

Expand Down
9 changes: 5 additions & 4 deletions docs/wallet.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,11 @@ For example:

## Deleting an Account {#remove}

To irreversibly delete the account from your wallet use `wallet remove <name>`.
For file-based accounts this will delete the file containing the private key
from your disk. For hardware wallet accounts this will delete the Oasis CLI
reference, but the private keys will remain intact on your hardware wallet.
To irreversibly delete the accounts from your wallet use
`wallet remove [names]`. For file-based accounts this will delete the file
containing the private key from your disk. For hardware wallet accounts this
will delete the Oasis CLI reference, but the private keys will remain intact on
your hardware wallet.

For example, let's delete `lenny` account:

Expand Down

0 comments on commit 51d0b13

Please sign in to comment.