mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-16 23:31:24 +01:00
set up structure for multiple cli commands
The previous cli is now behind the 'serve' subcommand.
This commit is contained in:
parent
1ee3bbb316
commit
be87774a3b
4 changed files with 60 additions and 39 deletions
|
|
@ -79,7 +79,7 @@ in
|
||||||
# Keep sorted
|
# Keep sorted
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
DynamicUser = true;
|
DynamicUser = true;
|
||||||
ExecStart = "${lib.getExe cfg.package} --config ${configFile}";
|
ExecStart = "${lib.getExe cfg.package} serve --config ${configFile}";
|
||||||
LockPersonality = true;
|
LockPersonality = true;
|
||||||
MemoryDenyWriteExecute = true;
|
MemoryDenyWriteExecute = true;
|
||||||
PrivateDevices = true;
|
PrivateDevices = true;
|
||||||
|
|
|
||||||
35
src/args.rs
35
src/args.rs
|
|
@ -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
51
src/cli.rs
Normal 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,
|
||||||
|
}
|
||||||
11
src/main.rs
11
src/main.rs
|
|
@ -19,6 +19,7 @@ use axum::{
|
||||||
use axum_server::{
|
use axum_server::{
|
||||||
bind, bind_rustls, tls_rustls::RustlsConfig, Handle as ServerHandle,
|
bind, bind_rustls, tls_rustls::RustlsConfig, Handle as ServerHandle,
|
||||||
};
|
};
|
||||||
|
use clap::Parser;
|
||||||
use futures_util::FutureExt;
|
use futures_util::FutureExt;
|
||||||
use http::{
|
use http::{
|
||||||
header::{self, HeaderName},
|
header::{self, HeaderName},
|
||||||
|
|
@ -41,7 +42,7 @@ use tower_http::{
|
||||||
use tracing::{debug, error, info, info_span, warn, Instrument};
|
use tracing::{debug, error, info, info_span, warn, Instrument};
|
||||||
|
|
||||||
mod api;
|
mod api;
|
||||||
mod args;
|
mod cli;
|
||||||
mod config;
|
mod config;
|
||||||
mod database;
|
mod database;
|
||||||
mod error;
|
mod error;
|
||||||
|
|
@ -51,6 +52,7 @@ mod utils;
|
||||||
|
|
||||||
pub(crate) use api::ruma_wrapper::{Ar, Ra};
|
pub(crate) use api::ruma_wrapper::{Ar, Ra};
|
||||||
use api::{client_server, server_server, well_known};
|
use api::{client_server, server_server, well_known};
|
||||||
|
use cli::{Args, Command};
|
||||||
pub(crate) use config::{Config, ListenConfig};
|
pub(crate) use config::{Config, ListenConfig};
|
||||||
pub(crate) use database::KeyValueDatabase;
|
pub(crate) use database::KeyValueDatabase;
|
||||||
pub(crate) use service::{pdu::PduEvent, Services};
|
pub(crate) use service::{pdu::PduEvent, Services};
|
||||||
|
|
@ -108,9 +110,12 @@ async fn main() -> ExitCode {
|
||||||
async fn try_main() -> Result<(), error::Main> {
|
async fn try_main() -> Result<(), error::Main> {
|
||||||
use error::Main as Error;
|
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)?;
|
let (_guard, reload_handles) = observability::init(&config)?;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue