diff options
| author | Ellie Huxtable <ellie@atuin.sh> | 2026-02-13 11:37:58 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-02-13 11:37:58 -0800 |
| commit | d52c4d6003adced1f6763261a7f9132719be5533 (patch) | |
| tree | 94d2f7c815814135edbc2ee0af56d2cedd1a1724 /crates/atuin-daemon/src/client.rs | |
| parent | feat: add Atuin AI inline CLI MVP (#3178) (diff) | |
| download | atuin-d52c4d6003adced1f6763261a7f9132719be5533.zip | |
feat: add autostart and pid management to daemon (#3180)
Diffstat (limited to 'crates/atuin-daemon/src/client.rs')
| -rw-r--r-- | crates/atuin-daemon/src/client.rs | 51 |
1 files changed, 42 insertions, 9 deletions
diff --git a/crates/atuin-daemon/src/client.rs b/crates/atuin-daemon/src/client.rs index a4b4690e..05067bda 100644 --- a/crates/atuin-daemon/src/client.rs +++ b/crates/atuin-daemon/src/client.rs @@ -1,6 +1,7 @@ use eyre::{Context, Result}; #[cfg(windows)] use tokio::net::TcpStream; +use tonic::Code; use tonic::transport::{Channel, Endpoint, Uri}; use tower::service_fn; @@ -12,13 +13,41 @@ use tokio::net::UnixStream; use atuin_client::history::History; use crate::history::{ - EndHistoryRequest, StartHistoryRequest, history_client::HistoryClient as HistoryServiceClient, + EndHistoryReply, EndHistoryRequest, ShutdownRequest, StartHistoryReply, StartHistoryRequest, + StatusReply, StatusRequest, history_client::HistoryClient as HistoryServiceClient, }; pub struct HistoryClient { client: HistoryServiceClient<Channel>, } +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum DaemonClientErrorKind { + Connect, + Unavailable, + Unimplemented, + Other, +} + +#[must_use] +pub fn classify_error(error: &eyre::Report) -> DaemonClientErrorKind { + for cause in error.chain() { + if cause.downcast_ref::<tonic::transport::Error>().is_some() { + return DaemonClientErrorKind::Connect; + } + + if let Some(status) = cause.downcast_ref::<tonic::Status>() { + return match status.code() { + Code::Unavailable => DaemonClientErrorKind::Unavailable, + Code::Unimplemented => DaemonClientErrorKind::Unimplemented, + _ => DaemonClientErrorKind::Other, + }; + } + } + + DaemonClientErrorKind::Other +} + // Wrap the grpc client impl HistoryClient { #[cfg(unix)] @@ -67,7 +96,7 @@ impl HistoryClient { Ok(HistoryClient { client }) } - pub async fn start_history(&mut self, h: History) -> Result<String> { + pub async fn start_history(&mut self, h: History) -> Result<StartHistoryReply> { let req = StartHistoryRequest { command: h.command, cwd: h.cwd, @@ -76,9 +105,7 @@ impl HistoryClient { timestamp: h.timestamp.unix_timestamp_nanos() as u64, }; - let resp = self.client.start_history(req).await?; - - Ok(resp.into_inner().id) + Ok(self.client.start_history(req).await?.into_inner()) } pub async fn end_history( @@ -86,12 +113,18 @@ impl HistoryClient { id: String, duration: u64, exit: i64, - ) -> Result<(String, u64)> { + ) -> Result<EndHistoryReply> { let req = EndHistoryRequest { id, duration, exit }; - let resp = self.client.end_history(req).await?; - let resp = resp.into_inner(); + Ok(self.client.end_history(req).await?.into_inner()) + } + + pub async fn status(&mut self) -> Result<StatusReply> { + Ok(self.client.status(StatusRequest {}).await?.into_inner()) + } - Ok((resp.id, resp.idx)) + pub async fn shutdown(&mut self) -> Result<bool> { + let resp = self.client.shutdown(ShutdownRequest {}).await?.into_inner(); + Ok(resp.accepted) } } |
