Skip to content

Commit 4f711bf

Browse files
authored
chore: Show more information in status command (#23)
1 parent dfea81a commit 4f711bf

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/cli.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ async fn main() -> anyhow::Result<()> {
4040
if response.status == ResponseStatus::Success {
4141
let session_info: HashMap<String, AgentSessionInfo> =
4242
serde_json::from_value(response.payload.unwrap())?;
43+
// HACK: taking advantage of the message field. Should probably serialized it in
44+
// the payload?
45+
println!("{}", response.message.unwrap_or("".to_string()));
4346
for (i, (cwd, session)) in session_info.iter().enumerate() {
4447
println!(
4548
"Session #{i} - Agent: {}, cwd: {cwd}, pid: {}, tmux location: {}",

src/daemon.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use chrono::{DateTime, Local};
12
use clap::{Parser, Subcommand};
23
use nix::sys::signal;
34
use nix::unistd::Pid;
@@ -57,13 +58,29 @@ impl Drop for FileGuard {
5758
}
5859
}
5960

61+
struct DaemonState {
62+
tmux_session_id: String,
63+
start_ts: DateTime<Local>,
64+
}
65+
66+
impl DaemonState {
67+
fn new(tmux_session_id: String) -> Self {
68+
Self {
69+
tmux_session_id,
70+
start_ts: Local::now(),
71+
}
72+
}
73+
}
74+
6075
async fn start_daemon() -> anyhow::Result<()> {
6176
env_logger::Builder::new()
6277
.format(|buf, record| write!(buf, "{}", record.args()))
6378
.parse_default_env()
6479
.init();
6580

6681
print_info("Starting daemon...");
82+
let session_id = get_tmux_session_id();
83+
let daemon_state = Arc::new(DaemonState::new(session_id));
6784
// TODO: check instance, socket
6885
let pid_path = PathBuf::from(get_pid_file_path());
6986
if pid_path.exists() {
@@ -91,16 +108,17 @@ async fn start_daemon() -> anyhow::Result<()> {
91108
tokio::select! {
92109
Ok((stream, _)) = listener.accept() => {
93110
let session_info_clone = session_info.clone();
111+
let daemon_state_clone = daemon_state.clone();
94112
tokio::spawn(async move {
95-
if let Err(e) = handle_connection(stream, session_info_clone).await {
113+
if let Err(e) = handle_connection(stream, session_info_clone, daemon_state_clone).await {
96114
print_error(&format!("Connect error {e}"));
97115
};
98116
});
99117
}
100118
_ = main_loop_interval.tick() => {
101119
// TODO: state management
102120
let session_info_clone = session_info.clone();
103-
get_agent_locations(session_info_clone).await?;
121+
get_agent_locations(session_info_clone, daemon_state.as_ref()).await?;
104122
}
105123
_ = tokio::signal::ctrl_c() => {
106124
print_info("Received SIGINT, shutting down...");
@@ -119,6 +137,7 @@ async fn start_daemon() -> anyhow::Result<()> {
119137
async fn handle_connection(
120138
mut stream: UnixStream,
121139
session_info: Arc<RwLock<HashMap<String, AgentSessionInfo>>>,
140+
daemon_state: Arc<DaemonState>,
122141
) -> anyhow::Result<()> {
123142
let buffer = read_from_stream(&mut stream).await?;
124143
print_debug(&format!("Received {buffer}"));
@@ -133,7 +152,11 @@ async fn handle_connection(
133152
DaemonResponse {
134153
status: ResponseStatus::Success,
135154
payload: Some(serialized),
136-
message: None,
155+
message: Some(format!(
156+
"Daemon started at {}, up for {} seconds",
157+
daemon_state.start_ts.format("%Y-%m-%d %H:%M:%S"),
158+
(Local::now() - daemon_state.start_ts).as_seconds_f64(),
159+
)),
137160
}
138161
} else {
139162
DaemonResponse {
@@ -237,6 +260,7 @@ async fn stop_daemon() -> anyhow::Result<()> {
237260
#[cfg(feature = "test-mode")]
238261
async fn get_agent_locations(
239262
_session_info: Arc<RwLock<HashMap<String, AgentSessionInfo>>>,
263+
_daemon_state: &DaemonState,
240264
) -> anyhow::Result<()> {
241265
print_info("Test mode: skipping agent location detection");
242266
Ok(())
@@ -245,6 +269,7 @@ async fn get_agent_locations(
245269
#[cfg(not(feature = "test-mode"))]
246270
async fn get_agent_locations(
247271
session_info: Arc<RwLock<HashMap<String, AgentSessionInfo>>>,
272+
daemon_state: &DaemonState,
248273
) -> anyhow::Result<()> {
249274
// TODO: clean up, consolidate, etc.
250275
let tmux_ls_output = tokio::process::Command::new("tmux")
@@ -256,14 +281,13 @@ async fn get_agent_locations(
256281
])
257282
.output()
258283
.await?;
259-
let session_id = get_tmux_session_id();
260284
let tmux_location_map: HashMap<String, (String, String, String)> =
261285
String::from_utf8_lossy(&tmux_ls_output.stdout)
262286
.lines()
263287
.filter_map(|s| {
264288
let segs: Vec<&str> = s.split_whitespace().collect();
265289
// Only fetch ones within the same tmux session
266-
if segs[0] != session_id {
290+
if segs[0] != daemon_state.tmux_session_id {
267291
return None;
268292
}
269293
segs[3].strip_prefix("/dev/").map(|stripped_tty| {

0 commit comments

Comments
 (0)