check if session exists before attaching

This commit is contained in:
Kunal Mohan 2021-05-22 21:16:44 +05:30
parent 62a2d9cff2
commit 1162d40ea0
2 changed files with 74 additions and 37 deletions

69
src/list_sessions.rs Normal file
View file

@ -0,0 +1,69 @@
use std::os::unix::fs::FileTypeExt;
use std::{fs, io, process};
use zellij_utils::consts::ZELLIJ_SOCK_DIR;
fn get_sessions() -> Result<Vec<String>, io::ErrorKind> {
match fs::read_dir(&*ZELLIJ_SOCK_DIR) {
Ok(files) => {
let mut sessions = Vec::new();
files.for_each(|file| {
let file = file.unwrap();
if file.file_type().unwrap().is_socket() {
sessions.push(file.file_name().into_string().unwrap());
}
});
Ok(sessions)
}
Err(err) => {
if let io::ErrorKind::NotFound = err.kind() {
Ok(Vec::with_capacity(0))
} else {
Err(err.kind())
}
}
}
}
pub(crate) fn list_sessions() {
let exit_code = match get_sessions() {
Ok(sessions) => {
if sessions.is_empty() {
println!("No active zellij sessions found.");
} else {
let curr_session =
std::env::var("ZELLIJ_SESSION_NAME").unwrap_or_else(|_| "".into());
sessions.iter().for_each(|session| {
let suffix = if curr_session == *session {
" (current)"
} else {
""
};
println!("{}{}", session, suffix);
})
}
0
}
Err(e) => {
eprintln!("Error occured: {:?}", e);
1
}
};
process::exit(exit_code);
}
pub(crate) fn assert_session(name: &str) {
let exit_code = match get_sessions() {
Ok(sessions) => {
if sessions.iter().any(|s| s == name) {
return;
}
println!("No session named {:?} found.", name);
0
}
Err(e) => {
eprintln!("Error occured: {:?}", e);
1
}
};
process::exit(exit_code);
}

View file

@ -1,14 +1,15 @@
mod list_sessions;
#[cfg(test)]
mod tests;
use list_sessions::{assert_session, list_sessions};
use std::convert::TryFrom;
use std::os::unix::fs::FileTypeExt;
use std::{fs, io, process};
use std::process;
use zellij_client::{os_input_output::get_client_os_input, start_client};
use zellij_server::{os_input_output::get_server_os_input, start_server};
use zellij_utils::{
cli::{CliArgs, Command, Sessions},
consts::{ZELLIJ_SOCK_DIR, ZELLIJ_TMP_DIR, ZELLIJ_TMP_LOG_DIR},
consts::{ZELLIJ_TMP_DIR, ZELLIJ_TMP_LOG_DIR},
input::config::Config,
logging::*,
setup::Setup,
@ -55,6 +56,7 @@ pub fn main() {
force,
})) = opts.command.clone()
{
assert_session(&session_name);
start_client(
Box::new(os_input),
opts,
@ -66,37 +68,3 @@ pub fn main() {
}
}
}
fn list_sessions() {
match fs::read_dir(&*ZELLIJ_SOCK_DIR) {
Ok(files) => {
let mut is_empty = true;
let session_name = std::env::var("ZELLIJ_SESSION_NAME").unwrap_or_else(|_| "".into());
files.for_each(|file| {
let file = file.unwrap();
if file.file_type().unwrap().is_socket() {
let fname = file.file_name().into_string().unwrap();
let suffix = if session_name == fname {
" (current)"
} else {
""
};
println!("{}{}", fname, suffix);
is_empty = false;
}
});
if is_empty {
println!("No active zellij sessions found.");
}
}
Err(err) => {
if let io::ErrorKind::NotFound = err.kind() {
println!("No active zellij sessions found.");
} else {
eprintln!("Error occured: {}", err);
process::exit(1);
}
}
}
process::exit(0);
}