-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_snippets.sh
executable file
·75 lines (66 loc) · 1.84 KB
/
update_snippets.sh
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
#!/bin/bash
shell=$(cat "/proc/$PPID/comm" 2>/dev/null)
detect_shell() {
if [[ $shell == *bash* ]]; then
shell='bash'
elif [[ $shell == *zsh* ]]; then
shell='zsh'
else
if [[ "$shell" == "$SHELL" ]]; then
printf 'ERROR: Unsupported shell: %s\n' "$shell" >&2
exit 1
else
shell=$SHELL
detect_shell
fi
fi
}
detect_shell
printf 'Detected shell as %s\n\n' "$shell" >&2
printf 'Updating snippets...\n' >&2
tmpfile=$(mktemp "${TMPDIR:-/tmp}/snippets.XXXXXXXXXX")
curl -fsSL https://raw.githubusercontent.com/encoding-hun/snippets/main/snippets.sh -o "$tmpfile"
if [[ -d "${XDG_CONFIG_HOME:-$HOME/.config}/$shell" ]]; then
rcfile="${XDG_CONFIG_HOME:-$HOME/.config}/$shell/${shell}rc"
snippets_file="${XDG_CONFIG_HOME:-$HOME/.config}/$shell/snippets.${shell}"
else
rcfile="$HOME/.${shell}rc"
snippets_file="$HOME/.${shell}_snippets"
fi
needs_source=1
printf '\n' >&2
if [[ -f "$snippets_file" ]]; then
if diff -q "$snippets_file" "$tmpfile" >/dev/null; then
printf '\e[32mAlready up to date\e[0m\n' >&2
needs_source=0
else
diff --color=always -u "$snippets_file" "$tmpfile" >&2
fi
else
diff --color=always -u "/dev/null" "$tmpfile" >&2
fi
printf '\n' >&2
if (( needs_source )); then
printf 'Saving snippets to %s\n' "$snippets_file" >&2
mv "$tmpfile" "$snippets_file"
else
rm -f "$tmpfile"
fi
if grep -q -F "$(basename "$snippets_file")" "$rcfile"; then
printf 'Found existing source line in %s\n' "$rcfile" >&2
else
printf 'Adding source line to %s\n' "$rcfile" >&2
printf 'source %s\n' "$snippets_file" >> "$rcfile"
fi
printf '\nDone!' >&2
if (( needs_source )); then
if [[ $1 == '--selfupdate' ]]; then
printf '%s' "$rcfile"
printf '\n' >&2
else
printf ' Restart your shell or run:\n' >&2
printf '$ \e[1msource %s\e[0m\n' "$rcfile" >&2
fi
else
printf '\n' >&2
fi