Skip to content

Commit

Permalink
refactor(cli): const flags and cobra stdout support
Browse files Browse the repository at this point in the history
  • Loading branch information
czar0 committed Dec 29, 2023
1 parent 0bac69a commit 8952ef8
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions cmd/ethkit/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import (
"github.com/0xsequence/ethkit/go-ethereum/params"
)

const (
flagBalanceBlock = "block"
flagBalanceEther = "ether"
flagBalanceRpcUrl = "rpc-url"
)

func init() {
rootCmd.AddCommand(NewBalanceCmd())
}
Expand All @@ -29,9 +35,9 @@ func NewBalanceCmd() *cobra.Command {
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")
cmd.Flags().StringP(flagBalanceBlock, "B", "latest", "The block height to query at")
cmd.Flags().BoolP(flagBalanceEther, "e", false, "Format the balance in ether")
cmd.Flags().StringP(flagBalanceRpcUrl, "r", "", "The RPC endpoint to the blockchain node to interact with")

return cmd
}
Expand All @@ -41,15 +47,15 @@ type balance struct {

func (c *balance) Run(cmd *cobra.Command, args []string) error {
fAccount := cmd.Flags().Args()[0]
fBlock, err := cmd.Flags().GetString("block")
fBlock, err := cmd.Flags().GetString(flagBalanceBlock)
if err != nil {
return err
}
fEther, err := cmd.Flags().GetBool("ether")
fEther, err := cmd.Flags().GetBool(flagBalanceEther)
if err != nil {
return err
}
fRpc, err := cmd.Flags().GetString("rpc-url")
fRpc, err := cmd.Flags().GetString(flagBalanceRpcUrl)
if err != nil {
return err
}
Expand Down Expand Up @@ -88,9 +94,9 @@ func (c *balance) Run(cmd *cobra.Command, args []string) error {

if fEther {
bal := weiToEther(wei)
fmt.Println(bal, "ether")
fmt.Fprintln(cmd.OutOrStdout(), bal, "ether")
} else {
fmt.Println(wei, "wei")
fmt.Fprintln(cmd.OutOrStdout(), wei, "wei")
}

return nil
Expand Down

0 comments on commit 8952ef8

Please sign in to comment.