-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·200 lines (169 loc) · 4.64 KB
/
setup.sh
File metadata and controls
executable file
·200 lines (169 loc) · 4.64 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
#!/bin/bash
# Add (or remove) an automatic backup hook to Claude Code settings.
# This wires backup.sh as a SessionEnd hook so your config is backed up
# every time a session ends.
#
# Usage:
# ./setup.sh # interactive setup
# ./setup.sh --remove # remove the hook
set -e
CLAUDE_DIR="$HOME/.claude"
SETTINGS="$CLAUDE_DIR/settings.json"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
BACKUP_SCRIPT="$SCRIPT_DIR/backup.sh"
REMOVE=false
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--remove)
REMOVE=true
shift
;;
-h|--help)
echo "Usage: ./setup.sh [--remove]"
echo ""
echo " Interactive setup for automatic Claude Code config backups."
echo " --remove Remove the automatic backup hook"
exit 0
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
# Check dependencies
if ! command -v python3 &>/dev/null; then
echo "Error: python3 is required but not found."
exit 1
fi
if [ ! -f "$SETTINGS" ]; then
echo "Error: $SETTINGS not found. Is Claude Code installed?"
exit 1
fi
# Make backup script executable
chmod +x "$BACKUP_SCRIPT"
HOOK_MARKER="claude-code-config-backup"
if [ "$REMOVE" = true ]; then
# Remove the hook
UPDATED=$(python3 -c "
import json, sys
with open('$SETTINGS') as f:
settings = json.load(f)
hooks = settings.get('hooks', {})
session_end = hooks.get('SessionEnd', [])
# Filter out our hook entries
filtered = []
for entry in session_end:
entry_hooks = entry.get('hooks', [])
entry_hooks = [h for h in entry_hooks if '$HOOK_MARKER' not in h.get('command', '')]
if entry_hooks:
entry['hooks'] = entry_hooks
filtered.append(entry)
if filtered:
hooks['SessionEnd'] = filtered
elif 'SessionEnd' in hooks:
del hooks['SessionEnd']
settings['hooks'] = hooks
print(json.dumps(settings, indent=2))
" 2>/dev/null)
if [ -n "$UPDATED" ]; then
echo "$UPDATED" > "$SETTINGS"
echo "Removed automatic backup hook from settings.json."
else
echo "Error updating settings. No changes made."
exit 1
fi
exit 0
fi
# Check if hook already exists
if grep -q "$HOOK_MARKER" "$SETTINGS" 2>/dev/null; then
echo "Automatic backup hook is already configured."
echo "To reconfigure, remove first: ./setup.sh --remove"
exit 0
fi
# --- Interactive setup ---
echo "Claude Code Config Backup - Setup"
echo ""
# Ask for backup location
DEFAULT_DIR="$HOME/.claude-backups"
echo "Where should backups be stored?"
echo " Default: $DEFAULT_DIR"
read -p " Path (enter for default): " CUSTOM_DIR
echo ""
if [ -n "$CUSTOM_DIR" ]; then
# Expand ~ if present
BACKUP_TARGET="${CUSTOM_DIR/#\~/$HOME}"
else
BACKUP_TARGET="$DEFAULT_DIR"
fi
# Check if directory exists and has contents
if [ -d "$BACKUP_TARGET" ] && [ "$(ls -A "$BACKUP_TARGET" 2>/dev/null)" ]; then
echo " Note: $BACKUP_TARGET already exists and is not empty."
read -p " Continue with this location? (y/N) " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Setup cancelled."
exit 0
fi
echo ""
fi
# Ask for backup mode
echo "Backup mode:"
echo " 1) Latest only - each session overwrites the previous backup"
echo " 2) Snapshots - each session creates a timestamped copy"
read -p " Choose (1/2, default 1): " -n 1 -r MODE_CHOICE
echo ""
echo ""
SNAPSHOT=false
if [ "$MODE_CHOICE" = "2" ]; then
SNAPSHOT=true
fi
# Build the hook command
HOOK_COMMAND="$BACKUP_SCRIPT --target $BACKUP_TARGET"
if [ "$SNAPSHOT" = true ]; then
HOOK_COMMAND="$BACKUP_SCRIPT --snapshot --target $BACKUP_TARGET"
fi
# Add the hook
UPDATED=$(python3 -c "
import json
with open('$SETTINGS') as f:
settings = json.load(f)
hooks = settings.setdefault('hooks', {})
session_end = hooks.setdefault('SessionEnd', [])
new_hook = {
'type': 'command',
'command': '$HOOK_COMMAND',
'timeout': 10
}
# Look for an existing catch-all matcher
added = False
for entry in session_end:
if entry.get('matcher', '') == '':
entry.setdefault('hooks', []).append(new_hook)
added = True
break
if not added:
session_end.append({
'matcher': '',
'hooks': [new_hook]
})
print(json.dumps(settings, indent=2))
" 2>/dev/null)
if [ -n "$UPDATED" ]; then
echo "$UPDATED" > "$SETTINGS"
echo "Setup complete."
echo ""
echo " Backup location: $BACKUP_TARGET"
if [ "$SNAPSHOT" = true ]; then
echo " Mode: snapshots (timestamped copy each session)"
else
echo " Mode: latest (single copy, overwritten each session)"
fi
echo ""
echo "Your config will be backed up every time a Claude Code session ends."
echo "To remove: ./setup.sh --remove"
else
echo "Error updating settings. No changes made."
exit 1
fi