/
install-cron.sh
executable file
·92 lines (74 loc) · 1.64 KB
/
install-cron.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env bash
#
# Installs a cron as current user to keep Bart up to date
##
function showHelp() {
echo "
Usage: $0 [-v]
$0 --help
Installs cron to hard reset the current Bart checkout.
...Sorry for now, you'll have to manually remove it (crontab -e)
-v Verbose output
--help Show this help
";
exit $1;
}
function parseArgs() {
while [ $# -gt 0 ]
do
case "$1" in
-v) verbose=1 ;;
--help) showHelp ;;
-*)
echo "Invalid option: \"$1\"" >&2;
showHelp 1
;;
*)
if [ $# -gt 0 ]; then
echo "$0 takes no arguments" >&2;
showHelp 1
fi
;;
esac
shift
done
}
##
# Set global variables
##
function configureVars() {
# Ensure we're not in a root directory so that git-dir will
# ...be a full path
cd $(dirname $0)
GIT_BART="$(git rev-parse --git-dir)"
}
function assertPermissions() {
if ! touch $GIT_BART; then
echo >&2 "User executing script must have write permissions to Bart checkout"
echo >&2 "Install failed"
exit 1
fi
}
function installCron() {
declare git="git --git-dir=$GIT_BART"
declare resetGit="$git fetch origin && $git reset --hard origin/master"
declare cronCmd="
# Fetch and reset bart against upstream
0,15,30,45 * * * * $resetGit"
[[ $verbose -eq 1 ]] && {
echo "DEBUG Adding new cron: $cronCmd"
}
# Append this command to existing list of cron commands
if echo "$(crontab -l) $cronCmd" | crontab - ; then
echo ""
echo "Successfully registered cron for $USER"
echo "View existing crons using crontab -l"
else
echo "Failed to register cron"
exit 1
fi
}
parseArgs "$@"
configureVars
assertPermissions
installCron