-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcache-tool.sh
executable file
·74 lines (65 loc) · 1.55 KB
/
cache-tool.sh
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
#!/bin/sh
# ==== Functions
function usage() {
echo ""
echo "Usage: $0 action [name:dir] [name:dir] ..."
echo ""
echo "Parameters:"
echo " action: Either 'collect' or 'extract'"
echo " name: A name given to the cache object"
echo " directory: The directory that's be cached under the given name"
echo ""
echo "Example:"
echo " $0 collect composer:~/.composer"
echo ""
exit 1
}
function collectCache() {
IFS=:
set $1
eval dir=$2
if [ ! -d $dir ]; then
echo " ! Directory $2 does not exist, skipping..."
return
fi
echo " - Collecting $2 into .cache/$1...";
cp -R $dir .cache/$1
}
function extractCache() {
IFS=:
set $1
eval dir=$2
if [ ! -d .cache/$1 ]; then
echo " ! Cache directory .cache/$1 does not exist, skipping..."
return
fi
echo " - Extracting .cache/$1 into $2...";
mkdir -p $dir && rm -rf $dir
cp -R .cache/$1 $dir
}
# ==== Start of the program
if [ $# -lt 2 ]; then
usage;
fi
if [[ "$1" != "collect" && "$1" != "extract" ]]; then
usage;
fi
ACTION=$1; shift;
if [[ "$ACTION" == "collect" ]]; then
echo "Collecting the cache..."
echo " - Creating the .cache directory..."
mkdir -p .cache;
for cache in "$@"; do
collectCache "$cache"
done
echo "Done!"
else
echo "Extracting the cache..."
mkdir -p .cache;
for cache in "$@"; do
extractCache "$cache"
done
echo " - Removing the .cache directory..."
rm -rf .cache
echo "Done!"
fi