-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdots
executable file
·417 lines (377 loc) · 10.4 KB
/
dots
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
#!/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:
#
# shellcheck shell=dash
#
# ----------------
# - Definitions -
# ----------------
# @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 homepage.
LINK="${LINK:-https://zakhary.dev/dotfiles}"
# @env REPO Remote URL to dotfiles repository.
REPO="${REPO:-https://github.com/kaplanz/dotfiles.git}"
# @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.
export LOG_FILE="${LOG_FILE:-"${DOTS}/logs/dots.$(date +%s).log"}"
mkdir -p "$(dirname "${LOG_FILE}")"
# @env LOG_LEVEL Filter for logging.
export LOG_LEVEL="${LOG_LEVEL:-info}"
# @env NO_COLOR Disable ANSI color escape codes.
export NO_COLOR="${NO_COLOR}"
# ----------------
# - Libraries -
# ----------------
#
# @desc Source a file. Backup web URL can optionally be provided.
#
import() {
# Extract arguments
local path=$1
local link=$2
# Determine source file
local file=
# Test local path
if [ -f "${path}" ]; then
# Use local file
file="${path}"
elif [ -n "${link}" ]; then
# Download remote file
file="${path##*/}"
curl "${link}" -o "${file}"
fi
# Source the file
. "${file}"
# Remove artifacts
if [ "${file}" != "${path}" ]; then
rm "${file}"
fi
}
# `libdots`
import "${DOTS}/include/libdots.sh" "${LINK}/include/libdots.sh"
# ----------------
# - Entrypoint -
# ----------------
#
# @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
monitor 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 :)"
}
# ----------------
# - Functions -
# ----------------
#
# @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)"
}
# ----------------
# - Execution -
# ----------------
main "$@"