//! The `im` binary: the command line, logging and the process lifetime. //! //! `im [--config ] [serve]` runs the server; a bare `im` does the same. `im client ...` manages the //! OpenID Connect clients (`cli.rs`). `im admin ...` is the operator command line, which is not built yet. The configuration file is the `--config` path, //! then `IM_CONFIG`, then `config/im.toml`. Exit status 0 is success, 1 a failure, 2 a usage error. mod api; mod assets; mod audit; mod auth; mod backchannel; mod backups; mod boot; mod cli; mod client_ip; mod health; mod i18n; mod live; mod mail; mod oidc; mod pages; mod security; mod service_probe; mod state; mod sweeper; mod throttle; mod views; use std::path::{Path, PathBuf}; use std::process::ExitCode; use tracing_subscriber::EnvFilter; /// Printed with a usage error. const USAGE: &str = "usage: im [--config ] [serve | client ... | backup now | restore --from | fsck | keys rotate]"; /// The configuration file when neither `--config` nor `IM_CONFIG` names one. const DEFAULT_CONFIG: &str = "config/im.toml"; /// The environment variable that names the configuration file. const CONFIG_ENV: &str = "IM_CONFIG"; /// The parsed command line: the `--config` path and the positional words. struct Cli { config: Option, words: Vec, } /// Splits the arguments into the `--config ` pair and the positional words. fn parse_args(args: impl Iterator) -> Result { let mut config = None; let mut words = Vec::new(); let mut args = args; while let Some(arg) = args.next() { if arg == "--config" { let path = args.next().ok_or("--config needs a path")?; config = Some(PathBuf::from(path)); } else { words.push(arg); } } Ok(Cli { config, words }) } /// The configuration path: the flag, else `IM_CONFIG`, else the default. fn config_path(flag: Option) -> PathBuf { flag.or_else(|| std::env::var_os(CONFIG_ENV).map(PathBuf::from)) .unwrap_or_else(|| PathBuf::from(DEFAULT_CONFIG)) } /// Logs to stderr. The level comes from `RUST_LOG`, and defaults to `info`. fn init_logging() { let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")); tracing_subscriber::fmt() .with_env_filter(filter) .with_writer(std::io::stderr) .init(); } /// Runs the server and maps its outcome to an exit status. async fn serve(config: &Path) -> ExitCode { match boot::serve(config).await { Ok(()) => ExitCode::SUCCESS, Err(error) => { tracing::error!(error = %error, "im stopped with an error"); ExitCode::FAILURE } } } #[tokio::main] async fn main() -> ExitCode { init_logging(); let cli = match parse_args(std::env::args().skip(1)) { Ok(cli) => cli, Err(message) => { eprintln!("im: {message}\n{USAGE}"); return ExitCode::from(2); } }; let words: Vec<&str> = cli.words.iter().map(String::as_str).collect(); match words.as_slice() { [] | ["serve"] => serve(&config_path(cli.config)).await, ["client", ..] => cli::client(&config_path(cli.config), &cli.words[1..]).await, ["backup", ..] => cli::backup(&config_path(cli.config), &cli.words[1..]).await, ["restore", ..] => cli::restore(&config_path(cli.config), &cli.words[1..]).await, ["fsck"] => cli::fsck(&config_path(cli.config)).await, ["keys", ..] => cli::keys(&config_path(cli.config), &cli.words[1..]).await, _ => { eprintln!("im: unknown command\n{USAGE}"); ExitCode::from(2) } } }