-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoc
More file actions
executable file
·56 lines (45 loc) · 1.13 KB
/
oc
File metadata and controls
executable file
·56 lines (45 loc) · 1.13 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
#!/usr/bin/env bash
set -euo pipefail
PORT_START=30000
PORT_END=31000
if [ $# -lt 1 ]; then
echo "Usage: oc <path> [path ...]" >&2
exit 1
fi
is_serving() {
local target="$1"
for pid in $(pgrep -f "opencode serve" 2>/dev/null); do
local cwd
cwd="$(readlink -f /proc/"$pid"/cwd 2>/dev/null)" || continue
[ "$cwd" = "$target" ] && return 0
done
return 1
}
port_in_use() {
ss -tlnH "sport = :$1" 2>/dev/null | grep -q . && return 0
return 1
}
next_port=$PORT_START
for dir in "$@"; do
abs="$(realpath "$dir" 2>/dev/null || echo "$dir")"
if [ ! -d "$abs" ]; then
echo "[skip] '$abs' is not a directory" >&2
continue
fi
if is_serving "$abs"; then
echo "[skip] already serving: $abs"
continue
fi
# Find next available port in range
while port_in_use "$next_port"; do
next_port=$((next_port + 1))
if [ "$next_port" -gt "$PORT_END" ]; then
echo "[error] no free ports in $PORT_START-$PORT_END" >&2
exit 1
fi
done
echo "[start] opencode serve in $abs (port $next_port)"
(cd "$abs" && opencode serve --port "$next_port" &)
next_port=$((next_port + 1))
done
wait