1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-07-29 22:01:14 +03:00

Allow loading multiple configuration files

This commit is contained in:
Quentin Gliech
2021-09-17 12:22:03 +02:00
parent bd441ceef7
commit 3159a9972f
2 changed files with 25 additions and 7 deletions

View File

@ -23,6 +23,7 @@ use std::path::PathBuf;
use anyhow::Context; use anyhow::Context;
use clap::Clap; use clap::Clap;
use mas_config::ConfigurationSection; use mas_config::ConfigurationSection;
use tracing::trace;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry};
use self::{ use self::{
@ -57,8 +58,14 @@ enum Subcommand {
#[derive(Clap, Debug)] #[derive(Clap, Debug)]
struct RootCommand { struct RootCommand {
/// Path to the configuration file /// Path to the configuration file
#[clap(short, long, global = true, default_value = "config.yaml")] #[clap(
config: PathBuf, short,
long,
global = true,
default_value = "config.yaml",
multiple_occurrences(true)
)]
config: Vec<PathBuf>,
#[clap(subcommand)] #[clap(subcommand)]
subcommand: Option<Subcommand>, subcommand: Option<Subcommand>,
@ -78,7 +85,7 @@ impl RootCommand {
} }
fn load_config<'de, T: ConfigurationSection<'de>>(&self) -> anyhow::Result<T> { fn load_config<'de, T: ConfigurationSection<'de>>(&self) -> anyhow::Result<T> {
T::load_from_file(&self.config).context("could not load configuration") T::load_from_files(&self.config).context("could not load configuration")
} }
} }
@ -105,5 +112,6 @@ async fn main() -> anyhow::Result<()> {
let opts = RootCommand::parse(); let opts = RootCommand::parse();
// And run the command // And run the command
trace!(?opts, "Running command");
opts.run().await opts.run().await
} }

View File

@ -56,15 +56,25 @@ pub trait ConfigurationSection<'a>: Sized + Deserialize<'a> + Serialize {
.context("could not load configuration") .context("could not load configuration")
} }
/// Load configuration from a list of files and environment variables.
fn load_from_files<P>(paths: &[P]) -> Result<Self, FigmentError>
where
P: AsRef<Path>,
{
let base = Figment::new().merge(Env::prefixed("MAS_").split("_"));
paths
.iter()
.fold(base, |f, path| f.merge(Yaml::file(path)))
.extract_inner(Self::path())
}
/// Load configuration from a file and environment variables. /// Load configuration from a file and environment variables.
fn load_from_file<P>(path: P) -> Result<Self, FigmentError> fn load_from_file<P>(path: P) -> Result<Self, FigmentError>
where where
P: AsRef<Path>, P: AsRef<Path>,
{ {
Figment::new() Self::load_from_files(&[path])
.merge(Env::prefixed("MAS_").split("_"))
.merge(Yaml::file(path))
.extract_inner(Self::path())
} }
/// Generate config used in unit tests /// Generate config used in unit tests