-
Notifications
You must be signed in to change notification settings - Fork 310
Expand file tree
/
Copy pathupdate.sh
More file actions
executable file
·344 lines (294 loc) · 10.2 KB
/
update.sh
File metadata and controls
executable file
·344 lines (294 loc) · 10.2 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/env bash
#############################################################################
# OpenAgents Control Updater
# Updates existing OpenCode components to latest versions
#
# Compatible with:
# - macOS (bash 3.2+)
# - Linux (bash 3.2+)
# - Windows (Git Bash, WSL)
#
# Usage:
# ./update.sh # Auto-detect install location
# ./update.sh --install-dir PATH # Update a specific install path
#
# Environment variables:
# OPENCODE_INSTALL_DIR # Override default install directory
# OPENCODE_BRANCH # Branch to pull from (default: main)
#############################################################################
set -e
# Detect platform
PLATFORM="$(uname -s)"
case "$PLATFORM" in
Linux*) PLATFORM="Linux";;
Darwin*) PLATFORM="macOS";;
CYGWIN*|MINGW*|MSYS*) PLATFORM="Windows";;
*) PLATFORM="Unknown";;
esac
# Colors (disable on Windows terminals without color support)
if [ "$PLATFORM" = "Windows" ] && [ -z "$WT_SESSION" ] && [ -z "$ConEmuPID" ]; then
RED=''
GREEN=''
YELLOW=''
BLUE=''
CYAN=''
BOLD=''
NC=''
else
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m'
fi
BRANCH="${OPENCODE_BRANCH:-main}"
REPO_URL="https://raw.githubusercontent.com/darrenhinde/OpenAgentsControl/${BRANCH}"
# CLI argument for custom install dir (overrides env var)
CUSTOM_INSTALL_DIR=""
# Track backup files for cleanup on exit
BACKUP_FILES=()
# Clean up any leftover backup files on exit/interrupt
cleanup_backups() {
for f in "${BACKUP_FILES[@]}"; do
[ -f "$f" ] && rm -f "$f"
done
}
trap cleanup_backups EXIT INT TERM
#############################################################################
# Utility Functions
#############################################################################
print_success() { echo -e "${GREEN}✓${NC} $1"; }
print_info() { echo -e "${BLUE}ℹ${NC} $1"; }
print_warning() { echo -e "${YELLOW}⚠${NC} $1"; }
print_error() { echo -e "${RED}✗${NC} $1" >&2; }
print_step() { echo -e "\n${CYAN}${BOLD}▶${NC} $1\n"; }
print_header() {
echo -e "${CYAN}${BOLD}"
echo "╔════════════════════════════════════════════════════════════════╗"
echo "║ ║"
echo "║ OpenAgents Control Updater v1.1.0 ║"
echo "║ ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo -e "${NC}"
}
print_usage() {
echo "Usage: $0 [--install-dir PATH]"
echo ""
echo "Options:"
echo " --install-dir PATH Update a specific installation directory"
echo " --help Show this help message"
echo ""
echo "Environment variables:"
echo " OPENCODE_INSTALL_DIR Override the default installation directory"
echo " OPENCODE_BRANCH Branch to pull updates from (default: main)"
echo ""
echo "Examples:"
echo " # Auto-detect and update"
echo " $0"
echo ""
echo " # Update a global installation"
echo " $0 --install-dir ~/.config/opencode"
echo ""
echo " # Update via environment variable"
echo " export OPENCODE_INSTALL_DIR=~/.config/opencode && $0"
}
#############################################################################
# Path Resolution
#############################################################################
get_global_install_path() {
# Return platform-appropriate global installation path
case "$PLATFORM" in
macOS)
echo "${HOME}/.config/opencode"
;;
Linux)
echo "${HOME}/.config/opencode"
;;
Windows)
# Windows Git Bash/WSL: Use same as Linux
echo "${HOME}/.config/opencode"
;;
*)
echo "${HOME}/.config/opencode"
;;
esac
}
normalize_path() {
local input_path="$1"
# Handle empty path
if [ -z "$input_path" ]; then
echo ""
return 1
fi
local normalized_path
# Expand tilde to $HOME (works on Linux, macOS, Windows Git Bash)
if [[ $input_path == ~* ]]; then
normalized_path="${HOME}${input_path:1}"
else
normalized_path="$input_path"
fi
# Convert backslashes to forward slashes (Windows compatibility)
normalized_path="${normalized_path//\\//}"
# Remove trailing slashes
normalized_path="${normalized_path%/}"
# If path is relative, make it absolute based on current directory
if [[ ! "$normalized_path" = /* ]] && [[ ! "$normalized_path" =~ ^[A-Za-z]: ]]; then
normalized_path="$(pwd)/${normalized_path}"
fi
echo "$normalized_path"
return 0
}
resolve_install_dir() {
local custom_dir="$1"
# Priority: CLI arg → env var → auto-detect (local then global)
if [ -n "$custom_dir" ]; then
normalize_path "$custom_dir"
return
fi
if [ -n "$OPENCODE_INSTALL_DIR" ]; then
normalize_path "$OPENCODE_INSTALL_DIR"
return
fi
# Auto-detect: prefer local project install, fall back to global
local local_path
local_path="$(pwd)/.opencode"
local global_path
global_path=$(get_global_install_path)
if [ -d "$local_path" ]; then
echo "$local_path"
elif [ -d "$global_path" ]; then
echo "$global_path"
else
# Neither exists — return local path so main() gives a clear error
echo "$local_path"
fi
}
#############################################################################
# Update Logic
#############################################################################
update_component() {
local path="$1"
local install_dir="$2"
local relative_path="${path#"$install_dir"/}"
# Guard: reject paths that escaped the install dir
if [[ "$relative_path" == /* ]] || [[ "$relative_path" == *..* ]]; then
print_warning "Skipping suspicious path: $path"
return 1
fi
local url="${REPO_URL}/.opencode/${relative_path}"
local backup="${path}.backup"
cp "$path" "$backup"
BACKUP_FILES+=("$backup")
if curl -fsSL "$url" -o "$path" 2>/dev/null; then
print_success "Updated $path"
rm -f "$backup"
# Remove from tracking array (bash 3.2 compatible)
local new_backups=()
for f in "${BACKUP_FILES[@]}"; do
[ "$f" != "$backup" ] && new_backups+=("$f")
done
BACKUP_FILES=("${new_backups[@]+"${new_backups[@]}"}")
else
print_warning "Could not update $path — restoring backup"
mv "$backup" "$path"
return 1
fi
}
update_all_components() {
local install_dir="$1"
local updated=0
local failed=0
# Update markdown files
while IFS= read -r -d '' file; do
if update_component "$file" "$install_dir"; then
updated=$((updated + 1))
else
failed=$((failed + 1))
fi
done < <(find "$install_dir" -name "*.md" -type f -print0)
# Update TypeScript files
while IFS= read -r -d '' file; do
if update_component "$file" "$install_dir"; then
updated=$((updated + 1))
else
failed=$((failed + 1))
fi
done < <(find "$install_dir" -name "*.ts" -type f -not -path "*/node_modules/*" -print0)
# Update shell scripts inside install dir
while IFS= read -r -d '' file; do
if update_component "$file" "$install_dir"; then
updated=$((updated + 1))
else
failed=$((failed + 1))
fi
done < <(find "$install_dir" -name "*.sh" -type f -print0)
print_info "Updated: $updated file(s), failed: $failed file(s)"
}
#############################################################################
# Argument Parsing
#############################################################################
parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
--install-dir=*)
CUSTOM_INSTALL_DIR="${1#*=}"
if [ -z "$CUSTOM_INSTALL_DIR" ]; then
print_error "--install-dir requires a non-empty path"
exit 1
fi
shift
;;
--install-dir)
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
CUSTOM_INSTALL_DIR="$2"
shift 2
else
print_error "--install-dir requires a path argument"
exit 1
fi
;;
--help|-h)
print_usage
exit 0
;;
*)
print_error "Unknown option: $1"
print_usage
exit 1
;;
esac
done
}
#############################################################################
# Main
#############################################################################
main() {
parse_args "$@"
print_header
local install_dir
install_dir=$(resolve_install_dir "$CUSTOM_INSTALL_DIR")
if [ ! -d "$install_dir" ]; then
print_error "Installation directory not found: $install_dir"
echo ""
echo "Searched locations:"
echo " 1. --install-dir argument"
echo " 2. OPENCODE_INSTALL_DIR environment variable"
echo " 3. Local path: $(pwd)/.opencode"
echo " 4. Global path: $(get_global_install_path)"
echo ""
echo "Run install.sh first to install components, or specify the correct"
echo "path with: $0 --install-dir PATH"
exit 1
fi
if [ ! -w "$install_dir" ]; then
print_error "No write permission for: $install_dir"
exit 1
fi
print_info "Updating installation at: ${CYAN}${install_dir}${NC}"
print_step "Updating components..."
update_all_components "$install_dir"
print_success "Update complete!"
}
main "$@"