-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup
executable file
·195 lines (169 loc) · 4.49 KB
/
setup
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env bash
DOTFILES_REPO_URL="https://github.com/guerrero/dotfiles.git"
DOTFILES_PATH="$HOME/.dotfiles"
function main() {
ask_for_sudo
install_xcode_command_line_tools
clone_dotfiles_repo
install_homebrew
install_ansible
run_modules_install_tasks
}
function ask_for_sudo() {
info "Checking for \`sudo\` access (which may request your password)..."
if sudo --validate &> /dev/null; then
# Update `sudo` timestamp until script has finished
# More info: https://gist.github.com/cowboy/3118588
while true; do
sudo --non-interactive true;
sleep 60;
kill -0 "$$" || exit
done 2>/dev/null &
success "Running script using \`sudo\` credentials..."
else
error "Request for \`sudo\` access failed"
exit 1
fi
}
function install_xcode_command_line_tools() {
info "Installing Xcode command line tools"
if [[ ! -d "$('xcode-select' -print-path 2>/dev/null)" ]]; then
xcode-select --install
while true; do
if [[ -d "$('xcode-select' -print-path 2>/dev/null)" ]]; then
success "Xcode command line tools installed successfully"
finish_installing_message
break
else
init_installing_message "Installing"
fi
done
else
success "Xcode command line tools are already installed"
fi
}
function clone_dotfiles_repo() {
info "Cloning dotfiles repository into ${DOTFILES_PATH}"
if [[ ! -e "$DOTFILES_PATH" ]]; then
if git clone $DOTFILES_REPO_URL "$DOTFILES_PATH"; then
success "Repository cloned successfully"
else
error "Cloning into ${DOTFILES_PATH} failed"
exit 1
fi
else
info "${DOTFILES_PATH} already exists"
update_local_dotfiles $DOTFILES_PATH
fi
}
function update_local_dotfiles() {
info "Pulling latest changes in dotfiles repository"
if git -C $1 pull origin main &> /dev/null; then
success "Newer repository changes pulled successfuly"
return
else
error "Please pull the latest changes manually"
exit 1
fi
}
function install_homebrew() {
info "Installing Homebrew"
if [[ ! "$(type -P brew)" ]]; then
url=https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh
if yes | /bin/bash -c "$(curl -fsSL ${url})"; then
eval "$(/opt/homebrew/bin/brew shellenv)"
success "Homebrew installed successfully"
else
error "Homebrew installation failed"
exit 1
fi
else
success "Homebrew is already installed"
fi
}
function install_ansible() {
info "Installing Ansible"
if [[ ! "$(type -P ansible)" ]]; then
if /opt/homebrew/bin/brew install ansible; then
success "Ansible installed successfully"
else
error "Ansible installation failed"
exit 1
fi
else
success "Ansible is already installed"
fi
}
function run_modules_install_tasks() {
info "Executing Ansible setup tasks"
/opt/homebrew/bin/ansible-playbook \
-i "$DOTFILES_PATH/inventory" \
"$DOTFILES_PATH/main.yml" \
--extra-vars "command=setup" \
--ask-become-pass \
|| exit 1
}
#
# Utilities
#
function init_installing_message() {
text[0]="$1 \r"
text[1]="$1. \r"
text[2]="$1.. \r"
text[3]="$1...\r"
for i in "${text[@]}"
do
echo -ne "$i"
sleep 1
done
}
function finish_installing_message() {
echo -ne '\n'
}
# Adapted from https://github.com/Sajjadhosn/dotfiles
function colored_echo() {
local color="$1";
local exp="$2";
local arrow="$3";
if ! [[ $color =~ '^[0-9]$' ]] ; then
case $(echo $color | tr '[:upper:]' '[:lower:]') in
black) color=0 ;;
red) color=1 ;;
green) color=2 ;;
yellow) color=3 ;;
blue) color=4 ;;
magenta) color=5 ;;
cyan) color=6 ;;
white|*) color=7 ;; # white or invalid color
esac
fi
tput bold;
tput setaf "$color";
echo "$arrow $exp";
tput sgr0;
}
function info() {
colored_echo blue "$1" "==>"
}
function success() {
colored_echo green "$1" "==>"
}
function error() {
colored_echo red "$1" "==>"
}
main "$@"
unset -f \
colored_echo \
info \
success \
error \
init_installing_message \
finish_installing_message \
ask_for_sudo \
install_xcode_command_line_tools \
clone_dotfiles_repo \
update_local_dotfiles \
install_homebrew \
install_ansible \
run_modules_install_tasks \
main