diff options
| author | Ellie Huxtable <ellie@elliehuxtable.com> | 2024-05-08 12:09:04 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-08 12:09:04 +0100 |
| commit | bce0faa1c2dc221b0ff77d2cd647bfb2a48ffa7e (patch) | |
| tree | 4d66bd95b151d3bab4cabf8799805c739f608bc4 /crates/atuin-daemon/src/client.rs | |
| parent | fix(config): add quotes for strategy value in comment (#1993) (diff) | |
| download | atuin-bce0faa1c2dc221b0ff77d2cd647bfb2a48ffa7e.zip | |
feat: add background daemon (#2006)
* init daemon crate
* wip
* minimal functioning daemon, needs cleanup for sure
* better errors
* add signal cleanup
* logging
* things
* add sync worker
* move daemon crate
* 30s -> 5mins
* make clippy happy
* fix stuff maybe?
* fmt
* trim packages
* rate limit fix
* more protoc huh
* this makes no sense, why linux why
* can it install literally just curl
* windows in ci is slow, and all the newer things will not work there. disable the daemon feature and it will build
* add daemon feature
* maybe this
* ok wut where is protoc
* try setting protoc
* hm
* try copying protoc
* remove optional
* add cross config
* idk nix
* does nix want this?
* some random pkg I found does this
* uh oh
* hack, be gone!
* update contributing
Diffstat (limited to '')
| -rw-r--r-- | crates/atuin-daemon/src/client.rs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/crates/atuin-daemon/src/client.rs b/crates/atuin-daemon/src/client.rs new file mode 100644 index 00000000..a832f9a9 --- /dev/null +++ b/crates/atuin-daemon/src/client.rs @@ -0,0 +1,60 @@ +use eyre::{eyre, Result}; +use tokio::net::UnixStream; +use tonic::transport::{Channel, Endpoint, Uri}; +use tower::service_fn; + +use atuin_client::history::History; + +use crate::history::{ + history_client::HistoryClient as HistoryServiceClient, EndHistoryRequest, StartHistoryRequest, +}; + +pub struct HistoryClient { + client: HistoryServiceClient<Channel>, +} + +// Wrap the grpc client +impl HistoryClient { + pub async fn new(path: String) -> Result<Self> { + let channel = Endpoint::try_from("http://atuin_local_daemon:0")? + .connect_with_connector(service_fn(move |_: Uri| { + let path = path.to_string(); + + UnixStream::connect(path) + })) + .await + .map_err(|_| eyre!("failed to connect to local atuin daemon. Is it running?"))?; + + let client = HistoryServiceClient::new(channel); + + Ok(HistoryClient { client }) + } + + pub async fn start_history(&mut self, h: History) -> Result<String> { + let req = StartHistoryRequest { + command: h.command, + cwd: h.cwd, + hostname: h.hostname, + session: h.session, + timestamp: h.timestamp.unix_timestamp_nanos() as u64, + }; + + let resp = self.client.start_history(req).await?; + + Ok(resp.into_inner().id) + } + + pub async fn end_history( + &mut self, + id: String, + duration: u64, + exit: i64, + ) -> Result<(String, u64)> { + let req = EndHistoryRequest { id, duration, exit }; + + let resp = self.client.end_history(req).await?; + let resp = resp.into_inner(); + + Ok((resp.id, resp.idx)) + } +} |
