Skip to content

Commit 38bf93e

Browse files
committed
Implement push::push
1 parent 935306b commit 38bf93e

2 files changed

Lines changed: 66 additions & 3 deletions

File tree

‎src/protocol.rs‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use core::convert::Infallible;
44
use core::pin::Pin;
5-
use core::str;
65
use core::task::{Context, Poll};
6+
use core::{fmt, str};
77
use std::io;
88

99
use futures::{AsyncRead, AsyncWrite};
@@ -23,6 +23,7 @@ pub(crate) enum Message {
2323
DrainDaemon,
2424
}
2525

26+
#[derive(Clone)]
2627
pub(crate) enum StoreDir {
2728
NixStore,
2829
Other(SmolStr),
@@ -89,6 +90,15 @@ impl<Reader: AsyncRead> futures::Stream for Receiver<Reader> {
8990
}
9091
}
9192

93+
impl fmt::Display for StoreDir {
94+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95+
match self {
96+
StoreDir::NixStore => NixStoreLiteral.fmt(f),
97+
StoreDir::Other(smol_str) => smol_str.fmt(f),
98+
}
99+
}
100+
}
101+
92102
impl str::FromStr for StoreDir {
93103
type Err = Infallible;
94104

‎src/push.rs‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,68 @@
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 nix_types::NixStorePath;
10+
use tokio::net::UnixStream;
11+
12+
use crate::context::Runtime;
13+
use crate::protocol::{self, StoreDir};
14+
use crate::tokio::Tokio;
415

516
#[derive(Debug, clap::Args)]
617
pub struct PushArgs {
718
#[arg(long)]
819
socket: PathBuf,
920
#[arg(required = true)]
10-
store_paths: Vec<String>,
21+
store_paths: Vec<NixStorePath<StoreDir>>,
1122
}
1223

1324
pub(crate) fn push(args: PushArgs) {
14-
println!("Pushing {:?} to {:?}", args.store_paths, args.socket);
25+
let push_result: Result<(), PushError> =
26+
Tokio::new().block_on(async move {
27+
let mut message_tx = UnixStream::connect(&args.socket)
28+
.await
29+
.map(async_compat::Compat::new)
30+
.map(protocol::Sender::new)
31+
.map_err(PushError::ConnectToSocket)?;
32+
33+
for store_path in args.store_paths {
34+
message_tx
35+
.send(protocol::Message::PushStorePath(store_path))
36+
.await
37+
.map_err(PushError::WriteMessage)?;
38+
}
39+
40+
Ok(())
41+
});
42+
43+
if let Err(push_error) = push_result {
44+
let _ = writeln!(io::stderr(), "{}", push_error);
45+
process::exit(1);
46+
}
47+
}
48+
49+
enum PushError {
50+
ConnectToSocket(io::Error),
51+
WriteMessage(io::Error),
52+
}
53+
54+
impl fmt::Display for PushError {
55+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56+
match self {
57+
Self::ConnectToSocket(error) => {
58+
write!(f, "Couldn't connect to Unix domain socket: {error}")
59+
},
60+
Self::WriteMessage(error) => {
61+
write!(
62+
f,
63+
"Couldn't write message to Unix domain socket: {error}"
64+
)
65+
},
66+
}
67+
}
1568
}

0 commit comments

Comments
 (0)