-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmigrate.sh
More file actions
executable file
·323 lines (283 loc) · 9.33 KB
/
Copy pathmigrate.sh
File metadata and controls
executable file
·323 lines (283 loc) · 9.33 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
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
#!/bin/bash
# migrate.sh - Migration script for Potions v2.5.0
# Author: Henrique A. Lavezzo (Rynaro)
#
# This script migrates existing Potions installations to the new configuration structure
set -eo pipefail
# Enable verbose mode when DEBUG is set
if [ "${DEBUG:-}" = "1" ]; then
set -x
fi
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
WHITE='\033[1;37m'
BOLD='\033[1m'
NC='\033[0m' # No Color
# Check if terminal supports colors
if [ -t 1 ]; then
HAS_COLOR=true
else
HAS_COLOR=false
fi
# Configuration
POTIONS_HOME="$HOME/.potions"
BACKUP_DIR="$POTIONS_HOME/backups/pre-migration-$(date +%Y%m%d-%H%M%S)"
# Logging functions
log_info() {
if [ "$HAS_COLOR" = true ]; then
echo -e "${CYAN}${BOLD}⟹${NC} ${WHITE}$1${NC}"
else
echo "==> $1"
fi
}
log_success() {
if [ "$HAS_COLOR" = true ]; then
echo -e "${GREEN}${BOLD}✓${NC} ${GREEN}$1${NC}"
else
echo "[OK] $1"
fi
}
log_warning() {
if [ "$HAS_COLOR" = true ]; then
echo -e "${YELLOW}${BOLD}⚠${NC} ${YELLOW}$1${NC}"
else
echo "[WARN] $1"
fi
}
log_error() {
if [ "$HAS_COLOR" = true ]; then
echo -e "${RED}${BOLD}✗${NC} ${RED}$1${NC}"
else
echo "[ERROR] $1"
fi
}
log_step() {
if [ "$HAS_COLOR" = true ]; then
echo ""
echo -e "${BLUE}${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE}${BOLD} $1${NC}"
echo -e "${BLUE}${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
else
echo ""
echo "========================================"
echo " $1"
echo "========================================"
fi
}
# Check if migration is needed
check_migration_needed() {
# Check for legacy files that need migration
local needs_migration=false
if [ -f "$POTIONS_HOME/.zsh_aliases" ] && [ ! -f "$POTIONS_HOME/config/aliases.zsh" ]; then
needs_migration=true
fi
if [ -f "$POTIONS_HOME/.zsh_secure_aliases" ] && [ ! -f "$POTIONS_HOME/config/secure.zsh" ]; then
needs_migration=true
fi
if [ -d "$POTIONS_HOME/sources" ] && [ ! -d "$POTIONS_HOME/config" ]; then
needs_migration=true
fi
if [ "$needs_migration" = false ]; then
log_info "No migration needed - already using new configuration structure"
return 1
fi
return 0
}
# Create backup
create_backup() {
log_step "Creating backup"
mkdir -p "$BACKUP_DIR"
# Backup legacy files
local files_to_backup=(
".zsh_aliases"
".zsh_secure_aliases"
"sources"
)
for item in "${files_to_backup[@]}"; do
if [ -e "$POTIONS_HOME/$item" ]; then
cp -r "$POTIONS_HOME/$item" "$BACKUP_DIR/"
log_info "Backed up: $item"
fi
done
log_success "Backup created at $BACKUP_DIR"
}
# Migrate files
migrate_files() {
log_step "Migrating configuration files"
# Ensure config directory exists
mkdir -p "$POTIONS_HOME/config"
# Migrate .zsh_aliases -> config/aliases.zsh
if [ -f "$POTIONS_HOME/.zsh_aliases" ]; then
if [ -f "$POTIONS_HOME/config/aliases.zsh" ]; then
# Append to existing file
echo "" >> "$POTIONS_HOME/config/aliases.zsh"
echo "# Migrated from .zsh_aliases" >> "$POTIONS_HOME/config/aliases.zsh"
cat "$POTIONS_HOME/.zsh_aliases" >> "$POTIONS_HOME/config/aliases.zsh"
else
# Create new file with header
{
echo "# User Aliases and Functions"
echo "# Migrated from .zsh_aliases"
echo ""
cat "$POTIONS_HOME/.zsh_aliases"
} > "$POTIONS_HOME/config/aliases.zsh"
fi
log_success "Migrated .zsh_aliases -> config/aliases.zsh"
fi
# Migrate .zsh_secure_aliases -> config/secure.zsh
if [ -f "$POTIONS_HOME/.zsh_secure_aliases" ]; then
if [ -f "$POTIONS_HOME/config/secure.zsh" ]; then
echo "" >> "$POTIONS_HOME/config/secure.zsh"
echo "# Migrated from .zsh_secure_aliases" >> "$POTIONS_HOME/config/secure.zsh"
cat "$POTIONS_HOME/.zsh_secure_aliases" >> "$POTIONS_HOME/config/secure.zsh"
else
{
echo "# Secure/Private Aliases"
echo "# Migrated from .zsh_secure_aliases"
echo ""
cat "$POTIONS_HOME/.zsh_secure_aliases"
} > "$POTIONS_HOME/config/secure.zsh"
fi
log_success "Migrated .zsh_secure_aliases -> config/secure.zsh"
fi
# Migrate sources/*.sh -> config/*.zsh
if [ -d "$POTIONS_HOME/sources" ]; then
# macos.sh -> config/macos.zsh
if [ -f "$POTIONS_HOME/sources/macos.sh" ]; then
{
echo "# macOS-specific Configuration"
echo "# Migrated from sources/macos.sh"
echo ""
cat "$POTIONS_HOME/sources/macos.sh"
} > "$POTIONS_HOME/config/macos.zsh"
log_success "Migrated sources/macos.sh -> config/macos.zsh"
fi
# linux.sh -> config/linux.zsh
if [ -f "$POTIONS_HOME/sources/linux.sh" ]; then
{
echo "# Linux-specific Configuration"
echo "# Migrated from sources/linux.sh"
echo ""
cat "$POTIONS_HOME/sources/linux.sh"
} > "$POTIONS_HOME/config/linux.zsh"
log_success "Migrated sources/linux.sh -> config/linux.zsh"
fi
# wsl.sh -> config/wsl.zsh
if [ -f "$POTIONS_HOME/sources/wsl.sh" ]; then
{
echo "# WSL-specific Configuration"
echo "# Migrated from sources/wsl.sh"
echo ""
cat "$POTIONS_HOME/sources/wsl.sh"
} > "$POTIONS_HOME/config/wsl.zsh"
log_success "Migrated sources/wsl.sh -> config/wsl.zsh"
fi
# termux.sh -> config/termux.zsh
if [ -f "$POTIONS_HOME/sources/termux.sh" ]; then
{
echo "# Termux-specific Configuration"
echo "# Migrated from sources/termux.sh"
echo ""
cat "$POTIONS_HOME/sources/termux.sh"
} > "$POTIONS_HOME/config/termux.zsh"
log_success "Migrated sources/termux.sh -> config/termux.zsh"
fi
fi
# Create placeholder files if they don't exist
if [ ! -f "$POTIONS_HOME/config/local.zsh" ]; then
cat > "$POTIONS_HOME/config/local.zsh" << 'EOF'
# Local Machine Configuration
# This file is for machine-specific settings (not synced)
EOF
log_info "Created config/local.zsh"
fi
if [ ! -f "$POTIONS_HOME/nvim/user.vim" ]; then
cat > "$POTIONS_HOME/nvim/user.vim" << 'EOF'
" User Neovim Configuration
" Add your custom settings here
EOF
log_info "Created nvim/user.vim"
fi
if [ ! -f "$POTIONS_HOME/zellij/user.kdl" ]; then
cat > "$POTIONS_HOME/zellij/user.kdl" << 'EOF'
// User Zellij Configuration
// Add your custom settings here
EOF
log_info "Created zellij/user.kdl"
fi
}
# Print completion message
print_completion() {
echo ""
if [ "$HAS_COLOR" = true ]; then
echo -e "${GREEN}${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${GREEN}${BOLD} ✓ Migration completed successfully!${NC}"
echo -e "${GREEN}${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
echo -e "${WHITE}Changes made:${NC}"
echo -e " ${CYAN}•${NC} Legacy files backed up to: ${CYAN}$BACKUP_DIR${NC}"
echo -e " ${CYAN}•${NC} New config files created in: ${CYAN}~/.potions/config/${NC}"
echo ""
echo -e "${WHITE}New structure:${NC}"
echo -e " ${CYAN}config/aliases.zsh${NC} - Your custom aliases and functions"
echo -e " ${CYAN}config/secure.zsh${NC} - Private/sensitive configurations"
echo -e " ${CYAN}config/local.zsh${NC} - Machine-specific settings"
echo -e " ${CYAN}config/{platform}.zsh${NC} - Platform-specific settings"
echo -e " ${CYAN}nvim/user.vim${NC} - Your Neovim customizations"
echo -e " ${CYAN}zellij/user.kdl${NC} - Your Zellij customizations"
echo ""
echo -e "${WHITE}Next steps:${NC}"
echo -e " ${CYAN}1.${NC} Restart your terminal or run ${BOLD}exec zsh${NC}"
echo -e " ${CYAN}2.${NC} Review and organize your settings in the new files"
echo -e " ${CYAN}3.${NC} (Optional) Remove legacy files after verifying migration"
echo ""
echo -e "${YELLOW}Note:${NC} Legacy files are still loaded for backwards compatibility."
echo -e " You can safely remove them after verifying your config works."
echo ""
else
echo "========================================"
echo " Migration completed successfully!"
echo "========================================"
echo ""
echo "Legacy files backed up to: $BACKUP_DIR"
echo ""
echo "Next steps:"
echo " 1. Restart your terminal or run 'exec zsh'"
echo " 2. Review your settings in ~/.potions/config/"
echo ""
fi
}
# Main function
main() {
log_step "Potions Configuration Migration"
log_info "This script migrates your configuration to the new structure"
echo ""
# Check if Potions is installed
if [ ! -d "$POTIONS_HOME" ]; then
log_error "Potions is not installed at $POTIONS_HOME"
exit 1
fi
# Check if migration is needed
if ! check_migration_needed; then
exit 0
fi
# Confirm migration
if [ "$1" != "--force" ] && [ "$1" != "-f" ]; then
read -p "Continue with migration? [Y/n] " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Nn]$ ]]; then
log_info "Migration cancelled."
exit 0
fi
fi
# Perform migration
create_backup
migrate_files
print_completion
}
# Run main
main "$@"