-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotfyle
executable file
·370 lines (343 loc) · 9.18 KB
/
dotfyle
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
#!/usr/bin/env sh
dir_name="neovim-configs"
is_ignored() {
found=false
case "$1" in
*"/." | *"/.." | *"/*" | *"/.git")
found=true
;;
esac
$found
}
find_repo() {
if { [ "$2" = "git" ] && [ -d "$1"/.git ] ; } || \
{ [ "$2" != "git" ] && [ -f "$1"/init.lua ] || [ -f "$1"/init.vim ] ; }; then
path=${1#"$HOME/.config/$dir_name/"}
echo "$path"
else
for x in "*" ".*"; do
for pathname in "$1/"$x; do
if is_ignored "$pathname"; then
continue # skip these directories
elif [ -d "$pathname" ]; then
find_repo "$pathname" "$2"
fi
done
done
fi
}
select_config() {
if command -v fzf >/dev/null; then
find_repo "$HOME/.config/$dir_name" "$1" | \
fzf --prompt="Config: " \
--preview="find $HOME/.config/$dir_name/{} -name 'init.[lv][ui][am]' -type f -exec cat \{} + -quit" \
--preview-window=right:70%:wrap --bind=ctrl-f:page-down,ctrl-b:page-up \
--height=100% --border
else
echo "fzf not found, please install it to use this option" >&2
fi
}
check_config() {
if [ -f "$location/$1/init.lua" ] || [ -f "$location/$1/init.vim" ]; then
true
else
false
fi
}
run() {
location="$HOME/.config/$dir_name"
case "$@" in
"")
;;
"-l"|"--last-used")
if [ -f "$location/.last_used" ];then
last_used=$(cat "$location/.last_used")
if check_config "$last_used"; then
appname="$last_used"
fi
fi
;;
"-a" | "--ask")
echo "This option is depreciated, it is now the default behaviour"
shift
;;
"-s"*| "--select"*)
shift
if check_config "$1"; then
appname="$1"
else
echo config not found
exit 1
fi
;;
*)
echo Unknown option provided
exit 1
;;
esac
if [ -z "$appname" ]; then
appname="$(select_config)"
fi
if [ -n "$appname" ]; then
echo "$appname" > "$HOME/.config/$dir_name/.last_used"
NVIM_APPNAME="$dir_name/$appname" exec nvim
fi
}
purge() {
if [ "$1" = "-f" ] || [ "$1" = "--force" ]; then
shift
elif [ $# = 0 ]; then
echo "Are you sure you want to purge all files? [yN]"
read -r yn
case "$yn" in
[Yy] | [Yy][Ee][Ss])
;;
"" | [Nn] | [Nn][Oo])
exit
;;
*)
echo Option not recognized. Aborting
exit 1
;;
esac
fi
dir="$1"
parent_dir=${dir%/*}
child_dir=${dir#*/}
echo Removing "$dir"
if [ "$(ls "$HOME/.config/$dir_name/$parent_dir" -I "$child_dir")" = "" ]; then
echo "$parent_dir/ only contains $child_dir/"
echo Removing "$parent_dir"
dir="$parent_dir"
fi
rm -rf "$HOME/.config/$dir_name/$dir"
rm -rf "$HOME/.cache/$dir_name/$dir"
rm -rf "$HOME/.local/share/$dir_name/$dir"
rm -rf "$HOME/.local/state/$dir_name/$dir"
}
remove() {
appname="$(select_config "git")"
if [ -n "$appname" ]; then
purge "$appname"
fi
}
install() {
repo="$1"
repo=${repo#*github.com[/:]}
repo=${repo#*dotfyle.com[/:]}
repo=${repo%.git}
repo=${repo%/tree/*}
repo=${repo%/blob/*}
if ! git ls-remote --exit-code git@github.com:"$repo" >/dev/null 2>&1; then
substring="${repo%*"-"*}"
if [ "$substring" != "$repo" ]; then
install "$substring"
else
echo Provided repo could not be found
fi
else
git clone --depth=1 git@github.com:"$repo" "$HOME/.config/$dir_name/$repo"
fi
}
update() {
location="$HOME/.config/$dir_name"
repos=$(find_repo "$location" "git")
for repo in $repos; do
g="git -C $location/$repo"
changes=$($g diff --name-only)
echo "Updating $repo"
if [ -z "$changes" ]; then
$g pull >/dev/null
else
echo ""
echo "Found the following unstaged changes:"
echo "$changes"
while [ -z "$complete" ]; do
if [ "$1" != "" ]; then
kos="$1"
kos=${kos#-}
kos=${kos#-}
else
echo "Would you like to keep, overwrite, or skip? [koS]"
read -r kos
fi
kos=$(echo "$kos" | tr '[:upper:]' '[:lower:]')
case "$kos" in
"k"|"keep")
echo "Keeping local changes"
$g pull --autostash >/dev/null
complete=true
;;
"o"|"overwrite")
echo "Overwriting local files"
$g reset --hard HEAD >/dev/null
$g pull >/dev/null
complete=true
;;
""|"s"|"skip")
echo "Skipping"
echo "You can find this config at:"
echo "$location/$repo"
complete=true
;;
*)
if [ "$1" != "" ]; then
shift
else
echo "Unknown option"
fi
;;
esac
done
unset complete
fi
done
}
upgrade() {
path="$(readlink -e -- "$0")"
curl -o "$path" https://raw.githubusercontent.com/RoryNesbitt/dotfyle-cli/main/dotfyle
}
help() {
case "$1" in
"-h"|"--help")
cmd="$2"
;;
*)
cmd="$1"
;;
esac
case "$cmd" in
"run")
echo "Usage:
dotfyle run [options] Select a config to load neovim with.
dotfyle [options] Alias of above.
This option will load the last used
config, or if that is unavailable it
will list out all configs with fzf
to select from.
Options:
-h|--help Print the help. Can be used after an above option
-l|--last-used Load the most recently used config
-s|--select [config] Select the specific config
"
;;
"rm" | "remove")
echo "Usage:
dotfyle rm Select a config to be removed.
dotfyle rm [config] Remove the config specified.
dotfyle remove Alias of above
This option allows a config to be
removed by either the cli argument
or by selection with fzf.
NOTE: This option does not currently
account for monorepos and will
therefore leave the contents of the
git repo except for the neovim config.
However, it will also remove any related
files, making it still a useful option
before manually removing the repo.
Monorepo support is on the TODO
"
;;
"purge")
echo "Usage:
dotfyle purge Remove all configs and related files.
This option will completely remove the
neovim-configs directory from all relevant
locations.
Options:
-f|--force Don't ask for confirmation before purging.
"
;;
"install" | "download")
echo "Usage:
dotfyle install [URL] Install a config from the url provided.
dotfyle download Alias of above.
This option lets you install a config from
a dotfyle or github url to the neovim-configs
directory.
"
;;
"update")
echo "Usage:
dotfyle update Update all installed configs
This option runs a git pull on all known configs.
If there are any unstaged changes, it will prompt
you for how to handle them.
Options:
-k|--keep Keep the local changes with autostash
-o|--overwrite Overwrite local files, then update
-s|--skip Skip this repo and continue
"
;;
"upgrade")
echo "Usage:
dotfyle upgrade Upgrade dotfyle-cli itself
This option runs curl to download the latest version
of dotfyle-cli from github. It will save to wherever
the current dotfyle-cli is located.
"
;;
*)
echo "Usage:
dotfyle run [options] Select a config to load neovim with
dotfyle rm Remove a config and related files
dotfyle purge Remove all configs and related files
dotfyle install [URL] Install a config from the url provided.
dotfyle update Update all installed configs
dotfyle upgrade Upgrade dotfyle-cli itself
dotfyle Alias of dotfyle run
Options:
-d|--dir [dir] Select a different directory to use. This option must be first
-h|--help Print the help. Can be used after an above option
-l|--last-used Load the most recently used config
-s|--select [config] Select the specific config
"
;;
esac
}
if [ "$1" = "-d" ] || [ "$1" = "--dir" ]; then
if [ -z "$2" ]; then
echo "No directory provided"
exit 1
fi
dir_name="$2"
shift 2
fi
case "$@" in
*"-h" | *"--help" | "-h"* | "--help"*)
help "$@"
;;
"" | "-a" | "--ask" | "-s"* | "--select"*)
run "$@"
;;
"run"*)
shift
run "$@"
;;
"rm"* | "remove"*)
shift
remove "$@"
;;
"purge"*)
shift
purge "$@"
;;
"install"* | "download"*)
shift
install "$@"
;;
"update"*)
shift
update "$@"
;;
"upgrade"*)
shift
upgrade "$@"
;;
*)
echo Invalid option provided
echo Exiting
exit 1
;;
esac