|
1 | 1 | //! TODO: docs. |
2 | 2 |
|
3 | | -use std::os::unix::net::UnixListener; |
| 3 | +use core::fmt; |
4 | 4 | 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; |
5 | 14 |
|
6 | 15 | #[derive(Debug, clap::Args)] |
7 | 16 | pub struct StartArgs { |
8 | 17 | #[arg(long)] |
9 | 18 | socket: PathBuf, |
10 | 19 | #[arg(long)] |
11 | | - auth_token: String, |
| 20 | + user: UserName, |
12 | 21 | #[arg(long)] |
13 | | - user: String, |
| 22 | + cache: CacheName, |
14 | 23 | #[arg(long)] |
15 | | - cache: String, |
| 24 | + auth_token: AuthToken, |
16 | 25 | } |
17 | 26 |
|
18 | 27 | 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 | + } |
28 | 72 | } |
0 commit comments