Add a "check-config" command to validate config files & tests for it

This commit is contained in:
Andreas Fuchs 2024-10-04 10:49:08 -04:00
parent 70ee206031
commit 26ba489aa3
24 changed files with 492 additions and 0 deletions

View file

@ -12,6 +12,7 @@ use crate::{
error, observability,
};
mod check_config;
mod serve;
/// Command line arguments
@ -29,6 +30,18 @@ pub(crate) struct Args {
pub(crate) enum Command {
/// Run the server.
Serve(ServeArgs),
/// Check the configuration file for syntax and semantic errors.
CheckConfig(CheckConfigArgs),
}
#[derive(clap::Args)]
pub(crate) struct CheckConfigArgs {
#[clap(flatten)]
config: ConfigArg,
#[clap(flatten)]
observability: ObservabilityArgs,
}
/// Wrapper for the `--config` arg.
@ -83,6 +96,9 @@ impl Args {
match self.command {
Command::Serve(args) => serve::run(args).await?,
Command::CheckConfig(args) => {
check_config::run(args.config).await?;
}
}
Ok(())
}
@ -93,6 +109,10 @@ impl Command {
// All subcommands other than `serve` should return `Some`. Keep these
// match arms sorted by the enum variant name.
match self {
Command::CheckConfig(args) => Some((
args.observability.log_format,
args.observability.log_filter.clone(),
)),
Command::Serve(_) => None,
}
}

11
src/cli/check_config.rs Normal file
View file

@ -0,0 +1,11 @@
use tracing::info;
use crate::{cli::ConfigArg, config, error};
pub(crate) async fn run(
args: ConfigArg,
) -> Result<(), error::CheckConfigCommand> {
let _config = config::load(args.config.as_ref()).await?;
info!("Configuration looks good");
Ok(())
}

View file

@ -45,6 +45,9 @@ pub(crate) enum Main {
#[error("failed to install global default tracing subscriber")]
SetSubscriber(#[from] tracing::subscriber::SetGlobalDefaultError),
#[error(transparent)]
CheckConfigCommand(#[from] CheckConfigCommand),
}
/// Errors returned from the `serve` CLI subcommand.
@ -72,6 +75,16 @@ pub(crate) enum ServeCommand {
ServerNameChanged(#[from] ServerNameChanged),
}
/// Errors returned from the `check-config` CLI subcommand.
// Missing docs are allowed here since that kind of information should be
// encoded in the error messages themselves anyway.
#[allow(missing_docs)]
#[derive(Error, Debug)]
pub(crate) enum CheckConfigCommand {
#[error("failed to validate configuration")]
Config(#[from] Config),
}
/// Error generated if `server_name` has changed or if checking this failed
// Missing docs are allowed here since that kind of information should be
// encoded in the error messages themselves anyway.