forked from CodelyTV/dotly
-
Notifications
You must be signed in to change notification settings - Fork 1
/
restorer
executable file
·527 lines (461 loc) · 16.5 KB
/
restorer
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#!/usr/bin/env bash
set -euo pipefail
##? Setups the environment
##?
##? Usage:
##? install [-c | --continue]
##?
##? Options:
##? -h --help Prints this help
##? -v --version Prints this script version
##? -c --continue Continue previous install withour cloning again your
##? dotfiles if they exists. Useful if previous restore fails.
##?
# Script variables
SCRIPT_NAME="dot dotfiles recovery"
SCRIPT_VERSION="v1.0.0"
# Default values
continue=false
# Arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--help | -h)
cat << EOF
Usage:
install [-c | --continue]
Options:
-h --help Prints this help
-v --version Prints this script version
-c --continue Continue previous install withour cloning again your
dotfiles if they exists. Useful if previous restore fails.
EOF
exit 0
;;
--version | -v)
echo "$SCRIPT_NAME $SCRIPT_VERSION"
echo
exit 0
;;
--continue | -c)
continue=true
;;
*) ;;
esac
done
ICLOUD_PATH="$HOME/Library/Mobile\ Documents/com~apple~CloudDocs/"
IS_ICLOUD_DOTFILES=false
# Check OSX
# Offer restoration from iCloud
# fzf iCloud subfolders
# Create a symbolic link in $HOME/.dotfiles
# Execute dot core install and all other stuff
DOTLY_LOG_FILE=${DOTLY_LOG_FILE:-$HOME/dotly.log}
export DOTLY_ENV=${DOTLY_ENV:-PROD}
export DOTLY_INSTALLER=true
red='\033[0;31m'
green='\033[0;32m'
purple='\033[0;35m'
normal='\033[0m'
_w() {
local -r text="${1:-}"
echo -e "$text"
}
_a() { _w " > $1"; }
_e() { _a "${red}$1${normal}"; }
_s() { _a "${green}$1${normal}"; }
_q() { read -rp "🤔 $1 : " "$2"; }
_pk() { read -rp "Press a key to ${1}... 👇" "REPLY"; }
_log() {
log_name="$1"
current_date=$(date "+%Y-%m-%d %H:%M:%S")
touch "$DOTLY_LOG_FILE"
echo "----- $current_date - $log_name -----" >> "$DOTLY_LOG_FILE"
while IFS= read -r log_message; do
echo "$log_message" >> "$DOTLY_LOG_FILE"
done
echo "" >> "$DOTLY_LOG_FILE"
}
current_timestamp() { date +%s; }
backup_dotfiles_dir() {
if [ -d "${1:-}" ]; then
local -r backup_path="$1.$(current_timestamp).back"
_e "The path '$1' already exist"
_s "Creating a backup in '$backup_path'"
mv "$1" "$backup_path"
else
_a "Ok! dotfiles will be located in: ${purple}$DOTFILES_PATH${normal}"
fi
[[ -n "${1:-}" ]] && mkdir -p "$(dirname "${1:-}")"
}
command_exists() {
type "$1" > /dev/null 2>&1
}
install_brew_osx() {
if [[ "$OSTYPE" =~ ^[darwin] ]] && ! command_exists brew; then
_w "Installing brew package manager for macOS 💾"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
}
package_install() {
install_brew_osx #Without this it will try to use apt
# Because apt that is not package manager exists on macOS
if command_exists dnf; then
_a "Installing '${*}' using dnf"
sudo dnf -y install "${*}" > /dev/null 2>&1 | _log "Installing '${*}' using dnf"
elif command_exists yum; then
_a "Installing '${*}' using yum"
yes | sudo yum install "${*}" > /dev/null 2>&1 | _log "Installing '${*}' using yum"
elif command_exists brew; then
_a "Installing '${*}' using brew"
yes | brew install "${*}" 2>&1 | _log "Installign '${*}' using brew"
elif command_exists pacman; then
_a "Installing '${*}' using pacman"
sudo pacman -S --noconfirm "${*}" > /dev/null 2>&1 | _log "Installign '${*}' using pacman"
elif command_exists apt-get; then
_a "Installing '${*}' using apt"
sudo apt-get -y install "${*}" > /dev/null 2>&1 | _log "Installign '${*}' using apt"
else
_e "Could not install '${*}', no package provider found"
return 1
fi
return 0
}
is_macos() {
[[ "$(uname -s)" == Darwin ]]
}
is_clt_installed() {
command -vp xcode-select > /dev/null 2>&1 && xpath=$(command -p xcode-select --print-path) && test -d "${xpath}" && test -x "${xpath}"
}
start_sudo() {
[[ "${DOTLY_ENV:-PROD}" == "CI" ]] && return
if ! has_sudo; then
command -p sudo -v -B
if has_sudo && [[ -z "${SUDO_PID:-}" ]]; then
(while true; do
command -p sudo -v
command -p sleep 30
done) &
SUDO_PID="$!"
builtin trap stop_sudo SIGINT SIGTERM
fi
fi
}
stop_sudo() {
builtin kill "$SUDO_PID" > /dev/null 2>&1
builtin trap - SIGINT SIGTERM
command -p sudo -k
}
has_sudo() {
[[ "${DOTLY_ENV:-PROD}" == "CI" ]] && return
command -p sudo -n -v > /dev/null 2>&1
}
if command -v brew > /dev/null 2>&1; then
eval "$(command brew shellenv)"
else
PATH="/usr/local/bin:/opt/homebrew/bin:/home/.linuxbrew/bin:${HOME}/.homebrew/bin:${HOME}/.brew/bin:${HOME}/.linuxbrew/bin${PATH:+:$PATH}"
fi
_w " ┌────────────────────────────────────┐"
_w "~ │ 🚀 Welcome to the ${green}sloth${normal} restorer! │ ~"
_w " └────────────────────────────────────┘"
_w
#### Begin user prompts ####
if [[ $(uname -s) == "Darwin" ]] && ! is_clt_installed; then
_w "We will need to use user elevation with sudo to install Command Line Tools"
start_sudo
if ! has_sudo; then
_e "Elevation error: wrong or not provided password"
_a "Yow will be asked later for the password again"
fi
fi
if
[[ "${DOTLY_ENV:-PROD}" != "CI" ]] &&
is_macos &&
[[ -z "${SETUP_ZSH_AND_BASH_IN_SHELLS:-}" ]]
then
SETUP_ZSH_AND_BASH_IN_SHELLS=false
if
_q "Do you want to setup more modern \`zsh\` & \`bash\` as valid shells in \`/etc/shells\` (require elevation)? [Y/n]" "PROMPT_REPLY" &&
[[ "${PROMPT_REPLY:-Y}" =~ ^[Yy] ]]
then
start_sudo
has_sudo && SETUP_ZSH_AND_BASH_IN_SHELLS=true
! has_sudo && _e "It can not be done without elevation, aborting this options (should be done later manually)" && SETUP_ZSH_AND_BASH_IN_SHELLS=false
fi
# We need to cache sudo to avoid asking for password later and every time
elif
[[ "${DOTLY_ENV:-PROD}" != "CI" ]] &&
is_macos &&
[[ -n "${SETUP_ZSH_AND_BASH_IN_SHELLS:-}" ]]
then
start_sudo
! has_sudo && _e "It can not be done without elevation, aborting this options (should be done later manually)"
SETUP_ZSH_AND_BASH_IN_SHELLS=false
fi
export SETUP_ZSH_AND_BASH_IN_SHELLS
# Backups
if
! ${IGNORE_APPLY_SYMLINKS:-false} &&
[[ -z "${SYMLINKS_ARGS[*]:-}" ]]
then
SYMLINKS_ARGS=(--interactive-backup)
if [[ "${DOTLY_ENV:-PROD}" == "CI" ]] || ${never_backup:-}; then
SYMLINKS_ARGS=(--ignore-backup)
elif ${always_backup:-}; then
SYMLINKS_ARGS=(--backup)
else
bk=false
output::yesno "Do you want to perform a backup of symlinks before apply them (this will include all existing files)" && bk=true
$bk && output::yesno "Do you want to be asked for every file" || SYMLINKS_ARGS=(--backup)
! $bk && SYMLINKS_ARGS=(--ignore-backup)
fi
fi
export SYMLINKS_ARGS
# ZSH as default Shell
zsh_shell_path="$(command -v zsh || true)"
if [[ -z "${SETUP_ZSH_AS_DEFAULT_SHELL:-}" ]]; then
SETUP_ZSH_AS_DEFAULT_SHELL=false
if
[[ "${DOTLY_ENV:-PROD}" != "CI" ]] &&
[[ "$SHELL" == *"zsh"* ]] &&
command_exists chsh &&
[[ -x "$zsh_shell_path" ]] &&
[[ -f "/etc/shells" ]] &&
! grep -q "^${zsh_shell_path}$" "/etc/shells"
then
_q "Do you want to setup the zsh \`$zsh_shell_path\` as default shell? [Y/n]" "PROMPT_REPLY" &&
[[ "${PROMPT_REPLY:-Y}" =~ ^[Yy] ]] &&
SETUP_ZSH_AS_DEFAULT_SHELL=true
_a "This will need a sudo authentication to add it to \`/etc/shells\` file"
start_sudo
if ! sudo -n -v; then
_e "Without sudo authentication this can not be done. You should do this later manually"
SETUP_ZSH_AS_DEFAULT_SHELL=false
fi
fi
elif
[[ -n "${SETUP_ZSH_AS_DEFAULT_SHELL:-}" ]] &&
[[ "${DOTLY_ENV:-PROD}" != "CI" ]] &&
[[ "$SHELL" == *"zsh"* ]] &&
command_exists chsh &&
[[ -x "$zsh_shell_path" ]] &&
[[ -f "/etc/shells" ]] &&
! grep -q "^${zsh_shell_path}$" "/etc/shells"
then
start_sudo
! has_sudo &&
_e "\`SETUP_ZSH_AS_DEFAULT_SHELL\` should be aborted because sudo elevation password is not set." &&
SETUP_ZSH_AS_DEFAULT_SHELL=false
elif
[[ -z "${SETUP_ZSH_AS_DEFAULT_SHELL:-}" ]] ||
[[ "${DOTLY_ENV:-PROD}" == "CI" ]] ||
! command_exists chsh ||
[[ ! -x "$zsh_shell_path" ]] ||
[[ ! -f "/etc/shells" ]] ||
grep -q "^${zsh_shell_path}$" "/etc/shells" ||
! has_sudo
then
SETUP_ZSH_AS_DEFAULT_SHELL=false
fi
export SETUP_ZSH_AS_DEFAULT_SHELL
# Ask if the user want to import all packages when have finished
if [[ -z "${USER_IMPORT_PACKAGES:-}" ]]; then
_w "Importing previous packages from the restorer is only possible if your"
_w "current hostname has the same as previous system. If this is not your"
_w "use case. Answer \`n\` and do it later manually by executing"
_a "\`dot package import\`"
_w "Apologies"
_w
_q "📦 Do you want to import previous installed packages when finish the recovery? [Y/n]" "USER_IMPORT_PACKAGES"
if [[ "${USER_IMPORT_PACKAGES:-Y}" =~ ^[Yy] ]]; then
USER_IMPORT_PACKAGES=true
fi
fi
# Files locations
_q "Where do you want your dotfiles to be located? (default ~/.dotfiles)" "DOTFILES_PATH"
DOTFILES_PATH="$(eval echo "${DOTFILES_PATH:-$HOME/.dotfiles}")"
export DOTFILES_PATH
dotly_inner_path="modules/sloth"
export DOTLY_PATH="$DOTFILES_PATH/$dotly_inner_path"
export SLOTH_PATH="${SLOTH_PATH:-${DOTLY_PATH:-}}"
# Backup if currently there are any dotfiles and prepare parent directory
if [[ -d "$DOTFILES_PATH" ]] && ! ${continue:-}; then
_q "🗂 Your DOTFILES_PATH is not empty. Do you want to do a backup first? [Y/n]" "PROMPT_REPLY"
[[ "${PROMPT_REPLY:-Y}" =~ ^[Yy] ]] && create_dotfiles_dir "$DOTFILES_PATH"
fi
if [[ ! -d "$DOTFILES_PATH" ]]; then
# Menu to select from where you want to restore your files
PS3="From where you want to install your dotfiles: "
if [[ "$OSTYPE" =~ ^[darwin] ]]; then
options=("GitHub" "Keybase" "Other Git alternative" "iCloud" "Quit")
else
options=("GitHub" "Keybase" "Other Git alternative" "Quit")
fi
GIT_URL=""
select opt in "${options[@]}"; do
case $opt in
"GitHub")
_q "👤 Which is your github user? [$USER]" "GITHUB_USER"
GITHUB_USER="${GITHUB_USER:-$USER}"
_q "📦 Which is your github repository? [dotfiles]" "GITHUB_REPOSITORY_NAME"
GITHUB_REPOSITORY_NAME="${GITHUB_REPOSITORY_NAME:-dotfiles}"
GIT_URL="git@github.com:$GITHUB_USER/$GITHUB_REPOSITORY_NAME.git"
GIT_URL_MIRROR="https://github.com/$GITHUB_USER/$GITHUB_REPOSITORY_NAME.git"
_w " Remember that if your dotfiles repository is private you"
_w "need to generate or restore the ssh key and added to your"
_w "github account in:"
_w " - Github > Settings > SSH and GPG Keys > Add new SSH key"
_w
_pk "continue when ready"
break
;;
"Keybase")
_w "Checking if keybase is installed"
if ! ls /Applications/Keybase.app > /dev/null 2>&1; then
_w "Keybase has not been detected. We will try to install it with the package manager."
_w "If the installation finish the script re run it to finish the install"
package_install keybase || { _e "Keybase could not be installed, try by yourself, do a login and re run this script." && exit 1; }
_s "Keybase is installed"
_pk "continue"
fi
_q "👤 Which is your Keybase user? [$USER]" "KEYBASE_USER"
KEYBASE_USER="${KEYBASE_USER:-$USER}"
KEYBASE_REPOSITORY_NAME=""
_q "📦 Which is your Keybase repository name? [dotfiles]" "KEYBASE_REPOSITORY_NAME"
KEYBASE_REPOSITORY_NAME="${KEYBASE_REPOSITORY_NAME:-"dotfiles"}"
GIT_URL="keybase://private/${KEYBASE_USER}/${KEYBASE_REPOSITORY_NAME}"
_w
_w " Remember that you need to start Keybase for the first time and login."
_w " If you don't do this firstly, the installation will fail"
_w
_pk "continue when ready"
_w
break
;;
"Other Git alternative")
while [ -z "$GIT_URL" ]; do
_q "👤 Which is your git repo url?" "GIT_URL"
done
break
;;
"iCloud")
_q "Which is the name of your iCloud dotfiles folder? [.dotfiles]" "ICLOUD_DOTFILES_PATH_NAME"
ICLOUD_DOTFILES_PATH="$ICLOUD_PATH/${ICLOUD_DOTFILES_PATH_NAME:-.dotfiles}"
DOTFILES_PATH="$HOME/$ICLOUD_DOTFILES_PATH_NAME"
if [[ ! -d "$ICLOUD_DOTFILES_PATH" ]]; then
_e "Dotfiles folder were not found in iCloud Drive"
exit 5
fi
ln -s "$ICLOUD_DOTFILES_PATH" "$DOTFILES_PATH"
;;
"Quit")
_w "Bye!"
exit 0
;;
*)
echo "invalid option $REPLY"
;;
esac
done
# Git Install
# In MacOS git is installed if Command Line Tools (CLT) are installed
# and not if git command exists because it exists but ask you to
# install CLT.
_w "Checking if Git is installed"
if is_macos && ! is_clt_installed; then
# In macOS if we do not have CLT then we do not have git. This could be wrong
# but we will want brew later and if we do not have CLT we can not have brew
#
# Brew manages the installation of brew so we will intall Brew
_w "Command Line Tools could not be detected"
install_brew_osx
_w
fi
if ! command_exists git; then
# Other OS checking if git exists...
if ! package_install git; then
_e "Could not continue without git"
exit 1
fi
fi
_s "Git command exists on this system"
_w
# Check if curl command exists and try to install it if not
if ! command_exists curl; then
# Other OS checking if git exists...
if ! package_install curl; then
_e "Could not continue without git"
exit 1
fi
fi
_s "Curl command exists on this system"
if ${IS_ICLOUD_DOTFILES:-false}; then
# Recovering your files
_w "Installing your dotfiles from $opt"
_a "Attemping: git clone ${GIT_URL} ${DOTFILES_PATH}"
is_cloned=false
git clone "${GIT_URL}" "${DOTFILES_PATH}" 2>&1 | _log "Cloning from $GIT_URL" && is_cloned=true
# This because maybe you do not have yet your ssh-keys because you did not download
# your dotfiles. And SSH URL for repository would be the default method using a
# repository that you can write.
if ! $is_cloned && [[ ! -d "$DOTFILES_PATH" ]] && [[ "$opt" == "GitHub" ]]; then
_a "Attemping: git clone ${GIT_URL_MIRROR} ${DOTFILES_PATH}"
git clone "$GIT_URL_MIRROR" "$DOTFILES_PATH" 2>&1 | _log "Cloning from $GIT_URL_MIRROR" && is_cloned=true
if $is_cloned; then
_s "Dotfiles restored."
_w
_w "Your dotfiles could not be downloaded using ssh. Were downloaded using https."
_w
_w "Trying to setup remote origin upstream url with git+ssh protocol"
cd "${DOTFILES_PATH}" &&
git remote set-url origin "${GIT_URL}" 2>&1 &&
_s "SSH Remote was set."
else
_e "Dotfiles could not be cloned. See more details: \`tail -f $HOME/dotly.log\`"
exit 1
fi
elif ! $is_cloned || [[ ! -d "$DOTFILES_PATH" ]]; then
_e "Dotfiles could not be cloned. See more details: \`tail -f $HOME/dotly.log\`"
exit 1
fi
_s "Dotfiles cloned successfully."
_w
fi
fi
#### End user prompts ####
# Update .Sloth/Dotly submodule
cd "${DOTFILES_PATH}"
# Only .Sloth/Dotly submodule must be updated recursively because we do not know if
# user has added any other submodules that are privated and maybe the user
# needs to configure something to access those repositories
git submodule update --init --recursive "$dotly_inner_path" 2>&1 | _log "Downloading .Sloth/Dotly and submodules" || {
_e "Downloading dotly failed. See for more details:"
_e " tail -f $HOME/dotly.log"
_w
exit 1
}
_w "Installing .Sloth/Dotly default tools"
_w "Please be patient this could take some time...🙏"
# Installing default .Sloth/Dotly tools
PATH="$PATH:/usr/local/bin:$HOME/.cargo/bin"
"${SLOTH_PATH:-${DOTLY_PATH:-}}/bin/dot" core install || {
_e ".Sloth/Dotly Could not be installed but your dotfiles are restored. Use:"
_e " tail -f $HOME/dotly.log"
_e "To know what happened and where is the point of failure"
_w
}
_a "🎉 dotfiles restored! 🎉"
# Installing packages
if ${USER_IMPORT_PACKAGES:-false}; then
_w "Importing your packages"
_w "This can take a very long time, be patient...🙏"
{
"${SLOTH_PATH:-${DOTLY_PATH:-}}/bin/dot" package import --never-prompt 2>&1 | _log "Importing user packages" &&
_a "Packages imported 👏" &&
_w
} || {
_e "📦 Packages import fail"
_w
}
fi
_a "🎉 dotfiles restored! 🎉"
_a "Please, restart your terminal to see the changes."