-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.zsh
executable file
·59 lines (49 loc) · 1.08 KB
/
utils.zsh
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
#!/usr/bin/env bash
case $- in
*i*) export IS_INTERACTIVE="1";;
*) export IS_SCRIPT="1";;
esac
is_interactive (){
if [ -n "$IS_INTERACTIVE" ]; then return 0; fi
return 1
}
case "$(uname -s)" in
Linux*) export IS_LINUX="1";;
Darwin*) export IS_MACOS="1";;
*) exit 1
esac
is_macos() {
if [ -n "$IS_MACOS" ]; then return 0; fi
return 1
}
is_linux() {
if [ -n "$IS_LINUX" ]; then return 0; fi
return 1
}
is_missing() {
if ! type "$1" > /dev/null; then return 0; fi
return 1
}
wait_until() {
is_interactive && {
read -r -s -k "?Press enter when $1..." < /dev/tty
}
! is_interactive && {
echo "Cannot wait for $1, terminal is not interactive. Exiting..."
exit 1
}
}
# link file and backup if needed
link(){
local src="$1"
local dst="$2"
# Backup the existing link
if [ -f "$dst" ] || [ -d "$dst" ] || [ -L "$dst" ]; then
# If the existing destination does not link to the current source, backup it
if ! [ "$src" = "$(readlink "$dst")" ]; then
mv "$dst" "${dst}.backup.$(date +%s)"
fi
fi
# Link the file
ln -fs "$src" "$dst"
}