//! The command line parser: picks the command and hands the rest to it. use super::{ Command, Environment, PathBuf, ServiceAction, logs_command, number, parse_flags, reconfigure_command, report_command, text, up_options, }; pub fn parse(args: &[String], env: &Environment) -> Result { let Some((command, rest)) = args.split_first() else { return Ok(Command::Help); }; match command.as_str() { "-h" | "--help" | "help" => Ok(Command::Help), "version" | "--version" | "-V" => Ok(Command::Version), "up" | "preflight" | "status" | "down" | "verify" | "reconfigure" | "logs" | "report" | "service" => parse_installation_command(command, rest, env), other => parse_small_command(other, rest), } } /// The commands that work on an installation: they locate it with the options of `up`. fn parse_installation_command( command: &str, rest: &[String], env: &Environment, ) -> Result { match command { "reconfigure" => reconfigure_command(rest, env), "logs" => logs_command(rest, env), "report" => report_command(rest, env), "service" => parse_service(rest, env), _ => { let options = up_options(rest, env)?; Ok(match command { "up" => Command::Up(options), "preflight" => Command::Preflight(options), "status" => Command::Status(options), "down" => Command::Down(options), _ => Command::Verify(options), }) } } } /// The commands with a few arguments of their own. fn parse_small_command(command: &str, rest: &[String]) -> Result { match command { "profiles" => parse_profiles(rest), "run" => parse_run(rest), "exec-inherit" => Ok(Command::ExecInherit(rest.to_vec())), "seal-exec" => Ok(Command::SealExec(rest.to_vec())), "seal-probe" => match rest { [file] if !file.starts_with("--") => Ok(Command::SealProbe(PathBuf::from(file))), _ => Err("usage: leleka seal-probe FILE".into()), }, other => Err(format!("unknown command {other:?}; see `leleka --help`")), } } fn parse_profiles(rest: &[String]) -> Result { match rest { [] => Ok(Command::Profiles(None)), [show, name] if show == "show" => Ok(Command::Profiles(Some(name.clone()))), _ => Err("usage: leleka profiles [show NAME]".into()), } } fn parse_run(rest: &[String]) -> Result { let flags = parse_flags(rest, &[("--config", true), ("--kar-fd", true)])?; let config = text(&flags, "--config").ok_or("leleka run needs --config FILE")?; Ok(Command::Run { config: PathBuf::from(config), kar_fd: flags .get("--kar-fd") .map(|_| number(&flags, "--kar-fd", 0u32)) .transpose()?, }) } fn parse_service(rest: &[String], env: &Environment) -> Result { let Some((action, rest)) = rest.split_first() else { return Err("usage: leleka service install|uninstall|status|start|stop".into()); }; let action = match action.as_str() { "install" => ServiceAction::Install, "uninstall" => ServiceAction::Uninstall, "status" => ServiceAction::Status, "start" => ServiceAction::Start, "stop" => ServiceAction::Stop, "hold" => ServiceAction::Hold, other => { return Err(format!( "unknown service action {other:?}; use install, uninstall, status, start or stop" )); } }; up_options(rest, env).map(|options| Command::Service(action, options)) }