-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestore-aiconfig
More file actions
executable file
·188 lines (164 loc) · 5.67 KB
/
restore-aiconfig
File metadata and controls
executable file
·188 lines (164 loc) · 5.67 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
#!/usr/bin/env bash
# Restore AI configuration files from agents/config/
# Environment setup
# -----------------------------------------------------------------------------
set -o pipefail
[[ ${DEBUG-} ]] && set -o xtrace
SCRIPT_DIR="$(cd "${BASH_SOURCE[0]%/*}" || exit 1; pwd)"
[[ ":${PATH}:" != *:"${SCRIPT_DIR}":* ]] && export PATH="${SCRIPT_DIR}:${PATH}"
source "${SCRIPT_DIR}/bash_modules/terminal.sh"
[[ -z ${BASH_MODULES_DIR-} ]] && echo "ERROR: terminal.sh module missing" && exit 1
function print_usage() {
cat <<EOF
Usage: $(basename "$0") [-h|--help]
Restore AI configuration files from agents/config/
This script copies:
- agents/config/claude-code/settings.json to ~/.claude/
- agents/commands/ to ~/.claude/commands/
- agents/config/claude-code/agents/ to ~/.claude/agents/
SAFETY FEATURE:
Before overwriting any existing files or directories, this script creates
a backup in ~/Backup/<yyyy-mm-dd>-restore-config/ to prevent data loss.
Non-existent files or directories are skipped without error.
Optional arguments:
-h, --help Show this help message and exit
EOF
}
if [[ "${1-}" == "-h" || "${1-}" == "--help" ]]; then
print_usage
exit 0
fi
function ctrlc_trap() {
log_newline
log_warning "Script interrupted. Exiting."
exit 130
}
trap ctrlc_trap SIGINT
# OS Detection
# -----------------------------------------------------------------------------
function get_os_name() {
if command -v fastfetch >/dev/null 2>&1; then
local os_name
os_name="$(fastfetch -l none -s OS | cut -d':' -f2 | xargs | cut -d' ' -f1)"
echo "${os_name}"
else
case "$(uname -s)" in
Linux)
echo "Linux"
;;
Darwin)
echo "macOS"
;;
*)
echo "Unknown"
;;
esac
fi
}
# Safety Backup Function
# -----------------------------------------------------------------------------
function safety_backup() {
local source_path="$1"
local backup_name="$2"
if [[ -e "${source_path}" ]]; then
local backup_path="${SAFETY_BACKUP_DIR}/${backup_name}"
local backup_dir
backup_dir="$(dirname "${backup_path}")"
if ! mkdir -p "${backup_dir}"; then
log_error "Failed to create backup directory: '${backup_dir}'"
return 1
fi
if [[ -d "${source_path}" ]]; then
# Directory - use rsync without --delete for additive backup
if rsync -a "${source_path}/" "${backup_path}/"; then
log_message " Backed up directory '${source_path}' to '${backup_path}'"
return 0
else
log_error "Failed to backup directory '${source_path}'"
return 1
fi
else
# File - use rsync
if rsync -a "${source_path}" "${backup_path}"; then
log_message " Backed up file '${source_path}' to '${backup_path}'"
return 0
else
log_error "Failed to backup file '${source_path}'"
return 1
fi
fi
fi
return 0
}
# Title and Setup
# -----------------------------------------------------------------------------
log_title "Configuration Restore"
declare OS_NAME
OS_NAME="$(get_os_name)"
declare -r OS_NAME
declare -r CLAUDE_SOURCE_DIR="${SCRIPT_DIR}/agents/config/claude-code"
declare -r COMMANDS_SOURCE_DIR="${SCRIPT_DIR}/agents/commands"
declare -r CLAUDE_TARGET_DIR="${HOME}/.claude"
declare -r CONFIG_SOURCE_DIR="${SCRIPT_DIR}/agents/config"
declare BACKUP_DATE
BACKUP_DATE="$(date +%Y-%m-%d)"
declare -r BACKUP_DATE
declare -r SAFETY_BACKUP_DIR="${HOME}/Backup/${BACKUP_DATE}-restore-config"
# Create target directories if they don't exist
if ! mkdir -p "${CLAUDE_TARGET_DIR}" "${SAFETY_BACKUP_DIR}"; then
log_error "Failed to create target directories"
exit 1
fi
log_heading "Restore Configuration"
log_message "OS Detected: '${OS_NAME}'"
log_message "Config Source: '${CONFIG_SOURCE_DIR}'"
log_message "Claude Source: '${CLAUDE_SOURCE_DIR}'"
log_message "Commands Source: '${COMMANDS_SOURCE_DIR}'"
log_message "Claude Target: '${CLAUDE_TARGET_DIR}'"
log_message "Safety Backup: '${SAFETY_BACKUP_DIR}'"
# Restore settings.json
# -----------------------------------------------------------------------------
log_subheading "Settings File"
if [[ -f "${CLAUDE_SOURCE_DIR}/settings.json" ]]; then
# Safety backup existing file
safety_backup "${CLAUDE_TARGET_DIR}/settings.json" "claude-code/settings.json"
if rsync -a "${CLAUDE_SOURCE_DIR}/settings.json" "${CLAUDE_TARGET_DIR}/"; then
log_success "settings.json restored"
else
log_failure "Failed to restore settings.json"
exit 1
fi
else
log_warning "settings.json not found in backup, skipping"
fi
# Restore commands directory
# -----------------------------------------------------------------------------
log_subheading "Commands Directory"
if [[ -d "${COMMANDS_SOURCE_DIR}" ]]; then
# Safety backup existing directory
safety_backup "${CLAUDE_TARGET_DIR}/commands" "claude-code/commands"
if rsync -a --delete "${COMMANDS_SOURCE_DIR}/" "${CLAUDE_TARGET_DIR}/commands/"; then
log_success "commands directory restored"
else
log_failure "Failed to restore commands directory"
exit 1
fi
else
log_warning "commands directory not found in backup, skipping"
fi
# Restore agents directory
# -----------------------------------------------------------------------------
log_subheading "Agents Directory"
if [[ -d "${CLAUDE_SOURCE_DIR}/agents" ]]; then
# Safety backup existing directory
safety_backup "${CLAUDE_TARGET_DIR}/agents" "claude-code/agents"
if rsync -a --delete "${CLAUDE_SOURCE_DIR}/agents/" "${CLAUDE_TARGET_DIR}/agents/"; then
log_success "agents directory restored"
else
log_failure "Failed to restore agents directory"
exit 1
fi
else
log_warning "agents directory not found in backup, skipping"
fi
log_done "Configuration restore completed"