Add -d flag to detect if a session exists

This commit is contained in:
Ingraham, Daniel James (GRC-LTV0) 2021-09-23 19:34:17 -04:00
parent 8c32909a15
commit 40b2714221
3 changed files with 43 additions and 1 deletions

View File

@ -25,6 +25,10 @@
.Cm command Op args ... .Cm command Op args ...
. .
.Nm .Nm
.Fl d
.Cm name
.
.Nm
.Fl n .Fl n
.Op options ... .Op options ...
.Cm name .Cm name
@ -84,6 +88,8 @@ Attach to an existing session.
Try to connect to an existing session, upon failure create said session and attach immediately to it. Try to connect to an existing session, upon failure create said session and attach immediately to it.
.It Fl c .It Fl c
Create a new session and attach immediately to it. Create a new session and attach immediately to it.
.It Fl d
Detect if the specified session exists, returning a successful exit status if true.
.It Fl n .It Fl n
Create a new session but do not attach to it. Create a new session but do not attach to it.
.El .El

View File

@ -606,11 +606,12 @@ int main(int argc, char *argv[]) {
server.name = basename(argv[0]); server.name = basename(argv[0]);
gethostname(server.host+1, sizeof(server.host) - 1); gethostname(server.host+1, sizeof(server.host) - 1);
while ((opt = getopt(argc, argv, "aAclne:fpqrv")) != -1) { while ((opt = getopt(argc, argv, "aAcdlne:fpqrv")) != -1) {
switch (opt) { switch (opt) {
case 'a': case 'a':
case 'A': case 'A':
case 'c': case 'c':
case 'd':
case 'n': case 'n':
action = opt; action = opt;
break; break;
@ -712,6 +713,13 @@ int main(int argc, char *argv[]) {
goto redo; goto redo;
} }
break; break;
case 'd':
if (session_exists(server.session_name)) {
return EXIT_SUCCESS;
} else {
return EXIT_FAILURE;
}
break;
} }
return 0; return 0;

View File

@ -171,6 +171,33 @@ run_test_dvtm() {
fi fi
} }
run_test_detect_session() {
check_environment || return 1;
local name="$1"
local cmd="$2"
local output="$name.out"
local output_expected="$name.expected"
TESTS_RUN=$((TESTS_RUN + 1))
echo -n "Running test: $name "
$cmd >/dev/null 2>&1
expected_abduco_epilog "$name" $? > "$output_expected" 2>&1
if detach | $ABDUCO $ABDUCO_OPTS -c "$name" $cmd >/dev/null 2>&1 && sleep 3 &&
$ABDUCO -d "$name" &&
$ABDUCO -a "$name" 2>&1 | tail -1 | sed 's/.$//' > "$output" &&
diff -u "$output_expected" "$output" && check_environment; then
rm "$output" "$output_expected"
TESTS_OK=$((TESTS_OK + 1))
echo "OK"
return 0
else
echo "FAIL"
return 1
fi
}
test_non_existing_command || echo "Execution of non existing command FAILED" test_non_existing_command || echo "Execution of non existing command FAILED"
run_test_attached "awk" "awk 'BEGIN {for(i=1;i<=1000;i++) print i}'" run_test_attached "awk" "awk 'BEGIN {for(i=1;i<=1000;i++) print i}'"
@ -207,6 +234,7 @@ EOT
chmod +x long-running.sh chmod +x long-running.sh
run_test_attached_detached "attach-detach" "./long-running.sh" run_test_attached_detached "attach-detach" "./long-running.sh"
run_test_detect_session "detect-session" "./long-running.sh"
rm ./long-running.sh rm ./long-running.sh