diff --git a/cmd/wallet/remove.go b/cmd/wallet/remove.go index 9108f1cc..056d2afa 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" @@ -11,17 +12,22 @@ import ( ) 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 +35,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 accounts: %s", strings.Join(uniqueArgs, ", ")) prompt := &survey.Input{ Message: fmt.Sprintf("Enter '%s' (without quotes) to confirm removal:", confirmText), } @@ -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) } 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: