Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::sync::Arc;
use tmux_botdomo::logger::{print_debug, print_error, print_info};
use tmux_botdomo::messages::{CliRequest, DaemonResponse, ResponseStatus, read_from_stream};
use tmux_botdomo::session::{Agent, AgentSessionInfo, TmuxLocation};
use tmux_botdomo::unix::{get_pid_file_path, get_socket_path};
use tmux_botdomo::unix::{get_pid_file_path, get_socket_path, get_tmux_session_id};
use tokio::io::AsyncWriteExt;
use tokio::net::{UnixListener, UnixStream};
use tokio::sync::RwLock;
Expand Down Expand Up @@ -256,11 +256,16 @@ async fn get_agent_locations(
])
.output()
.await?;
let session_id = get_tmux_session_id();
let tmux_location_map: HashMap<String, (String, String, String)> =
String::from_utf8_lossy(&tmux_ls_output.stdout)
.lines()
.filter_map(|s| {
let segs: Vec<&str> = s.split_whitespace().collect();
// Only fetch ones within the same tmux session
if segs[0] != session_id {
return None;
}
segs[3].strip_prefix("/dev/").map(|stripped_tty| {
(
stripped_tty.to_string(),
Expand Down Expand Up @@ -313,10 +318,6 @@ async fn get_agent_locations(
writable_session_info.insert(cwd.clone(), session);
}
print_info(&format!("Detected {agent} session for {cwd}"));
} else {
print_error(&format!(
"Can't gather enough information for {agent} session on pid {pid}",
));
}
}
Ok(())
Expand Down
24 changes: 18 additions & 6 deletions src/unix.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
pub const TMUX_BOTDOMO_SOCK_PATH: &str = "/tmp/tmux-botdomo.sock";

pub fn get_pid_file_path() -> String {
let session_id = get_tmux_session_id();
// TODO: XDG_RUNTIME_DIR?
format!(
"/tmp/tmux-botdomo-{}.pid",
std::env::var("USER").unwrap_or_else(|_| "unknown".to_string())
"/tmp/tmux-botdomo-{}-{}.pid",
std::env::var("USER").unwrap_or_else(|_| "unknown".to_string()),
session_id,
)
}

pub fn get_socket_path() -> String {
std::env::var("TMUX_BOTDOMO_SOCK_PATH").unwrap_or(TMUX_BOTDOMO_SOCK_PATH.to_string())
let session_id = get_tmux_session_id();
std::env::var("TMUX_BOTDOMO_SOCK_PATH").unwrap_or(format!(
"/tmp/tmux-botdomo-{}-{}.sock",
std::env::var("USER").unwrap_or_else(|_| "unknown".to_string()),
session_id,
))
}

#[cfg(feature = "test-mode")]
pub fn get_tmux_session_id() -> String {
"test".to_string()
}

pub fn get_tmux_session_id() -> Option<String> {
#[cfg(not(feature = "test-mode"))]
pub fn get_tmux_session_id() -> String {
std::process::Command::new("tmux")
.args(["display-message", "-p", "#{session_id}"])
.output()
.ok()
.and_then(|output| String::from_utf8(output.stdout).ok())
.map(|s| s.trim().to_string())
.unwrap_or("none".to_string())
}