1#!/usr/bin/env bash
2
3set -euxo pipefail
4
5if [[ $# -lt 1 || $# -gt 2 ]]; then
6 echo "usage: $0 <MAX_SIZE_IN_GB> [SMALL_CLEAN_SIZE_IN_GB]"
7 exit 1
8fi
9
10if ! [[ -d target ]]; then
11 echo "target directory does not exist yet"
12 exit 0
13fi
14
15max_size_gb=$1
16small_clean_size_gb=${2:-}
17
18if [[ -n "${small_clean_size_gb}" && ${small_clean_size_gb} -ge ${max_size_gb} ]]; then
19 echo "error: small clean threshold (${small_clean_size_gb}gb) must be smaller than max size (${max_size_gb}gb)"
20 exit 1
21fi
22
23current_size=$(du -s target | cut -f1)
24current_size_gb=$(( ${current_size} / 1024 / 1024 ))
25
26echo "target directory size: ${current_size_gb}gb. max size: ${max_size_gb}gb"
27
28if [[ ${current_size_gb} -gt ${max_size_gb} ]]; then
29 echo "clearing target directory"
30 shopt -s dotglob
31 rm -rf target/*
32elif [[ -n "${small_clean_size_gb}" && ${current_size_gb} -gt ${small_clean_size_gb} ]]; then
33 echo "running cargo clean --workspace (size above small clean threshold of ${small_clean_size_gb}gb)"
34 cargo clean --workspace
35fi