-
Notifications
You must be signed in to change notification settings - Fork 5
/
common.sh
62 lines (52 loc) · 1.1 KB
/
common.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
function die {
echo $@ 1>&2
exit 1
return 1
}
function tool_exists {
which $1 &>/dev/null
if [[ $? -ne 0 ]]; then
die "$1 is not installed (hint: run setup_packages.sh first)"
fi
}
function check_failure {
local rc=${1:-$?}
test $? -eq 0 || die " = Failed!"
return 1
}
function echo_ok {
echo " = OK!"
}
function is_sudo {
[ "$(id -u)" -eq 0 ] || return 1
[ "$SUDO_USER" != "" ] || return 1
return 0
}
function ensure_sudo {
is_sudo || die "You must run this script as sudo!"
}
function run_as_sudo {
ensure_sudo
sudo -H $*
}
function run_as_user {
is_sudo && {
sudo -H -u $SUDO_USER $*
} || {
$*
}
}
function sync_git {
local url="$1"
local dir="${2:-$(basename $url)}"
echo " - Synchronising $url ..."
local exists="false"
(test -d "$dir/.git" &&
git --git-dir "$dir/.git" status &>/dev/null) && {
exists="true"
(cd "$dir" && git pull 1>/dev/null)
} || (git clone "$url" "$dir" 1>/dev/null)
check_failure
test "$exists" == "true" && return 2
return 0
}