diff --git a/cmd/list.go b/cmd/list.go index 72a9cf7..951d514 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -15,7 +15,12 @@ var listCmd = &cobra.Command{ Use: "list", Short: "List wallets or accounts within a wallet", Run: func(cmd *cobra.Command, args []string) { - if walletIndex < 0 { + if walletAccount != "" { + // For when a specific account is specified. A single account is returned. + rpcClient := rpc.Client{URL: rpcURL} + getBalanceAndPrint(walletAccount, rpcClient) + } else if walletIndex < 0 { + // For when nothing is specified, shows the number of accounts in each wallet. for i, wi := range wallets { n := len(wi.Accounts) switch n { @@ -26,6 +31,8 @@ var listCmd = &cobra.Command{ } } } else { + // For when a specific wallet is specified, shows the balance of all accounts + // in that wallet. checkWalletIndex() var accounts []string for address := range wallets[walletIndex].Accounts { @@ -35,12 +42,9 @@ var listCmd = &cobra.Command{ rpcClient := rpc.Client{URL: rpcURL} var balanceSum, pendingSum big.Int for _, address := range accounts { - balance, pending, err := rpcClient.AccountBalance(address) - fatalIf(err) + balance, pending := getBalanceAndPrint(address, rpcClient) balanceSum.Add(&balanceSum, &balance.Int) pendingSum.Add(&pendingSum, &pending.Int) - fmt.Print(address) - printAmounts(&balance.Int, &pending.Int) } if len(accounts) > 1 { fmt.Print(strings.Repeat(" ", 61), "Sum:") @@ -50,6 +54,14 @@ var listCmd = &cobra.Command{ }, } +func getBalanceAndPrint(account string, rpcClient rpc.Client) (balance, pending *rpc.RawAmount) { + balance, pending, err := rpcClient.AccountBalance(account) + fatalIf(err) + fmt.Print(account) + printAmounts(&balance.Int, &pending.Int) + return balance, pending +} + func printAmounts(balance, pending *big.Int) { if balance.Sign() > 0 { fmt.Printf(" %s", util.NanoAmount{Raw: balance})