use std::io::{self, Read, Write}; use std::os::unix::net::{UnixListener, UnixStream}; use std::thread; mod cli; fn main() { // Initialize the existing synchronized directories. // TODO // TODO: Register signal handler for graceful shutdown. // Listen for CLI commands. // TODO: Graceful shutdown on errors. let listener = UnixListener::bind(cli::socket_path()).unwrap(); for stream in listener.incoming() { match stream { Ok(stream) => { thread::spawn(move || { let mut stream = stream; match handle_cli_client(&mut stream) { Ok(()) => {} Err(e) => { // Log error and try to send it to the stream. // TODO write!(stream, "Error: {:?}", e).ok(); } }; }); } Err(err) => { eprintln!("Error while listening on the local unix socket: {:?}", err); break; } } } } fn handle_cli_client(stream: &mut UnixStream) -> Result<(), Error> { let mut request = String::new(); stream.read_to_string(&mut request)?; let options: cli::Options = serde_json::from_str(&request)?; let _verbose = options.verbose; match options.command { _cmd @ cli::Command::ListDirectories { .. } => { // TODO } _cmd @ cli::Command::AddDirectory { .. } => { // TODO } _cmd @ cli::Command::RemoveDirectory { .. } => { // TODO } } // TODO Ok(()) } #[derive(Debug)] enum Error { Json(serde_json::error::Error), Io(std::io::Error), } impl From for Error { fn from(e: serde_json::error::Error) -> Error { Error::Json(e) } } impl From for Error { fn from(e: io::Error) -> Error { Error::Io(e) } }