Skip to content

Commit

Permalink
refactor(cli): factory pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
czar0 committed Dec 27, 2023
1 parent 37f0a58 commit 0bac69a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 30 deletions.
11 changes: 8 additions & 3 deletions cmd/ethkit/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@ import (
)

func init() {
balance := &balance{}
rootCmd.AddCommand(NewBalanceCmd())
}

func NewBalanceCmd() *cobra.Command {
c := &balance{}
cmd := &cobra.Command{
Use: "balance [account]",
Short: "Get the balance of an account",
Aliases: []string{"b"},
Args: cobra.ExactArgs(1),
RunE: balance.Run,
RunE: c.Run,
}

cmd.Flags().StringP("block", "B", "latest", "The block height to query at")
cmd.Flags().BoolP("ether", "e", false, "Format the balance in ether")
cmd.Flags().StringP("rpc-url", "r", "", "The RPC endpoint to the blockchain node to interact with")

rootCmd.AddCommand(cmd)
return cmd
}

type balance struct {
Expand Down
36 changes: 9 additions & 27 deletions cmd/ethkit/balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,11 @@ import (
// "strconv"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func NewBalanceCommand() *cobra.Command {
balance := &balance{}

cmd := &cobra.Command{
Use: "balance [account]",
Short: "Get the balance of an account",
Args: cobra.ExactArgs(1),
RunE: balance.Run,
}

cmd.Flags().StringP("block", "B","latest", "")
cmd.Flags().BoolP("ether", "e", false, "")
cmd.Flags().StringP("rpc-url", "r", "", "")

return cmd
}

func TestOKExecuteBalanceCommand(t *testing.T) {
cmd := NewBalanceCommand()
func TestOKExecuteBalanceCmd(t *testing.T) {
cmd := NewBalanceCmd()
b := new(bytes.Buffer)
cmd.SetOut(b)
cmd.SetErr(b)
Expand All @@ -41,7 +23,7 @@ func TestOKExecuteBalanceCommand(t *testing.T) {

// TODO: Fix bad output redirection that makes assert to fail
func TestOKBalanceValidWei(t *testing.T) {
cmd := NewBalanceCommand()
cmd := NewBalanceCmd()
b := new(bytes.Buffer)
cmd.SetOut(b)
cmd.SetArgs([]string{"0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742", "--rpc-url", "https://nodes.sequence.app/sepolia"})
Expand All @@ -51,7 +33,7 @@ func TestOKBalanceValidWei(t *testing.T) {

// TODO: Fix bad output redirection that makes assert to fail
func TestOKBalanceValidEther(t *testing.T) {
cmd := NewBalanceCommand()
cmd := NewBalanceCmd()
b := new(bytes.Buffer)
cmd.SetOut(b)
cmd.SetErr(b)
Expand All @@ -61,7 +43,7 @@ func TestOKBalanceValidEther(t *testing.T) {
}

func TestFailInvalidAddress(t *testing.T) {
cmd := NewBalanceCommand()
cmd := NewBalanceCmd()
b := new(bytes.Buffer)
cmd.SetOut(b)
cmd.SetErr(b)
Expand All @@ -71,7 +53,7 @@ func TestFailInvalidAddress(t *testing.T) {
}

func TestFailInvalidRPC(t *testing.T) {
cmd := NewBalanceCommand()
cmd := NewBalanceCmd()
b := new(bytes.Buffer)
cmd.Println(b)
cmd.SetErr(b)
Expand All @@ -81,7 +63,7 @@ func TestFailInvalidRPC(t *testing.T) {
}

func TestFailNotExistingBlockHeigh(t *testing.T) {
cmd := NewBalanceCommand()
cmd := NewBalanceCmd()
b := new(bytes.Buffer)
cmd.SetOut(b)
cmd.SetErr(b)
Expand All @@ -91,7 +73,7 @@ func TestFailNotExistingBlockHeigh(t *testing.T) {
}

func TestFailNotAValidStringBlockHeigh(t *testing.T) {
cmd := NewBalanceCommand()
cmd := NewBalanceCmd()
b := new(bytes.Buffer)
cmd.SetOut(b)
cmd.SetErr(b)
Expand All @@ -101,7 +83,7 @@ func TestFailNotAValidStringBlockHeigh(t *testing.T) {
}

func TestFailNotAValidNumberBlockHeigh(t *testing.T) {
cmd := NewBalanceCommand()
cmd := NewBalanceCmd()
b := new(bytes.Buffer)
cmd.SetOut(b)
cmd.SetErr(b)
Expand Down

0 comments on commit 0bac69a

Please sign in to comment.