-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdots
More file actions
executable file
·534 lines (484 loc) · 12.6 KB
/
dots
File metadata and controls
executable file
·534 lines (484 loc) · 12.6 KB
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
528
529
530
531
532
533
534
#!/bin/sh
# File: dots
# Author: Zakhary Kaplan <https://zakhary.dev>
# Created: 05 Jun 2019
# Version: 0.1.0
# SPDX-License-Identifier: MIT
# Vim: set ft=sh:
#
## Defines
#
# @env NAME Name of invoked command
NAME="${0##*/}"
# @env VERSION Version number of this script.
VERSION=0.1.0
# @env DOTS Default dotfiles installation directory path.
DOTS="${DOTS:-$HOME/.dots}"
# @env REPO Remote URL to dotfiles repository.
REPO="${REPO:-https://git.zakhary.dev/dotfiles}"
# @env GIT Call git from the proper directory.
GIT="git -C ${DOTS}"
# @env MAKE Run the associated Makefile.
MAKE="make -C ${DOTS}"
# @env LOG_FILE Logging output file.
LOG_FILE="${LOG_FILE:-"${DOTS}/logs/dots.$(date +%s).log"}"
# @env LOG_LEVEL Filter for logging.
LOG_LEVEL="${LOG_LEVEL:-info}"
# @env NO_COLOR Disable ANSI color escape codes.
NO_COLOR="${NO_COLOR}"
#
## Library
#
# Aliases
# @alias log -<e|w|i|d|t>
alias error="log -e"
alias warn="log -w"
alias info="log -i"
alias debug="log -d"
alias trace="log -t"
# Methods
#
# @desc Display formatted log messages.
# @env LOG_LEVEL "warn"
#
log() {
# Parse opts
local level=info
local noeol=
while getopts ":l:ewidtn" opt; do
case ${opt} in
l)
level="${OPTARG}"
;;
e)
level=error
;;
w)
level=warn
;;
i)
level=info
;;
d)
level=debug
;;
t)
level=trace
;;
n)
noeol=1
;;
\?)
return
;;
esac
done
shift $((OPTIND - 1))
OPTIND=1 # XXX: cannot uneset in `dash`
# Extract from env
local filter=${LOG_LEVEL:-warn}
# Define levels
# shellcheck disable=2034
local none=0
# shellcheck disable=2034
local error=1
# shellcheck disable=2034
local warn=2
# shellcheck disable=2034
local info=3
# shellcheck disable=2034
local debug=4
# shellcheck disable=2034
local trace=5
# Indirect variable expansion
local filterno levelno
filterno="$(eval "echo \${${filter}}")"
levelno="$(eval "echo \${${level}}")"
# Check for misconfiguration
if [ -z "${filterno}" ] || [ -z "${levelno}" ]; then
return
fi
# Filter-out disabled messages
if [ "${filterno}" -lt "${levelno}" ]; then
return
fi
# Prepare ANSI color sequences
local clear=
local color=
local style=
if [ -z "${NO_COLOR}" ]; then
clear=$(printf "\033[0m")
case ${level} in
error )
color=$(printf "\033[31m")
style=$(printf "\033[1m")
;;
warn )
color=$(printf "\033[33m")
;;
info )
color=$(printf "\033[32m")
;;
debug )
color=$(printf "\033[34m")
;;
trace )
color=$(printf "\033[35m")
;;
esac
fi
# Check if EOL is needed
local eol=
if [ -z "${noeol}" ]; then
eol="\n"
fi
# Display message
printf "[%s] %s${eol}" "${color}${style}${level}${clear}" "${style}$*${clear}"
}
#
# @desc Exit early with a failure code and a message.
#
bail() {
local code="$1"
shift
error "$*"
exit "${code:-1}"
}
#
# @desc Exit early with a failure code.
#
cancel() {
bail 1 "Operation cancelled."
}
#
# @desc Run and monitor a command, logging output and exiting on failure.
#
monitor() {
# Execute monitored command, log output
$@ >> "${LOG_FILE}" 2>&1
# Extract status code
local code=$?
# Check for success
if [ ${code} -eq 0 ]; then
return # no-op on success
fi
# Indicate failure
error "Failed with exit code: ${code}"
# Display last few lines of output
tail -10 "${LOG_FILE}" | while read line; do warn "${line}"; done
# Abort failed execution
cancel
}
#
## Application
#
#
# @desc Print help.
#
usage() {
cat 1>&2 <<EOF
${NAME} ${VERSION}
Zakhary's dotfiles manager
Usage: ${NAME} [OPTIONS] [COMMAND]
Commands:
i, install Perform standard installation.
u, upgrade Upgrade the current installation.
Options:
-a Run with animations enabled
-c <WHEN> Coloring: auto, always, never
-x Run in script mode (disables animations, color)
-y Disable confirmation prompt
-l <LEVEL> Logging: none, error, warn, info, debug, trace
-q Run quietly
-v Run verbosely
-h Print help
-V Print version
EOF
}
#
# @desc Print banner.
#
banner() {
info 'Welcome to... '
info ' '
info ' /\/| __ _ _ __ _ _ '
info '|/\/ / /__| | ___ | |_ / _(_) | ___ ___ '
info ' / // _` |/ _ \| __| |_| | |/ _ \/ __| '
info ' / /| (_| | (_) | |_| _| | | __/\__ \ '
info ' /_/(_)__,_|\___/ \__|_| |_|_|\___||___/ '
info ' '
info ' by: Zakhary Kaplan '
info
}
#
# @desc Executable body.
#
main() {
# Declare config, set defaults
local animate=
local color=auto
local proceed=
local subcommand=
# Parse opts
while getopts ":ac:xyl:vqhV" opt; do
case ${opt} in
a)
animate=1
;;
c)
color="${OPTARG}"
;;
x)
animate=
color=never
;;
y)
proceed=yes
;;
l)
LOG_LEVEL="${OPTARG}"
;;
v)
LOG_LEVEL=trace
;;
q)
LOG_LEVEL=none
animate=
;;
h)
usage
exit
;;
V)
printf "%s\n" "${NAME} ${VERSION}"
exit
;;
\?)
printf "%s: illegal option -- %c\n" "${NAME}" "${OPTARG}"
printf "usage: %s [-cxylvqhV] [install|upgrade]\n" "${NAME}"
exit 1
;;
:)
printf "%s: option requires an argument -- %c\n" "${NAME}" "${OPTARG}"
printf "usage: %s [-cxylvqhV] [install|upgrade]\n" "${NAME}"
exit 1
;;
esac
done
shift $((OPTIND - 1))
OPTIND=1 # XXX: cannot uneset in `dash`
# Parse args
if [ "$#" -le 1 ]; then
# [SUBCOMMAND]
[ -n "$1" ] && {
subcommand="$1"
shift
}
else
printf "%s: extra arguments\n" "${NAME}"
printf "usage: %s [-cxylvqhV] [install|upgrade]\n" "${NAME}"
exit 1
fi
# Validate `color`
case "${color}" in
auto)
[ ! -t 0 ] && NO_COLOR=1
;;
always)
NO_COLOR=
;;
never)
NO_COLOR=1
;;
*)
printf "%s: illegal argument -- %s\n" "${NAME}" "${color}"
printf "usage: %s -c <auto|always|never>\n" "${NAME}"
exit 1
;;
esac
# Validate `LOG_LEVEL`
case "${LOG_LEVEL}" in
none|error|warn)
animate= # animations use `info` level
;;
info|debug|trace)
;;
*)
printf "%s: illegal argument -- %s\n" "${NAME}" "${LOG_LEVEL}"
printf "usage: %s -l <none|error|warn|info|debug|trace>\n" "${NAME}"
exit 1
;;
esac
# Validate `subcommand`
case "${subcommand}" in
install|i)
subcommand=install
;;
upgrade|u)
subcommand=upgrade
;;
"")
# Check if we're already installed, if so, then perform an upgrade
[ -d "${DOTS}" ] && subcommand=upgrade || subcommand=install
;;
*)
printf "%s: illegal subcommand -- %s\n" "${NAME}" "${subcommand}"
printf "usage: %s [-cxylvqhV] [install|upgrade]\n" "${NAME}"
exit 1
;;
esac
# Display the banner for a few seconds with an animated countdown
banner
if [ -n "${animate}" ]; then
local i=3; while [ $i -gt 0 ]; do
info "Starting in $i..." "$(printf "\033[A")"
sleep 1;
i=$((i - 1))
done
printf "\033[2K\r"
fi
# Check dependencies
debug "Checking dependencies..."
dep_checkall || {
# If dependencies not found, do not install
error "Failed: missing dependencies."
error "Please install missing dependencies and try again."
error
dep_logall -w
warn
[ "$(uname)" = "Darwin" ] && {
info "NOTE: Homebrew could be used to satisfy all dependencies."
info " Follow the instructions at: https://brew.sh"
info
}
cancel
}
dep_logall -t
debug "Satisfied. May proceed."
debug
# Perform subcommand
debug "Performing ${subcommand}."
case "${subcommand}" in
install)
# Confirm installation
warn "Installation will occur at: \`${DOTS}\`"
[ -z "${proceed}" ] && warn -n && read -rp "Proceed with installation? [Y/n] " proceed
case "${proceed}" in
[Yy]*)
;;
"")
;;
*)
bail 0 "Installation cancelled by user."
;;
esac
info
# Download dotfiles
info "Downloading..."
if [ ! -d "${DOTS}" ]; then
git clone "${REPO}" "${DOTS}"
else
warn "File already exists at path: \`${DOTS}\`"
if ${GIT} rev-parse >/dev/null 2>&1; then
if [ "$(${GIT} remote get-url origin)" != "${REPO}" ]; then
error "Path contains a foreign git repository."
cancel
fi
else
error "File is not a git directory."
cancel
fi
warn "Using existing installation."
fi
info "Downloaded. May proceed."
info
;;
upgrade)
# Confirm upgrade
[ -z "${proceed}" ] && info -n && read -rp "Proceed with upgrade? [Y/n] " proceed
case "${proceed}" in
[Yy]*)
;;
"")
;;
*)
bail 0 "Upgrade cancelled by user."
;;
esac
info
# Abort early if repo is dirty
if [ -n "$(${GIT} status -s)" ]; then
error "Dotfiles repo is dirty."
cancel
fi
# Unstow dotfiles
info "Unlinking installed files..."
monitor ${MAKE} unstow
info "Success."
info
# Update dotfiles repository
info "Updating dotfiles..."
# Pull updates
monitor ${GIT} pull --rebase --recurse-submodules
info "Success."
info
;;
esac
# Run Makefile
info "Running Makefile..."
monitor ${MAKE} install
info "Success."
info
# Complete installation
info "Completed. Please use responsibly :)"
}
#
## Utilities
#
#
# @desc Check a single dependency.
#
dep_check() {
test "$(command -v "$1")"
}
#
# @desc Check if minimal dependencies are installed.
#
dep_checkall() {
{
# Check if Homebrew is installed
dep_check brew
} || {
# Check if individual dependencies are installed
dep_check curl &&
dep_check git &&
dep_check make &&
dep_check stow &&
dep_check zsh &&
return
}
}
#
# @desc Print a single dependency.
#
dep_print() {
printf "%s %s" "$(command -v "$1" >/dev/null 2>&1 && echo '✓' || echo '✗')" "$1"
}
#
# @desc Print all dependencies.
#
dep_logall() {
log "$@" -- "Required:"
log "$@" -- " $(dep_print curl)"
log "$@" -- " $(dep_print git)"
log "$@" -- " $(dep_print make)"
log "$@" -- " $(dep_print stow)"
log "$@" -- " $(dep_print zsh)"
log "$@" -- "Recommended:"
log "$@" -- " $(dep_print nvim)"
log "$@" -- " $(dep_print tmux)"
}
#
## Entrypoint
#
main "$@"