//! `memoryd serve --config `. use std::ffi::OsString; use std::path::PathBuf; use std::process::ExitCode; use std::sync::Arc; use memoryd::config::Config; use memoryd::serve; const USAGE: &str = "usage: memoryd serve --config "; const START_FAILED: &str = "see docs/runbook.md#memoryd-start-failed"; fn main() -> ExitCode { let args: Vec = std::env::args_os().skip(1).collect(); let Some(path) = parse(&args) else { eprintln!("{USAGE}"); return ExitCode::from(2); }; let config = match Config::load(&path) { Ok(config) => config, Err(e) => { eprintln!("memoryd: {e}\n{START_FAILED}"); return ExitCode::from(1); } }; let log: serve::Log = Arc::new(|line: &str| eprintln!("{line}")); let running = match serve::start(config, log) { Ok(running) => running, Err(e) => { eprintln!("memoryd: {e}\n{START_FAILED}"); return ExitCode::from(1); } }; eprintln!("memoryd: serving"); match running.run() { Ok(()) => ExitCode::SUCCESS, Err(e) => { eprintln!("memoryd: stopped serving: {e}\n{START_FAILED}"); ExitCode::from(1) } } } /// The config path of `serve --config `, or `None` for any other list. fn parse(args: &[OsString]) -> Option { match args { [serve, flag, path] if serve == "serve" && flag == "--config" => Some(PathBuf::from(path)), _ => None, } }