Skip to content

Commit 4ac3082

Browse files
committed
Implement drain::Drain
1 parent 38bf93e commit 4ac3082

4 files changed

Lines changed: 69 additions & 4 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ nix-types = { git = "ssh://git@github.com/sub60/cache.git", package = "types" }
1313
pin-project-lite = "0.2"
1414
smol_str = "0.3.6"
1515
tokio = { version = "1", default-features = false, features = [
16+
"io-std",
17+
"io-util",
1618
"net",
1719
"rt-multi-thread",
1820
] }

‎src/drain.rs‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
//! TODO: docs.
22
3+
use core::fmt;
4+
use std::io::{self, Write};
35
use std::path::PathBuf;
6+
use std::process;
7+
8+
use futures::SinkExt;
9+
use tokio::net::UnixStream;
10+
11+
use crate::context::Runtime;
12+
use crate::protocol;
13+
use crate::tokio::Tokio;
414

515
#[derive(Debug, clap::Args)]
616
pub struct DrainArgs {
@@ -9,5 +19,56 @@ pub struct DrainArgs {
919
}
1020

1121
pub(crate) fn drain(args: DrainArgs) {
12-
println!("Draining {:?}", args.socket);
22+
let drain_result: Result<(), DrainError> =
23+
Tokio::new().block_on(async move {
24+
let socket = UnixStream::connect(&args.socket)
25+
.await
26+
.map_err(DrainError::ConnectToSocket)?;
27+
28+
let (mut read_half, write_half) = socket.into_split();
29+
30+
protocol::Sender::new(async_compat::Compat::new(write_half))
31+
.send(protocol::Message::DrainDaemon)
32+
.await
33+
.map_err(DrainError::WriteMessage)?;
34+
35+
let mut stdout = tokio::io::stdout();
36+
37+
if let Err(io_err) =
38+
tokio::io::copy(&mut read_half, &mut stdout).await
39+
{
40+
let _ = writeln!(
41+
io::stderr(),
42+
"Couldn't copy bytes to stdout: {io_err}"
43+
);
44+
}
45+
46+
Ok(())
47+
});
48+
49+
if let Err(drain_error) = drain_result {
50+
let _ = writeln!(io::stderr(), "{}", drain_error);
51+
process::exit(1);
52+
}
53+
}
54+
55+
enum DrainError {
56+
ConnectToSocket(io::Error),
57+
WriteMessage(io::Error),
58+
}
59+
60+
impl fmt::Display for DrainError {
61+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62+
match self {
63+
Self::ConnectToSocket(error) => {
64+
write!(f, "Couldn't connect to Unix domain socket: {error}")
65+
},
66+
Self::WriteMessage(error) => {
67+
write!(
68+
f,
69+
"Couldn't write message to Unix domain socket: {error}"
70+
)
71+
},
72+
}
73+
}
1374
}

‎src/push.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ pub struct PushArgs {
2424
pub(crate) fn push(args: PushArgs) {
2525
let push_result: Result<(), PushError> =
2626
Tokio::new().block_on(async move {
27-
let mut message_tx = UnixStream::connect(&args.socket)
27+
let socket = UnixStream::connect(&args.socket)
2828
.await
29-
.map(async_compat::Compat::new)
30-
.map(protocol::Sender::new)
3129
.map_err(PushError::ConnectToSocket)?;
3230

31+
let mut message_tx =
32+
protocol::Sender::new(async_compat::Compat::new(socket));
33+
3334
for store_path in args.store_paths {
3435
message_tx
3536
.send(protocol::Message::PushStorePath(store_path))

0 commit comments

Comments
 (0)