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

feat(cmd/wallet): Support removal of multiple accounts #222

Merged
merged 1 commit into from
Apr 23, 2024
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
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 @@ -11,25 +12,30 @@ import (
)

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 accounts: %s", strings.Join(uniqueArgs, ", "))
prompt := &survey.Input{
Message: fmt.Sprintf("Enter '%s' (without quotes) to confirm removal:", confirmText),
}
Expand All @@ -41,14 +47,28 @@ 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)
}
},
}

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

func init() {
rmCmd.Flags().AddFlagSet(common.AnswerYesFlag)
}
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
Loading