-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup-utils.sh
64 lines (56 loc) · 1.36 KB
/
setup-utils.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
#!/bin/sh
is_linux() {
[[ "$(uname)" == "Linux" ]]
}
is_mac() {
# [[ "$OSTYPE" == "darwin"* ]]
[[ "$(uname)" == "Darwin" ]]
}
if is_mac; then
export OS_PYTHON="python3"
export OS_PIP="pip3"
else
export OS_PYTHON="python"
export OS_PIP="pip"
fi
confirm() {
action=$1
# TODO: maybe want user to confirm by pressing enter as well?
read -p "Would you like to $action? (y/n): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
return 0
elif [[ $REPLY =~ ^[Nn]$ ]]; then
return 1
else
confirm "$action"
fi
}
check_dependency() {
command -v "$1" > /dev/null 2>&1
}
backup_dst_config() {
dst=$1
as_user=$2
if [ -e "$dst" ]; then
backup="$dst.$(date +%s)"
echo "Backing up $dst to $backup"
$as_user mv "$dst" "$backup"
fi
}
link_config() {
src=$1
dst=$2
as_root=${3:-false}
copy_only=${4:-false}
[[ $as_root = true ]] && as_user="sudo" || as_user=""
[[ $copy_only = true ]] && command="cp" || command="ln -s"
mkdir -p "$(dirname "$dst")"
backup_dst_config "$dst" "$as_user"
$as_user $command "$src" "$dst"
}
get_latest_release() {
curl --silent "https://api.github.com/repos/$1/releases/latest" | # Get latest release from GitHub api
grep '"tag_name":' | # Get tag line
sed -E 's/.*"([^"]+)".*/\1/' | tr -d '\n' # Pluck JSON value
}