From 51d0b137ac839944c124a9b1dc147bbccc233f05 Mon Sep 17 00:00:00 2001 From: amela Date: Fri, 19 Apr 2024 10:20:09 +0200 Subject: [PATCH] feat(cmd/wallet): Support removal of multiple accounts --- cmd/wallet/remove.go | 44 ++++++++++++++++++++++++++++++++------------ docs/wallet.md | 9 +++++---- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/cmd/wallet/remove.go b/cmd/wallet/remove.go index 9108f1cc..38438a10 100644 --- a/cmd/wallet/remove.go +++ b/cmd/wallet/remove.go @@ -2,6 +2,7 @@ package wallet import ( "fmt" + "strings" "github.com/AlecAivazis/survey/v2" "github.com/spf13/cobra" @@ -10,18 +11,35 @@ 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 ", + Use: "remove [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() { @@ -29,7 +47,7 @@ var rmCmd = &cobra.Command{ 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), } @@ -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) + } }, } diff --git a/docs/wallet.md b/docs/wallet.md index 99866b11..4ae0c7e1 100644 --- a/docs/wallet.md +++ b/docs/wallet.md @@ -214,10 +214,11 @@ For example: ## Deleting an Account {#remove} -To irreversibly delete the account from your wallet use `wallet remove `. -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: