-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathclose-swarm
More file actions
executable file
·75 lines (63 loc) · 2.28 KB
/
Copy pathclose-swarm
File metadata and controls
executable file
·75 lines (63 loc) · 2.28 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
#!/usr/bin/env zsh
set -euo pipefail
usage() {
echo "Usage: close-swarm [project-root]" >&2
echo "Stops the SwarmForge swarm for the given project (default: current directory)." >&2
exit 1
}
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
usage
fi
SELF_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_ROOT="$(cd "${1:-.}" && pwd)"
STATE_DIR="$PROJECT_ROOT/.swarmforge"
SOCKET_FILE="$STATE_DIR/tmux-socket"
SESSIONS_FILE="$STATE_DIR/sessions.tsv"
WINDOW_IDS_FILE="$STATE_DIR/window-ids"
WINDOWS_STATE_FILE="$STATE_DIR/windows.tsv"
if [[ ! -d "$STATE_DIR" ]]; then
echo "No SwarmForge swarm found at $PROJECT_ROOT (missing .swarmforge/)." >&2
exit 1
fi
if [[ -x "$SELF_DIR/swarmforge/scripts/swarm-cleanup.sh" ]]; then
SCRIPT_DIR="$SELF_DIR/swarmforge/scripts"
elif [[ -x "$SELF_DIR/swarm-cleanup.sh" ]]; then
SCRIPT_DIR="$SELF_DIR"
elif [[ -x "$PROJECT_ROOT/swarmforge/scripts/swarm-cleanup.sh" ]]; then
SCRIPT_DIR="$PROJECT_ROOT/swarmforge/scripts"
else
echo "Could not find swarm-cleanup.sh relative to $SELF_DIR or $PROJECT_ROOT." >&2
exit 1
fi
if [[ ! -f "$SOCKET_FILE" ]]; then
echo "No SwarmForge swarm found at $PROJECT_ROOT (missing .swarmforge/tmux-socket)." >&2
exit 1
fi
TMUX_SOCKET="$(tr -d '[:space:]' < "$SOCKET_FILE")"
if [[ -z "$TMUX_SOCKET" ]]; then
echo "No SwarmForge swarm found at $PROJECT_ROOT (empty tmux socket)." >&2
exit 1
fi
sessions=()
if [[ -f "$SESSIONS_FILE" ]]; then
while IFS=$'\t' read -r _index _role session _rest; do
[[ -n "${session:-}" ]] || continue
sessions+=("$session")
done < "$SESSIONS_FILE"
fi
if (( ${#sessions[@]} == 0 )) && [[ -f "$WINDOWS_STATE_FILE" ]]; then
while IFS=$'\t' read -r _index _window_id session _title; do
[[ -n "${session:-}" ]] || continue
sessions+=("$session")
done < "$WINDOWS_STATE_FILE"
fi
if (( ${#sessions[@]} == 0 )) && [[ -S "$TMUX_SOCKET" ]]; then
while IFS= read -r session; do
[[ -n "$session" ]] || continue
sessions+=("$session")
done < <(tmux -S "$TMUX_SOCKET" list-sessions -F '#{session_name}' 2>/dev/null || true)
fi
if [[ -n "${SWARMFORGE_TERMINAL:-}" && -z "${SWARMFORGE_TERMINAL_BACKEND:-}" ]]; then
export SWARMFORGE_TERMINAL_BACKEND="$SWARMFORGE_TERMINAL"
fi
"$SCRIPT_DIR/swarm-cleanup.sh" "$TMUX_SOCKET" "$WINDOW_IDS_FILE" "${sessions[@]+${sessions[@]}}"