set up structure for multiple cli commands

The previous cli is now behind the 'serve' subcommand.
This commit is contained in:
Benjamin Lee 2024-08-31 20:51:26 -07:00
parent 1ee3bbb316
commit be87774a3b
No known key found for this signature in database
GPG key ID: FB9624E2885D55A4
4 changed files with 60 additions and 39 deletions

View file

@ -79,7 +79,7 @@ in
# Keep sorted
serviceConfig = {
DynamicUser = true;
ExecStart = "${lib.getExe cfg.package} --config ${configFile}";
ExecStart = "${lib.getExe cfg.package} serve --config ${configFile}";
LockPersonality = true;
MemoryDenyWriteExecute = true;
PrivateDevices = true;

View file

@ -1,35 +0,0 @@
//! Integration with `clap`
use std::path::PathBuf;
use clap::{CommandFactory as _, FromArgMatches as _, Parser};
/// Command line arguments
#[derive(Parser)]
#[clap(about, version = crate::version())]
pub(crate) struct Args {
/// Path to the configuration file
#[clap(long, short)]
pub(crate) config: Option<PathBuf>,
}
/// Parse command line arguments into structured data
pub(crate) fn parse() -> Args {
let mut command = Args::command().mut_arg("config", |x| {
let help = "Set the path to the configuration file";
x.help(help).long_help(format!(
"{}\n\nIf this option is specified, the provided value is used \
as-is.\n\nIf this option is not specified, then the XDG Base \
Directory Specification is followed, searching for the path `{}` \
in the configuration directories.
",
help,
crate::config::DEFAULT_PATH.display(),
))
});
match Args::from_arg_matches(&command.get_matches_mut()) {
Ok(x) => x,
Err(e) => e.format(&mut command).exit(),
}
}

51
src/cli.rs Normal file
View file

@ -0,0 +1,51 @@
//! Integration with `clap`
use std::path::PathBuf;
use clap::{Parser, Subcommand};
/// Command line arguments
#[derive(Parser)]
#[clap(
about,
version = crate::version(),
)]
pub(crate) struct Args {
#[clap(subcommand)]
pub(crate) command: Command,
}
#[derive(Subcommand)]
pub(crate) enum Command {
/// Run the server.
Serve(ServeArgs),
}
/// Wrapper for the `--config` arg.
///
/// This exists to centralize the `mut_arg` code that sets the help value based
/// on runtime information.
#[derive(clap::Args)]
#[clap(mut_arg("config", |x| {
let help = "Set the path to the configuration file";
x.help(help).long_help(format!(
"{}\n\nIf this option is specified, the provided value is used \
as-is.\n\nIf this option is not specified, then the XDG Base \
Directory Specification is followed, searching for the path `{}` \
in the configuration directories.
",
help,
crate::config::DEFAULT_PATH.display(),
))
}))]
pub(crate) struct ConfigArg {
/// Path to the configuration file
#[clap(long, short)]
pub(crate) config: Option<PathBuf>,
}
#[derive(clap::Args)]
pub(crate) struct ServeArgs {
#[clap(flatten)]
pub(crate) config: ConfigArg,
}

View file

@ -19,6 +19,7 @@ use axum::{
use axum_server::{
bind, bind_rustls, tls_rustls::RustlsConfig, Handle as ServerHandle,
};
use clap::Parser;
use futures_util::FutureExt;
use http::{
header::{self, HeaderName},
@ -41,7 +42,7 @@ use tower_http::{
use tracing::{debug, error, info, info_span, warn, Instrument};
mod api;
mod args;
mod cli;
mod config;
mod database;
mod error;
@ -51,6 +52,7 @@ mod utils;
pub(crate) use api::ruma_wrapper::{Ar, Ra};
use api::{client_server, server_server, well_known};
use cli::{Args, Command};
pub(crate) use config::{Config, ListenConfig};
pub(crate) use database::KeyValueDatabase;
pub(crate) use service::{pdu::PduEvent, Services};
@ -108,9 +110,12 @@ async fn main() -> ExitCode {
async fn try_main() -> Result<(), error::Main> {
use error::Main as Error;
let args = args::parse();
let args = Args::parse();
// This is a placeholder, the logic specific to the 'serve' command will be
// moved to another file in a later commit
let Command::Serve(args) = args.command;
let config = config::load(args.config.as_ref()).await?;
let config = config::load(args.config.config.as_ref()).await?;
let (_guard, reload_handles) = observability::init(&config)?;