-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
install_dotfiles.sh
executable file
·100 lines (83 loc) · 2.2 KB
/
install_dotfiles.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
92
93
94
95
96
97
98
99
100
#!/bin/sh
# -e: exit on error
# -u: exit on unset variables
set -eu
log_color() {
color_code="$1"
shift
printf "\033[${color_code}m%s\033[0m\n" "$*" >&2
}
log_red() {
log_color "0;31" "$@"
}
log_blue() {
log_color "0;34" "$@"
}
log_task() {
log_blue "🔃" "$@"
}
log_manual_action() {
log_red "⚠️" "$@"
}
log_error() {
log_red "❌" "$@"
}
error() {
log_error "$@"
exit 1
}
sudo() {
# shellcheck disable=SC2312
if [ "$(id -u)" -eq 0 ]; then
"$@"
else
if ! command sudo --non-interactive true 2>/dev/null; then
log_manual_action "Root privileges are required, please enter your password below"
command sudo --validate
fi
command sudo "$@"
fi
}
git_clean() {
path=$(realpath "$1")
remote="$2"
branch="$3"
log_task "Cleaning '${path}' with '${remote}' at branch '${branch}'"
git="git -C ${path}"
# Ensure that the remote is set to the correct URL
if ${git} remote | grep -q "^origin$"; then
${git} remote set-url origin "${remote}"
else
${git} remote add origin "${remote}"
fi
${git} checkout -B "${branch}"
${git} fetch origin "${branch}"
${git} reset --hard FETCH_HEAD
${git} clean -fdx
unset path remote branch git
}
DOTFILES_REPO_HOST=${DOTFILES_REPO_HOST:-"https://github.com"}
DOTFILES_USER=${DOTFILES_USER:-"felipecrs"}
DOTFILES_REPO="${DOTFILES_REPO_HOST}/${DOTFILES_USER}/dotfiles"
DOTFILES_BRANCH=${DOTFILES_BRANCH:-"master"}
DOTFILES_DIR="${HOME}/.dotfiles"
if ! command -v git >/dev/null 2>&1; then
log_task "Installing git with APT"
sudo apt update
sudo DEBIAN_FRONTEND=noninteractive apt install --yes --no-install-recommends git
fi
if [ -d "${DOTFILES_DIR}" ]; then
git_clean "${DOTFILES_DIR}" "${DOTFILES_REPO}" "${DOTFILES_BRANCH}"
else
log_task "Cloning '${DOTFILES_REPO}' at branch '${DOTFILES_BRANCH}' to '${DOTFILES_DIR}'"
git clone --branch "${DOTFILES_BRANCH}" "${DOTFILES_REPO}" "${DOTFILES_DIR}"
fi
if [ -f "${DOTFILES_DIR}/install.sh" ]; then
INSTALL_SCRIPT="${DOTFILES_DIR}/install.sh"
elif [ -f "${DOTFILES_DIR}/install" ]; then
INSTALL_SCRIPT="${DOTFILES_DIR}/install"
else
error "No install script found in the dotfiles."
fi
log_task "Running '${INSTALL_SCRIPT}'"
exec "${INSTALL_SCRIPT}" "$@"