diff options
author | Kunal Mohan <kunalmohan99@gmail.com> | 2021-05-22 21:16:44 +0530 |
---|---|---|
committer | Kunal Mohan <kunalmohan99@gmail.com> | 2021-05-22 22:19:50 +0530 |
commit | 1162d40ea00aec38907c7079e53c62a02c2c5eda (patch) | |
tree | d215180e62ea3a7b4d0604f652a65fac7a71b5ac /src | |
parent | 62a2d9cff2780262d76938d2f3364464b559e0ec (diff) |
check if session exists before attaching
Diffstat (limited to 'src')
-rw-r--r-- | src/list_sessions.rs | 69 | ||||
-rw-r--r-- | src/main.rs | 42 |
2 files changed, 74 insertions, 37 deletions
diff --git a/src/list_sessions.rs b/src/list_sessions.rs new file mode 100644 index 000000000..41437e1a7 --- /dev/null +++ b/src/list_sessions.rs @@ -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); +} diff --git a/src/main.rs b/src/main.rs index 319ad307d..5a6d5ed18 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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); -} |