forked from CodebuffAI/codebuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtmux-stop.sh
More file actions
executable file
·154 lines (138 loc) · 3.98 KB
/
Copy pathtmux-stop.sh
File metadata and controls
executable file
·154 lines (138 loc) · 3.98 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
#!/usr/bin/env bash
#######################################################################
# tmux-stop.sh - Stop a tmux session
#######################################################################
#
# DESCRIPTION:
# Kills a tmux session. Use this to clean up after testing.
# Silently succeeds if session doesn't exist (idempotent).
#
# USAGE:
# ./scripts/tmux/tmux-stop.sh SESSION_NAME [OPTIONS]
#
# ARGUMENTS:
# SESSION_NAME Name of the tmux session to stop
#
# OPTIONS:
# --all Stop ALL test sessions (tui-test-* and cli-test-*)
# --list List all active tmux sessions first
# --help Show this help message
#
# EXAMPLES:
# # Stop a specific session
# ./scripts/tmux/tmux-stop.sh tui-test-123
#
# # Stop all test sessions
# ./scripts/tmux/tmux-stop.sh --all
#
# # List sessions then stop one
# ./scripts/tmux/tmux-stop.sh --list tui-test-123
#
# EXIT CODES:
# 0 - Success (session stopped or already doesn't exist)
# 1 - Error (invalid arguments)
#
#######################################################################
set -e
# Defaults
STOP_ALL=false
LIST_FIRST=false
# Check minimum arguments
if [[ $# -lt 1 ]]; then
echo "Usage: $0 SESSION_NAME [OPTIONS]" >&2
echo " $0 --all" >&2
echo "Run with --help for more information" >&2
exit 1
fi
# First argument handling
SESSION_NAME=""
# Handle --help and --all as first arg
case "$1" in
--help)
head -n 40 "$0" | tail -n +2 | sed 's/^# //' | sed 's/^#//'
exit 0
;;
--all)
STOP_ALL=true
shift
;;
--list)
LIST_FIRST=true
shift
if [[ $# -gt 0 && "$1" != "--"* ]]; then
SESSION_NAME="$1"
shift
fi
;;
*)
SESSION_NAME="$1"
shift
;;
esac
# Parse remaining arguments
while [[ $# -gt 0 ]]; do
case $1 in
--all)
STOP_ALL=true
shift
;;
--list)
LIST_FIRST=true
shift
;;
--help)
head -n 40 "$0" | tail -n +2 | sed 's/^# //' | sed 's/^#//'
exit 0
;;
*)
if [[ -z "$SESSION_NAME" ]]; then
SESSION_NAME="$1"
fi
shift
;;
esac
done
# List sessions if requested
if [[ "$LIST_FIRST" == true ]]; then
echo "Active tmux sessions:"
tmux list-sessions 2>/dev/null || echo " (none)"
echo ""
fi
# Stop all test sessions
if [[ "$STOP_ALL" == true ]]; then
# Get all tui-test-* sessions (and legacy cli-test-* for backward compatibility)
SESSIONS=$(tmux list-sessions -F '#{session_name}' 2>/dev/null | grep -E '^(tui-test-|cli-test-)' || true)
if [[ -z "$SESSIONS" ]]; then
echo "No tui-test-* or cli-test-* sessions found"
exit 0
fi
COUNT=0
while IFS= read -r session; do
tmux kill-session -t "$session" 2>/dev/null && ((COUNT++)) || true
done <<< "$SESSIONS"
echo "Stopped $COUNT session(s)"
exit 0
fi
# Check if session name was provided
if [[ -z "$SESSION_NAME" ]]; then
echo "❌ No session name specified" >&2
echo " Use --all to stop all test sessions" >&2
exit 1
fi
# Update session-info.yaml status to 'stopped' before killing
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
SESSION_DIR="$PROJECT_ROOT/debug/tmux-sessions/$SESSION_NAME"
SESSION_INFO="$SESSION_DIR/session-info.yaml"
if [[ -f "$SESSION_INFO" ]]; then
# Update status to 'stopped' and add stopped timestamp
STOPPED_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
if [[ "$(uname)" == "Darwin" ]]; then
sed -i '' "s/^status: active$/status: stopped/" "$SESSION_INFO"
else
sed -i "s/^status: active$/status: stopped/" "$SESSION_INFO"
fi
echo "stopped: $STOPPED_TIME" >> "$SESSION_INFO"
fi
# Stop the specific session (silently succeed if not found)
tmux kill-session -t "$SESSION_NAME" 2>/dev/null || true