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 ff7b70e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 36 deletions.
12 changes: 12 additions & 0 deletions cmd/common/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
var (
selectedHeight int64
force bool
answerYes bool
)

// HeightFlag is the flag for specifying block height.
Expand All @@ -20,6 +21,9 @@ var HeightFlag *flag.FlagSet
// ForceFlag is a force mode switch.
var ForceFlag *flag.FlagSet

// AnswerYesFlag answers yes to all questions.
var AnswerYesFlag *flag.FlagSet

// GetHeight returns the user-selected block height.
func GetHeight() int64 {
return selectedHeight
Expand All @@ -30,6 +34,11 @@ func IsForce() bool {
return force
}

// IsForce returns force mode.
func GetAnswerYes() bool {
return answerYes
}

// GetActualHeight returns the user-selected block height if explicitly
// specified, or the current latest height.
func GetActualHeight(
Expand All @@ -53,4 +62,7 @@ func init() {

ForceFlag = flag.NewFlagSet("", flag.ContinueOnError)
ForceFlag.BoolVarP(&force, "force", "f", false, "treat safety check errors as warnings")

AnswerYesFlag = flag.NewFlagSet("", flag.ContinueOnError)
AnswerYesFlag.BoolVarP(&answerYes, "yes", "y", false, "answer yes to all questions")
}
79 changes: 44 additions & 35 deletions cmd/wallet/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,51 @@ import (
"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"

"github.com/oasisprotocol/cli/cmd/common"
"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 (
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 !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)
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)
},
}
)

func init() {
rmCmd.Flags().AddFlagSet(common.AnswerYesFlag)
}
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
2 changes: 1 addition & 1 deletion examples/transaction/sign.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Method: staking.Transfer
Body:
To: oasis1qrydpazemvuwtnp3efm7vmfvg3tde044qg6cxwzx
Amount: 1.0 TEST
Nonce: 43
Nonce: 44
Fee:
Amount: 0.0 TEST
Gas limit: 1265
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 ff7b70e

Please sign in to comment.