add cli override for any configuration item

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-25 02:59:54 +00:00
parent 4e975887cf
commit c423a83656
8 changed files with 68 additions and 14 deletions

1
Cargo.lock generated
View file

@ -708,6 +708,7 @@ dependencies = [
"tikv-jemallocator",
"tokio",
"tokio-metrics",
"toml",
"tracing",
"tracing-core",
"tracing-subscriber",

View file

@ -35,6 +35,11 @@ version = "0.2.8"
version = "0.20"
features = ["features"]
[workspace.dependencies.toml]
version = "0.8.14"
default-features = false
features = ["parse"]
[workspace.dependencies.sanitize-filename]
version = "0.5.0"

View file

@ -86,6 +86,7 @@ tikv-jemalloc-sys.optional = true
tikv-jemalloc-sys.workspace = true
tokio.workspace = true
tokio-metrics.workspace = true
toml.workspace = true
tracing-core.workspace = true
tracing-subscriber.workspace = true
tracing.workspace = true

View file

@ -9,10 +9,8 @@ use either::{
Either,
Either::{Left, Right},
};
use figment::{
providers::{Env, Format, Toml},
Figment,
};
use figment::providers::{Env, Format, Toml};
pub use figment::{value::Value as FigmentValue, Figment};
use itertools::Itertools;
use regex::RegexSet;
use ruma::{
@ -408,8 +406,8 @@ const DEPRECATED_KEYS: &[&str; 9] = &[
];
impl Config {
/// Initialize config
pub fn new(path: &Option<PathBuf>) -> Result<Self> {
/// Pre-initialize config
pub fn load(path: &Option<PathBuf>) -> Result<Figment> {
let raw_config = if let Some(config_file_env) = Env::var("CONDUIT_CONFIG") {
Figment::new()
.merge(Toml::file(config_file_env).nested())
@ -431,13 +429,18 @@ impl Config {
.merge(Env::prefixed("CONDUWUIT_").global().split("__"))
};
Ok(raw_config)
}
/// Finalize config
pub fn new(raw_config: &Figment) -> Result<Self> {
let config = match raw_config.extract::<Self>() {
Err(e) => return Err!("There was a problem with your configuration file: {e}"),
Ok(config) => config,
};
// don't start if we're listening on both UNIX sockets and TCP at same time
check::is_dual_listening(&raw_config)?;
check::is_dual_listening(raw_config)?;
Ok(config)
}

View file

@ -57,6 +57,12 @@ pub enum Error {
HttpHeader(#[from] http::header::InvalidHeaderValue),
#[error("{0}")]
CargoToml(#[from] cargo_toml::Error),
#[error("{0}")]
FigmentError(#[from] figment::error::Error),
#[error("{0}")]
TomlSerError(#[from] toml::ser::Error),
#[error("{0}")]
TomlDeError(#[from] toml::de::Error),
// ruma
#[error("{0}")]

View file

@ -10,6 +10,7 @@ pub mod pdu;
pub mod server;
pub mod utils;
pub use ::toml;
pub use config::Config;
pub use error::Error;
pub use info::{rustc_flags_capture, version, version::version};

View file

@ -3,16 +3,24 @@
use std::path::PathBuf;
use clap::Parser;
use conduit::{Config, Result};
use conduit::{
config::{Figment, FigmentValue},
err, toml, Err, Result,
};
/// Commandline arguments
#[derive(Parser, Debug)]
#[clap(version = conduit::version(), about, long_about = None)]
pub(crate) struct Args {
#[arg(short, long)]
/// Optional argument to the path of a conduwuit config TOML file
/// Path to the config TOML file (optional)
pub(crate) config: Option<PathBuf>,
/// Override a configuration variable using TOML 'key=value' syntax
#[arg(long, short('O'))]
pub(crate) option: Vec<String>,
#[cfg(feature = "console")]
/// Activate admin command console automatically after startup.
#[arg(long, num_args(0))]
pub(crate) console: bool,
@ -23,10 +31,38 @@ pub(crate) struct Args {
pub(super) fn parse() -> Args { Args::parse() }
/// Synthesize any command line options with configuration file options.
pub(crate) fn update(config: &mut Config, args: &Args) -> Result<()> {
pub(crate) fn update(mut config: Figment, args: &Args) -> Result<Figment> {
#[cfg(feature = "console")]
// Indicate the admin console should be spawned automatically if the
// configuration file hasn't already.
config.admin_console_automatic |= args.console;
Ok(())
if args.console {
config = config.join(("admin_console_automatic", true));
}
// All other individual overrides can go last in case we have options which
// set multiple conf items at once and the user still needs granular overrides.
for option in &args.option {
let (key, val) = option
.split_once('=')
.ok_or_else(|| err!("Missing '=' in -O/--option: {option:?}"))?;
if key.is_empty() {
return Err!("Missing key= in -O/--option: {option:?}");
}
if val.is_empty() {
return Err!("Missing =val in -O/--option: {option:?}");
}
// The value has to pass for what would appear as a line in the TOML file.
let val = toml::from_str::<FigmentValue>(option)?;
let FigmentValue::Dict(_, val) = val else {
panic!("Unexpected Figment Value: {val:#?}");
};
// Figment::merge() overrides existing
config = config.merge((key, val[key].clone()));
}
Ok(config)
}

View file

@ -22,8 +22,9 @@ pub(crate) struct Server {
impl Server {
pub(crate) fn build(args: &Args, runtime: Option<&runtime::Handle>) -> Result<Arc<Self>, Error> {
let mut config = Config::new(&args.config)?;
crate::clap::update(&mut config, args)?;
let raw_config = Config::load(&args.config)?;
let raw_config = crate::clap::update(raw_config, args)?;
let config = Config::new(&raw_config)?;
#[cfg(feature = "sentry_telemetry")]
let sentry_guard = crate::sentry::init(&config);