Skip to content

Commit dfea81a

Browse files
authored
chore: Convert it to session-based (#22)
* chore: Convert it to session-based * fix linux test * go agane? * Revert "go agane?" This reverts commit 7de3d70. * Revert "fix linux test" This reverts commit dcfe818. * mock get_tmux_session_id?
1 parent 10c44d5 commit dfea81a

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/daemon.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::sync::Arc;
99
use tmux_botdomo::logger::{print_debug, print_error, print_info};
1010
use tmux_botdomo::messages::{CliRequest, DaemonResponse, ResponseStatus, read_from_stream};
1111
use tmux_botdomo::session::{Agent, AgentSessionInfo, TmuxLocation};
12-
use tmux_botdomo::unix::{get_pid_file_path, get_socket_path};
12+
use tmux_botdomo::unix::{get_pid_file_path, get_socket_path, get_tmux_session_id};
1313
use tokio::io::AsyncWriteExt;
1414
use tokio::net::{UnixListener, UnixStream};
1515
use tokio::sync::RwLock;
@@ -256,11 +256,16 @@ async fn get_agent_locations(
256256
])
257257
.output()
258258
.await?;
259+
let session_id = get_tmux_session_id();
259260
let tmux_location_map: HashMap<String, (String, String, String)> =
260261
String::from_utf8_lossy(&tmux_ls_output.stdout)
261262
.lines()
262263
.filter_map(|s| {
263264
let segs: Vec<&str> = s.split_whitespace().collect();
265+
// Only fetch ones within the same tmux session
266+
if segs[0] != session_id {
267+
return None;
268+
}
264269
segs[3].strip_prefix("/dev/").map(|stripped_tty| {
265270
(
266271
stripped_tty.to_string(),
@@ -313,10 +318,6 @@ async fn get_agent_locations(
313318
writable_session_info.insert(cwd.clone(), session);
314319
}
315320
print_info(&format!("Detected {agent} session for {cwd}"));
316-
} else {
317-
print_error(&format!(
318-
"Can't gather enough information for {agent} session on pid {pid}",
319-
));
320321
}
321322
}
322323
Ok(())

src/unix.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
1-
pub const TMUX_BOTDOMO_SOCK_PATH: &str = "/tmp/tmux-botdomo.sock";
2-
31
pub fn get_pid_file_path() -> String {
2+
let session_id = get_tmux_session_id();
43
// TODO: XDG_RUNTIME_DIR?
54
format!(
6-
"/tmp/tmux-botdomo-{}.pid",
7-
std::env::var("USER").unwrap_or_else(|_| "unknown".to_string())
5+
"/tmp/tmux-botdomo-{}-{}.pid",
6+
std::env::var("USER").unwrap_or_else(|_| "unknown".to_string()),
7+
session_id,
88
)
99
}
1010

1111
pub fn get_socket_path() -> String {
12-
std::env::var("TMUX_BOTDOMO_SOCK_PATH").unwrap_or(TMUX_BOTDOMO_SOCK_PATH.to_string())
12+
let session_id = get_tmux_session_id();
13+
std::env::var("TMUX_BOTDOMO_SOCK_PATH").unwrap_or(format!(
14+
"/tmp/tmux-botdomo-{}-{}.sock",
15+
std::env::var("USER").unwrap_or_else(|_| "unknown".to_string()),
16+
session_id,
17+
))
18+
}
19+
20+
#[cfg(feature = "test-mode")]
21+
pub fn get_tmux_session_id() -> String {
22+
"test".to_string()
1323
}
1424

15-
pub fn get_tmux_session_id() -> Option<String> {
25+
#[cfg(not(feature = "test-mode"))]
26+
pub fn get_tmux_session_id() -> String {
1627
std::process::Command::new("tmux")
1728
.args(["display-message", "-p", "#{session_id}"])
1829
.output()
1930
.ok()
2031
.and_then(|output| String::from_utf8(output.stdout).ok())
2132
.map(|s| s.trim().to_string())
33+
.unwrap_or("none".to_string())
2234
}

0 commit comments

Comments
 (0)