|
1 | 1 | //! TODO: docs. |
2 | 2 |
|
| 3 | +use core::fmt; |
| 4 | +use std::io::{self, Write}; |
3 | 5 | 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; |
4 | 15 |
|
5 | 16 | #[derive(Debug, clap::Args)] |
6 | 17 | pub struct PushArgs { |
7 | 18 | #[arg(long)] |
8 | 19 | socket: PathBuf, |
9 | 20 | #[arg(required = true)] |
10 | | - store_paths: Vec<String>, |
| 21 | + store_paths: Vec<NixStorePath<StoreDir>>, |
11 | 22 | } |
12 | 23 |
|
13 | 24 | 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 | + } |
15 | 68 | } |
0 commit comments