Skip to content

Commit

Permalink
chore: avoid command panic
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Jun 18, 2023
1 parent c587a29 commit d1ee116
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
15 changes: 7 additions & 8 deletions pkg/account/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,29 @@ func NewCreateAccountCmd(option common.Options) *cobra.Command {
// RunE is the Cobra command's main function.
// It generates a new account using the provided private key or generates a new one if none is provided.
// It then encodes the private key and writes the account object to a file in the wallet directory.
RunE: func(cmd *cobra.Command, args []string) error {
var (
err error
)
Run: func(cmd *cobra.Command, args []string) {
// NewLocalWallet creates a new local wallet with a directory path.
wallet, err := NewLocalWallet(walletDir)
if err != nil {
return err
fmt.Fprintln(option.ErrOut, err)
return
}

// NewAccount creates a new account.
account, err := NewAccount()
if err != nil {
return err
fmt.Fprintln(option.ErrOut, err)
return
}

// StoreAccount stores the account in the wallet.
err = wallet.StoreAccount(account)
if err != nil {
return err
fmt.Fprintln(option.ErrOut, err)
return
}

fmt.Fprintf(option.Out, "account/%s created\n", account.Address)
return nil
},
}

Expand Down
11 changes: 7 additions & 4 deletions pkg/account/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package account

import (
"fmt"

"github.com/bestchains/bc-cli/pkg/common"
"github.com/spf13/cobra"
)
Expand All @@ -33,19 +35,20 @@ func NewDeleteAccountCmd(option common.Options) *cobra.Command {
Short: "Delete the account according to the wallet information.",

// RunE is the function that runs when the command is called.
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
// Create a new local wallet.
wallet, err := NewLocalWallet(walletDir)
if err != nil {
return err
fmt.Fprintln(option.ErrOut, err)
return
}

// Delete the specified accounts.
err = wallet.DeleteAccounts(args...)
if err != nil {
return err
fmt.Fprintln(option.ErrOut, err)
return
}
return nil
},
}

Expand Down
10 changes: 6 additions & 4 deletions pkg/account/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package account

import (
"fmt"
"strings"

"github.com/bestchains/bc-cli/pkg/common"
Expand All @@ -41,17 +42,19 @@ func NewGetAccountCmd(option common.Options) *cobra.Command {
// Remove trailing slash from wallet path.
walletDir = strings.TrimSuffix(walletDir, "/")
},
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
// Create a new local wallet.
wallet, err := NewLocalWallet(walletDir)
if err != nil {
return err
fmt.Fprintln(option.ErrOut, err)
return
}

// Get a list of accounts from the wallet.
accounts, err := wallet.ListAccounts()
if err != nil {
return err
fmt.Fprintln(option.ErrOut, err)
return
}

// Create a list of account printers.
Expand All @@ -62,7 +65,6 @@ func NewGetAccountCmd(option common.Options) *cobra.Command {

// Print the account information.
printer.Print(option.Out, accountHeader, print)
return nil
},
}

Expand Down

0 comments on commit d1ee116

Please sign in to comment.