-
Notifications
You must be signed in to change notification settings - Fork 1
/
balance
executable file
·62 lines (46 loc) · 1.21 KB
/
balance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash
set -euo pipefail
source ${BASH_SOURCE%/*}/.helpers.sh
usage() {
cat<<-EOF
$0 <address> [<address>, ...]
Check the current balance of accounts in the ledger and the balance if all
pending transactions.
--help -h Print this help message.
EOF
}
readonly balance_filter='.[$acc] // 0'
readonly debit_filter='
map(select(.from == "\($acc)"))
| map(.amount)
| add
// 0'
readonly credit_filter='
map(select(.to == "\($acc)"))
| map(.amount)
| add
// 0'
main() {
accounts=()
while [ $# -gt 0 ]; do
case "$1" in
--help|-h) usage ; exit 0 ; ;;
-*) echo "unknown flag $1 (-h for help)" >&2 ; exit 1 ; ;;
*) accounts+=("$1"); shift ;;
esac
done
table ${accounts[@]}| column -t -s' '
}
table() {
echo "Account Balance Pending Balance"
for acc in $@; do
ledger_balance=$(jq -er --arg acc "$acc" "$balance_filter" "$ledger")
# Create pending if it doesn't exist.
[ -s "$pending" ] || touch "$pending"
debit=$(jq -er --slurp --arg acc "$acc" "$debit_filter" "$pending")
credit=$(jq -er --slurp --arg acc "$acc" "$credit_filter" "$pending")
total=$((ledger_balance - debit + credit))
echo "$acc: $ledger_balance $total"
done
}
main "$@"