From d1ee11628cae5899a4ddca059d471f3c5c12c973 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sun, 18 Jun 2023 22:00:19 +0800 Subject: [PATCH] chore: avoid command panic --- pkg/account/create.go | 15 +++++++-------- pkg/account/delete.go | 11 +++++++---- pkg/account/get.go | 10 ++++++---- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/pkg/account/create.go b/pkg/account/create.go index 5edd5c1..d5a700b 100644 --- a/pkg/account/create.go +++ b/pkg/account/create.go @@ -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 }, } diff --git a/pkg/account/delete.go b/pkg/account/delete.go index 92cf143..7f3cc02 100644 --- a/pkg/account/delete.go +++ b/pkg/account/delete.go @@ -17,6 +17,8 @@ limitations under the License. package account import ( + "fmt" + "github.com/bestchains/bc-cli/pkg/common" "github.com/spf13/cobra" ) @@ -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 }, } diff --git a/pkg/account/get.go b/pkg/account/get.go index e9a4940..c82efdb 100644 --- a/pkg/account/get.go +++ b/pkg/account/get.go @@ -17,6 +17,7 @@ limitations under the License. package account import ( + "fmt" "strings" "github.com/bestchains/bc-cli/pkg/common" @@ -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. @@ -62,7 +65,6 @@ func NewGetAccountCmd(option common.Options) *cobra.Command { // Print the account information. printer.Print(option.Out, accountHeader, print) - return nil }, }