-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-utils.sh
More file actions
executable file
·267 lines (228 loc) · 8.8 KB
/
sync-utils.sh
File metadata and controls
executable file
·267 lines (228 loc) · 8.8 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
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
#!/bin/bash
# ==============================================================================
# Script Name: sync-utils.sh
# Description: Quickly syncs local config (tmux, bash, bin) to system paths
# for rapid development and testing.
# Usage: ./sync-utils.sh [-c|--cleanup] [-l|--list] [-h|--help]
# ==============================================================================
set -e
# Colors
C_BLUE=$'\033[1;34m'
C_GREEN=$'\033[32m'
C_RED=$'\033[31m'
C_YELLOW=$'\033[33m'
C_BOLD=$'\033[1m'
T_RESET=$'\033[0m'
print_usage() {
cat <<EOF
Usage: $(basename "$0") [options]
Quickly syncs local configuration to system paths for rapid development.
Targets:
- Tmux: ~/.config/tmux
- Starship: ~/.config/starship.toml
- Bash: ~/.bash_aliases
- Bin: ~/.local/bin
Options:
${C_BLUE}-c, --cleanup${T_RESET} Remove old backup files/directories
${C_BLUE}-l, --list${T_RESET} List existing backup directories
${C_BLUE}-h, --help${T_RESET} Show this help message
EOF
}
# Parse Arguments
CLEANUP=false
LIST_BACKUPS=false
for arg in "$@"; do
case $arg in
-c|--cleanup) CLEANUP=true ;;
-l|--list) LIST_BACKUPS=true ;;
-h|--help) print_usage; exit 0 ;;
*) print_usage; exit 1 ;;
esac
done
# Where are we running?
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
# Tmux Paths
SRC_TMUX_CONF="${SCRIPT_DIR}/config/tmux/tmux.conf"
SRC_TMUX_SCRIPTS="${SCRIPT_DIR}/config/tmux/scripts/dv"
DEST_TMUX_CONF="${HOME}/.config/tmux/tmux.conf"
DEST_TMUX_SCRIPTS_DIR="${HOME}/.config/tmux/scripts/dv"
DEST_TMUX_BASE_DIR="${HOME}/.config/tmux"
# Bash Paths
SRC_BASH_ALIASES="${SCRIPT_DIR}/config/bash/.bash_aliases"
DEST_BASH_ALIASES="${HOME}/.bash_aliases"
# Starship Paths
SRC_STARSHIP_CONF="${SCRIPT_DIR}/config/starship.toml"
DEST_STARSHIP_CONF="${HOME}/.config/starship.toml"
# Bin Paths
SRC_BIN_DIR="${SCRIPT_DIR}/bin"
DEST_BIN_DIR="${HOME}/.local/bin"
# --- Helper Functions ---
backup_path() {
local path="$1"
if [ -e "$path" ]; then
local backup="${path}.bak_$(date +%Y%m%d_%H%M%S)"
echo " ${C_YELLOW}[i] Backing up${T_RESET} current config to $(basename "$backup")..."
cp -r "$path" "$backup"
fi
}
sync_file() {
local src="$1"
local dest="$2"
if [ -f "$src" ]; then
mkdir -p "$(dirname "$dest")"
if cp "$src" "$dest"; then
echo " ${C_GREEN}[✓] Updated:${T_RESET} $dest"
else
echo " ${C_RED}[✗] Error:${T_RESET} Failed to copy to $dest"
fi
else
echo " ${C_RED}[✗] Error:${T_RESET} Source file not found: $src"
fi
}
sync_dir_contents() {
local src="$1"
local dest="$2"
local chmod_pattern="$3"
if [ -d "$src" ]; then
mkdir -p "$dest"
cp "$src"/* "$dest/" 2>/dev/null || true
if [ -n "$chmod_pattern" ]; then
chmod +x "$dest"/$chmod_pattern 2>/dev/null || true
fi
local count
count=$(find "$src" -maxdepth 1 -type f | wc -l)
echo " ${C_GREEN}[✓] Updated:${T_RESET} Contents of $dest ${C_YELLOW}($count files)${T_RESET}"
ls -m "$src"
return 0
else
# Return failure so caller can handle specific error messaging
return 1
fi
}
perform_cleanup() {
echo "${C_BLUE}[i] Cleaning up old backups...${T_RESET}"
tmux_backups=$(find "${HOME}/.config" -maxdepth 1 -type d -name "tmux.bak_*" 2>/dev/null)
bash_backups=$(find "${HOME}" -maxdepth 1 -type f -name ".bash_aliases.bak_*" 2>/dev/null)
starship_backups=$(find "${HOME}/.config" -maxdepth 1 -type f -name "starship.toml.bak_*" 2>/dev/null)
all_backups=$(printf "%s\n%s\n%s" "$tmux_backups" "$bash_backups" "$starship_backups" | grep -v "^$")
total_count=$(echo "$all_backups" | grep -c -v "^$")
if [ "$total_count" -gt 0 ]; then
echo "The following backups will be deleted:"
echo "$all_backups" | sort | sed 's/^/ - /'
total_size=$(echo "$all_backups" | tr '\n' '\0' | xargs -0 du -ch 2>/dev/null | tail -n 1 | cut -f1)
echo ""
echo " Total size to be freed: ${C_BOLD}${total_size}${T_RESET}"
echo ""
read -p " ${C_YELLOW}[?] Are you sure you want to delete these ${C_BOLD}$total_count${T_RESET}${C_YELLOW} backup(s)? [y/N]${T_RESET} " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "$all_backups" | tr '\n' '\0' | xargs -0 rm -rf
echo " ${C_GREEN}[✓] Removed $total_count old backup(s).${T_RESET}"
else
echo " ${C_RED}[✗] Cleanup cancelled.${T_RESET}"
fi
else
echo " [i] No old backups found."
fi
}
verify_tmux_bindings() {
echo " ${C_BLUE}[i] Verifying script bindings:${T_RESET}"
# Check each script to see if it is bound in the current session
current_bindings=$(tmux list-keys -a)
for script in "$SRC_TMUX_SCRIPTS"/*.sh; do
[ -e "$script" ] || continue
script_name=$(basename "$script")
match=$(echo "$current_bindings" | grep -F "$script_name" | head -n 1)
if [ -n "$match" ]; then
# Extract Table
if [[ "$match" =~ -T[[:space:]]+([^[:space:]]+) ]]; then
table="${BASH_REMATCH[1]}"
else
table="prefix"
fi
# Extract Key by stripping known flags
# 1. Remove 'bind-key' or 'bind' and optional '-r'
temp=$(echo "$match" | sed -E 's/^(bind-key|bind)[[:space:]]+(-r[[:space:]]+)?//')
# 2. Remove '-N "Note"' if present (in case it appears in the command string)
temp=$(echo "$temp" | sed -E "s/-N[[:space:]]+(\"[^\"]*\"|'[^']*')[[:space:]]+//")
# 3. Remove '-T table' if present
temp=$(echo "$temp" | sed -E 's/-T[[:space:]]+[^[:space:]]+[[:space:]]+//')
# 4. The first remaining word is the key
key=$(echo "$temp" | awk '{print $1}')
# Fetch Note explicitly using the key and table
note=$(tmux list-keys -N -T "$table" "$key" 2>/dev/null | awk '{$1=""; print $0}' | sed 's/^[[:space:]]*//')
if [ "$table" == "prefix" ]; then
label="Prefix + $key"
elif [ "$table" == "root" ]; then
label="$key"
else
label="$table: $key"
fi
if [ -n "$note" ]; then
echo " ${C_GREEN}[✓] $script_name${T_RESET} ($label) → \"$note\""
else
echo " ${C_GREEN}[✓] $script_name${T_RESET} ($label)"
fi
else
echo " ${C_YELLOW}/!\ $script_name${T_RESET} (Not bound)"
fi
done
}
# --- Main Execution ---
if [ "$LIST_BACKUPS" = true ]; then
echo "${C_BLUE}[i] Existing backups:${T_RESET}"
# Find tmux backups
find "${HOME}/.config" -maxdepth 1 -type d -name "tmux.bak_*" 2>/dev/null | sort | sed 's/^/ - /'
# Find bash backups
find "${HOME}" -maxdepth 1 -type f -name ".bash_aliases.bak_*" 2>/dev/null | sort | sed 's/^/ - /'
# Find starship backups
find "${HOME}/.config" -maxdepth 1 -type f -name "starship.toml.bak_*" 2>/dev/null | sort | sed 's/^/ - /'
exit 0
fi
if [ "$CLEANUP" = true ]; then
perform_cleanup
exit 0
fi
# 1. Tmux Sync
echo "${C_BLUE}[i] Syncing Tmux Configuration...${T_RESET}"
backup_path "$DEST_TMUX_BASE_DIR"
sync_file "$SRC_TMUX_CONF" "$DEST_TMUX_CONF"
if ! sync_dir_contents "$SRC_TMUX_SCRIPTS" "$DEST_TMUX_SCRIPTS_DIR" "*.sh"; then
echo " ${C_RED}[✗] Error:${T_RESET} Source scripts directory not found at $SRC_TMUX_SCRIPTS"
fi
# 2. Starship Sync
echo ""
echo "${C_BLUE}[i] Syncing Starship Config...${T_RESET}"
backup_path "$DEST_STARSHIP_CONF"
sync_file "$SRC_STARSHIP_CONF" "$DEST_STARSHIP_CONF"
# 3. Bash Sync
echo ""
echo "${C_BLUE}[i] Syncing Bash Aliases...${T_RESET}"
backup_path "$DEST_BASH_ALIASES"
sync_file "$SRC_BASH_ALIASES" "$DEST_BASH_ALIASES"
# 4. Bin Sync
echo ""
echo "${C_BLUE}[i] Syncing Binaries...${T_RESET}"
if ! sync_dir_contents "$SRC_BIN_DIR" "$DEST_BIN_DIR" "dv-*"; then
echo " ${C_YELLOW}[!] No binaries found in $SRC_BIN_DIR${T_RESET}"
fi
# Ensure library files are not executable
if [ -f "$DEST_BIN_DIR/dv-common.sh" ]; then
chmod -x "$DEST_BIN_DIR/dv-common.sh"
fi
# 5. Post-Sync Actions
echo ""
if [ -n "$TMUX" ]; then
tmux source-file "$DEST_TMUX_CONF"
tmux display-message "Dev Config Synced & Reloaded!"
echo " ${C_GREEN}[✓] Reloaded${T_RESET} active tmux session."
verify_tmux_bindings
else
echo " ${C_BLUE}Not inside tmux.${T_RESET} Run '${C_BOLD}tmux source ~/.config/tmux/tmux.conf${T_RESET}' to apply."
fi
echo ""
echo " ${C_BLUE}To apply bash changes:${T_RESET} source ~/.bash_aliases"
# If in Tmux, pre-type the command for the user
if [[ -n "$TMUX" ]] && command -v tmux &>/dev/null; then
tmux send-keys -t "$(tmux display-message -p "#{pane_id}")" "source ~/.bash_aliases"
fi