-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.zsh
More file actions
71 lines (58 loc) · 1.52 KB
/
init.zsh
File metadata and controls
71 lines (58 loc) · 1.52 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
#!/bin/zsh
export PATH="$PATH:$DOTFILES_HOME/bin"
WARN_COLOR="\e[33m"
SUCCESS_COLOR="\e[32m"
ERROR_COLOR="\e[31m"
INFO_COLOR="\e[37m"
DEBUG_COLOR="\e[38m"
RESET_COLOR="\e[m"
_message() {
msg=$1
color=$2
printf '%b%b%b\n' "${color}" "${msg}" "${RESET_COLOR}"
}
success() {
_message "${1}" "$SUCCESS_COLOR"
}
info() {
_message "${1}" "$INFO_COLOR"
}
debug() {
_message "${1}" "$DEBUG_COLOR"
}
warn() {
_message "${1}" "$WARN_COLOR"
}
error() {
_message "error: ${1}" "$ERROR_COLOR"
}
fail() {
_message "failed: ${1}" "$ERROR_COLOR"
exit 1
}
# Function that takes two arguments: source and target
# It creates a symbolic link from source to target
# If the target already exists, it backs it up by renaming it with a .bak extension
# Usage: link /path/to/source /path/to/target
link() {
SOURCE=$1
TARGET=$2
info "Linking $SOURCE to $TARGET"
# Check if source exists, if so, backup
if [ -d "$TARGET" ]; then
mv "$TARGET" "${TARGET}.bak"
warn "Existing file/directory at $TARGET moved to ${TARGET}.bak"
fi
# Check if the target already exists as a symlink
# and if it points to the correct source
if [ -L "$TARGET" ]; then
if [ "$(readlink "$TARGET")" = "$SOURCE" ]; then
success "Symlink at $TARGET already points to $SOURCE"
else
mv "$TARGET" "${TARGET}.bak"
warn "Existing symlink at $TARGET moved to ${TARGET}.bak"
fi
fi
ln -s "$SOURCE" "$TARGET"
success "Linked $SOURCE to $TARGET"
}