diff --git a/cmd/wallet/remove.go b/cmd/wallet/remove.go index 04e6bb34..b1227bd4 100644 --- a/cmd/wallet/remove.go +++ b/cmd/wallet/remove.go @@ -5,43 +5,63 @@ import ( "github.com/AlecAivazis/survey/v2" "github.com/spf13/cobra" + flag "github.com/spf13/pflag" "github.com/oasisprotocol/cli/config" ) -var rmCmd = &cobra.Command{ - Use: "remove ", - Aliases: []string{"rm"}, - Short: "Remove an existing account", - Args: cobra.ExactArgs(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)) - } - - 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) - prompt := &survey.Input{ - Message: fmt.Sprintf("Enter '%s' (without quotes) to confirm removal:", confirmText), - } - err := survey.AskOne(prompt, &result) - cobra.CheckErr(err) - - if result != confirmText { - cobra.CheckErr("Aborted.") - } - - err = cfg.Wallet.Remove(name) - cobra.CheckErr(err) - - err = cfg.Save() - cobra.CheckErr(err) - }, +var ( + yes bool + + rmCmd = &cobra.Command{ + Use: "remove ", + Aliases: []string{"rm"}, + Short: "Remove an existing account", + Args: cobra.ExactArgs(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)) + } + + if !yes { + 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) + prompt := &survey.Input{ + Message: fmt.Sprintf("Enter '%s' (without quotes) to confirm removal:", confirmText), + } + err := survey.AskOne(prompt, &result) + cobra.CheckErr(err) + + if result != confirmText { + cobra.CheckErr("Aborted.") + } + + err = cfg.Wallet.Remove(name) + cobra.CheckErr(err) + + err = cfg.Save() + cobra.CheckErr(err) + + } else { + err := cfg.Wallet.Remove(name) + cobra.CheckErr(err) + + err = cfg.Save() + cobra.CheckErr(err) + } + }, + } +) + +func init() { + yesFlag := flag.NewFlagSet("", flag.ContinueOnError) + yesFlag.BoolVarP(&yes, "yes", "y", false, "confirm all questions") + rmCmd.Flags().AddFlagSet(yesFlag) } diff --git a/docs/wallet.md b/docs/wallet.md index e6ab7be6..99866b11 100644 --- a/docs/wallet.md +++ b/docs/wallet.md @@ -241,6 +241,11 @@ logan ledger (ed25519-legacy:0) oasis1qpl4axynedmdrrgrg7dpw3yxc4 oscar (*) file (ed25519-raw) oasis1qp87hflmelnpqhzcqcw8rhzakq4elj7jzv090p3e ``` +You can also delete accounct in non-interactive mode format by passing the +`-y` parameter: + +![code shell](../examples/wallet/remove-y.in.static) + ## Set Default Account {#set-default} To change your default account, use `wallet set-default ` and the diff --git a/examples/wallet/remove-y.in.static b/examples/wallet/remove-y.in.static new file mode 100644 index 00000000..220e5fd1 --- /dev/null +++ b/examples/wallet/remove-y.in.static @@ -0,0 +1 @@ +oasis wallet remove lenny -y diff --git a/examples/wallet/remove-y.out.static b/examples/wallet/remove-y.out.static new file mode 100644 index 00000000..e69de29b