Skip to content

Commit 763fcc2

Browse files
committed
Sketch start::start
1 parent ff5f30b commit 763fcc2

3 files changed

Lines changed: 60 additions & 18 deletions

File tree

‎src/protocol.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub(crate) enum Message {
2424
}
2525

2626
#[derive(Clone)]
27-
pub(crate) enum StoreDir {
27+
pub enum StoreDir {
2828
NixStore,
2929
Other(SmolStr),
3030
}

‎src/push.rs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
//! TODO: docs.
22
33
use core::fmt;
4-
use std::io::{self, Write};
54
use std::path::PathBuf;
6-
use std::process;
5+
use std::{io, process};
76

87
use futures::SinkExt;
98
use nix_types::NixStorePath;
109
use tokio::net::UnixStream;
1110

12-
use crate::context::Runtime;
1311
use crate::protocol::{self, StoreDir};
1412
use crate::tokio::Tokio;
1513

@@ -42,7 +40,7 @@ pub(crate) fn push(args: PushArgs) {
4240
});
4341

4442
if let Err(push_error) = push_result {
45-
let _ = writeln!(io::stderr(), "{}", push_error);
43+
eprintln!("{push_error}");
4644
process::exit(1);
4745
}
4846
}

‎src/start.rs‎

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,72 @@
11
//! TODO: docs.
22
3-
use std::os::unix::net::UnixListener;
3+
use core::fmt;
44
use std::path::PathBuf;
5+
use std::{io, process};
6+
7+
use nix_types::{CacheName, UserName};
8+
use tokio::net::UnixListener;
9+
10+
use crate::AuthToken;
11+
use crate::context::Context;
12+
use crate::real_context::{self, RealContext};
13+
use crate::tokio::Tokio;
514

615
#[derive(Debug, clap::Args)]
716
pub struct StartArgs {
817
#[arg(long)]
918
socket: PathBuf,
1019
#[arg(long)]
11-
auth_token: String,
20+
user: UserName,
1221
#[arg(long)]
13-
user: String,
22+
cache: CacheName,
1423
#[arg(long)]
15-
cache: String,
24+
auth_token: AuthToken,
1625
}
1726

1827
pub(crate) fn start(args: StartArgs) {
19-
let _listener = match UnixListener::bind(&args.socket) {
20-
Ok(socket) => socket,
21-
Err(err) => panic!(
22-
"Failed to create Unix domain socket at {:?}: {err}",
23-
args.socket
24-
),
25-
};
26-
27-
println!("Started {:?}", args.socket);
28+
let tokio = Tokio::new();
29+
30+
let mut ctx = RealContext::new(tokio.spawner());
31+
32+
let start_result: Result<i64, StartError> = tokio.block_on(async {
33+
let _cache = ctx
34+
.cache(args.user, args.cache, args.auth_token)
35+
.await
36+
.map_err(StartError::GetCache)?;
37+
38+
let _listener =
39+
UnixListener::bind(&args.socket).map_err(StartError::BindSocket)?;
40+
41+
todo!("fork process, pass cache and listener to fork");
42+
});
43+
44+
match start_result {
45+
Ok(daemon_pid) => {
46+
println!(
47+
"Started daemon with process ID {daemon_pid}, listening on {}",
48+
args.socket.display()
49+
);
50+
},
51+
Err(start_error) => {
52+
eprintln!("{start_error}");
53+
process::exit(1);
54+
},
55+
}
56+
}
57+
58+
enum StartError {
59+
BindSocket(io::Error),
60+
GetCache(real_context::CacheError),
61+
}
62+
63+
impl fmt::Display for StartError {
64+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65+
match self {
66+
Self::BindSocket(io_error) => {
67+
write!(f, "Couldn't bind to Unix domain socket: {io_error}")
68+
},
69+
Self::GetCache(error) => error.fmt(f),
70+
}
71+
}
2872
}

0 commit comments

Comments
 (0)