-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
install_dotfiles.sh
executable file
·98 lines (81 loc) · 2.06 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
#!/usr/bin/env 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:-"ulises-jeremias"}
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
error "Git is not installed"
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}" "$@"