Skip to content

Commit

Permalink
wallet: Add -y flag to oasis wallet remove
Browse files Browse the repository at this point in the history
  • Loading branch information
amela committed Apr 5, 2024
1 parent 080e06c commit 0e0fb40
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 35 deletions.
90 changes: 55 additions & 35 deletions cmd/wallet/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <name>",
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 <name>",
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)
}
5 changes: 5 additions & 0 deletions docs/wallet.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <name>` and the
Expand Down
1 change: 1 addition & 0 deletions examples/wallet/remove-y.in.static
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
oasis wallet remove lenny -y
Empty file.

0 comments on commit 0e0fb40

Please sign in to comment.