-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclean-old-results
executable file
·95 lines (80 loc) · 2.28 KB
/
clean-old-results
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
SCRIPT=$(basename ${0})
BASE_DIR=$(dirname "${0}")
function print_incorrect_syntax() {
echo "ERROR: Invalid usage options (use -h for help)"
}
function usage() {
cat <<EOF
Clean old terracumber results.
Syntax:
${SCRIPT} <ARGUMENTS>
Mandatory arguments:
-r, --results_path=<PATH> Path to the terracumber results (can't be /)
Optional arguments:
-s, --save=<NUMBER> The number of newest results to be saved
(20 by default)
-d, --dry_run If present, show a report but do not remove
anything
EOF
}
function is_number() {
local re='^[0-9]+$'
if ! [[ ${1} =~ $re ]] ; then
echo "false"
fi
echo "true"
}
function remove_results() {
local RESULTS_PATH="${1}/*"
local SAVE=${2}
local DRY_RUN=${3}
TOTAL=$(ls -tld ${RESULTS_PATH} | wc -l)
if [ ${TOTAL} -gt ${SAVE} ]; then
if [ "${DRY_RUN}" == "true" ]; then
echo "DRY RUN: There are more than ${SAVE} result folders, would remove all except the ${SAVE} newest..."
else
echo "There are more than ${SAVE} result folders, removing all except the ${SAVE} newest..."
fi
for RESULT in $(ls -td ${RESULTS_PATH} | tail -n +$((${SAVE}+1))); do
if [ "${DRY_RUN}" == "true" ]; then
echo "DRY RUN: Would remove ${RESULT}"
else
echo "Removing ${RESULT}..."
rm -rf ${RESULT}
fi
done
else
echo "There are ${SAVE} or less result folders"
fi
}
# read the options
ARGS=$(getopt -o h,r:,s:,d --long help,results_path:,save:,dry_run -n "${SCRIPT}" -- "$@")
if [ $? -ne 0 ];
then
print_incorrect_syntax
exit 1
fi
eval set -- "${ARGS}"
while true ; do
case "${1}" in
-h|--help) usage; exit 1;;
-r|--results_path) RESULTS_PATH="$(echo ${2}|sed 's:/*$::')"; shift 2;;
-s|--save) SAVE="${2}"; shift 2 ;;
-d|--dry_run) DRY_RUN="true"; shift 1 ;;
--) shift ; break ;;
*) print_incorrect_syntax; exit 1;;
esac
done
if [ "${SAVE}" == "" ]; then
SAVE=20
fi
if [ "${RESULTS_PATH}" == "" -o "$(is_number ${SAVE})" != "true" ]; then
print_incorrect_syntax
exit 1
fi
if [ ! -d ${RESULTS_PATH} ]; then
echo "Directory ${RESULTS_PATH} does not exist!"
exit 1
fi
remove_results ${RESULTS_PATH} ${SAVE} ${DRY_RUN}