| 1 | #! /bin/bash |
| 2 | |
| 3 | total_cargo_build_space() { |
| 4 | local -r dir="$1" |
| 5 | local -r list="$2" |
| 6 | |
| 7 | local cargo_toml_path |
| 8 | local target_path |
| 9 | |
| 10 | find "$dir" -type f -name Cargo.toml \ |
| 11 | | while read -r cargo_toml_path |
| 12 | do |
| 13 | target_path=$(dirname "$cargo_toml_path")/target; |
| 14 | if test -d "$target_path" |
| 15 | then |
| 16 | if [[ "$list" -eq 1 ]] |
| 17 | then |
| 18 | du -sh "$target_path" |
| 19 | else |
| 20 | du -s "$target_path" |
| 21 | fi |
| 22 | fi |
| 23 | done \ |
| 24 | | if [[ "$list" -eq 1 ]] |
| 25 | then |
| 26 | sort -h -k 1 |
| 27 | else |
| 28 | awk '{tot += $1} END {print tot * 1024}' | numfmt --to=iec |
| 29 | fi |
| 30 | } |
| 31 | |
| 32 | main() { |
| 33 | local dir="$HOME" |
| 34 | local list=0 |
| 35 | local arg |
| 36 | |
| 37 | while : |
| 38 | do |
| 39 | arg="$1" |
| 40 | case "$arg" in |
| 41 | '') |
| 42 | break |
| 43 | ;; |
| 44 | -l | --list) |
| 45 | list=1 |
| 46 | shift |
| 47 | ;; |
| 48 | -*) |
| 49 | printf 'Unknown flag: "%s"\n' "$arg" >&2 |
| 50 | exit 1 |
| 51 | ;; |
| 52 | *) |
| 53 | dir="$arg" |
| 54 | shift |
| 55 | esac |
| 56 | done |
| 57 | |
| 58 | total_cargo_build_space "$dir" "$list" |
| 59 | } |
| 60 | |
| 61 | main "$@" |