-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathautoshpool
executable file
·96 lines (78 loc) · 2.1 KB
/
autoshpool
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
#!/bin/bash
# autoshpool - connect to first existing shpool session or start a new one
get_current_shpool_session() {
echo "$SHPOOL_SESSION_NAME"
}
exit_if_already_attached() {
session=$(get_current_shpool_session)
if test -n "$session"; then
echo "Already connected to shpool session $session"
exit 1
fi
}
abort_if_hung() {
if ! timeout 1 shpool list >/dev/null; then
echo "shpool not responding" >&2
exit 124
fi
}
get_prefix() {
# Use the current project name with a colon at the end, or empty string.
local prefix="$(basename "$(rootdir)")"
test -n "$prefix" && echo "$prefix:"
}
get_shpool_sessions() {
# Session names can be empty, so need handle rows with only 2 columns specially.
shpool list | awk 'NR > 1 && NF == 3 && $1 ~ /^'"${SHPOOL_PREFIX:-.}"'/ { print }'
}
get_disconnected_shpool_sessions() {
get_shpool_sessions | awk '$NF == "disconnected" { print $1 }'
}
attach_first_disconnected() {
declare -a disconnected=( $(get_disconnected_shpool_sessions) )
if test "${#disconnected[@]}" -gt 0; then
session="${disconnected[0]}"
echo "Attaching shpool session $session"
exec shpool attach "$session"
fi
}
create_and_attach_next() {
declare -A sessions
for session in $(get_shpool_sessions); do
sessions[$session]=1
done
for num in $(seq 1 100); do
declare session="$SHPOOL_PREFIX$num"
if test -z "${sessions[$session]}"; then
echo "Creating shpool session $session"
exec shpool attach "$session"
fi
done
echo "You already have more than 100 sessions??" >&2
exit 2
}
autoshpool_main() {
#exec >>"$HOME/.autoshpool.log" 2>>"$HOME/.autoshpool.log"
exit_if_already_attached
source "$HOME/.shrc.vcs"
SHPOOL_PREFIX="${SHPOOL_PREFIX:-$(get_prefix)}"
abort_if_hung
attach_first_disconnected
create_and_attach_next
}
is_sourced() {
if [ -n "$ZSH_VERSION" ]; then
case $ZSH_EVAL_CONTEXT in *:file:*)
return 0;;
esac
else
case ${0##*/} in dash|-dash|bash|-bash|ksh|-ksh|sh|-sh)
return 0;;
esac
fi
return 1
}
test $# -gt 0 && exec shpool "$@"
if ! is_sourced; then
autoshpool_main
fi