-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcommon.sh
More file actions
37 lines (31 loc) · 891 Bytes
/
common.sh
File metadata and controls
37 lines (31 loc) · 891 Bytes
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
# shellcheck shell=bash
# Strict bash
set -euo pipefail
COMMAND_NAME="$1"
OUTPUT="${2-}"
if [[ -n "$OUTPUT" ]]; then
_OUTPUT_ABSOLUTE=$(realpath "$OUTPUT")
fi
TEMPORARIES_LOG=$(mktemp --tmpdir tmp."$COMMAND_NAME"-temp-log.XXXXXXXXXX)
function temp() {
NEW_FILE="$(mktemp --tmpdir tmp."$COMMAND_NAME".XXXXXXXXXX "$@")"
echo "$NEW_FILE" >> "$TEMPORARIES_LOG"
echo "$NEW_FILE"
}
function at_exit() {
# Cleanup all temporary files created through temp
while IFS= read -r TEMPORARY; do
rm -rf "$TEMPORARY"
done < "$TEMPORARIES_LOG"
rm -f "$TEMPORARIES_LOG"
# If this command has any output, ensure it has been produced
if [[ -n "$OUTPUT" && ! -e "$_OUTPUT_ABSOLUTE" ]]; then
echo "Output not produced" > /dev/stderr
exit 1
fi
}
trap at_exit EXIT
if [[ $OUTPUT == */ ]]; then
rm -rf "$OUTPUT"
mkdir -p "$OUTPUT"
fi