2022-03-28 04:27:07 +00:00
|
|
|
use clap::Parser;
|
2022-12-23 18:56:15 +00:00
|
|
|
use std::path::Path;
|
2022-03-28 04:27:07 +00:00
|
|
|
|
2022-03-28 00:10:06 +00:00
|
|
|
mod commandline;
|
|
|
|
mod defaults;
|
|
|
|
mod file;
|
|
|
|
mod primitives;
|
2020-06-07 00:54:06 +00:00
|
|
|
|
2022-03-28 04:27:07 +00:00
|
|
|
use commandline::{Args, Output};
|
|
|
|
use config::Config;
|
|
|
|
use defaults::Defaults;
|
|
|
|
|
|
|
|
pub(crate) use commandline::Operation;
|
|
|
|
pub(crate) use file::{ConfigFile as Configuration, OpenTelemetry, Repo, Sled, Tracing};
|
2022-10-01 00:38:11 +00:00
|
|
|
pub(crate) use primitives::{
|
|
|
|
AudioCodec, Filesystem, ImageFormat, LogFormat, ObjectStorage, Store, VideoCodec,
|
|
|
|
};
|
2022-03-28 04:27:07 +00:00
|
|
|
|
2022-12-23 18:56:15 +00:00
|
|
|
pub(crate) fn configure_without_clap<P: AsRef<Path>, Q: AsRef<Path>>(
|
|
|
|
config_file: Option<P>,
|
|
|
|
save_to: Option<Q>,
|
|
|
|
) -> color_eyre::Result<(Configuration, Operation)> {
|
|
|
|
let config = Config::builder().add_source(config::Config::try_from(&Defaults::default())?);
|
|
|
|
|
|
|
|
let config = if let Some(config_file) = config_file {
|
|
|
|
config.add_source(config::File::from(config_file.as_ref()))
|
|
|
|
} else {
|
|
|
|
config
|
|
|
|
};
|
|
|
|
|
|
|
|
let built = config
|
|
|
|
.add_source(config::Environment::with_prefix("PICTRS").separator("__"))
|
|
|
|
.build()?;
|
|
|
|
|
|
|
|
let operation = Operation::Run;
|
|
|
|
|
|
|
|
let config: Configuration = built.try_deserialize()?;
|
|
|
|
|
|
|
|
if let Some(save_to) = save_to {
|
|
|
|
let output = toml::to_string_pretty(&config)?;
|
|
|
|
std::fs::write(save_to, output)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((config, operation))
|
|
|
|
}
|
|
|
|
|
2022-03-29 01:47:46 +00:00
|
|
|
pub(crate) fn configure() -> color_eyre::Result<(Configuration, Operation)> {
|
2022-03-28 04:27:07 +00:00
|
|
|
let Output {
|
|
|
|
config_format,
|
|
|
|
operation,
|
|
|
|
save_to,
|
|
|
|
config_file,
|
|
|
|
} = Args::parse().into_output();
|
|
|
|
|
|
|
|
let config = Config::builder().add_source(config::Config::try_from(&Defaults::default())?);
|
|
|
|
|
|
|
|
let config = if let Some(config_file) = config_file {
|
|
|
|
config.add_source(config::File::from(config_file))
|
|
|
|
} else {
|
|
|
|
config
|
|
|
|
};
|
|
|
|
|
|
|
|
let built = config
|
|
|
|
.add_source(config::Environment::with_prefix("PICTRS").separator("__"))
|
|
|
|
.add_source(config::Config::try_from(&config_format)?)
|
|
|
|
.build()?;
|
|
|
|
|
|
|
|
let config: Configuration = built.try_deserialize()?;
|
2021-10-23 04:48:56 +00:00
|
|
|
|
2022-03-28 04:27:07 +00:00
|
|
|
if let Some(save_to) = save_to {
|
|
|
|
let output = toml::to_string_pretty(&config)?;
|
|
|
|
std::fs::write(save_to, output)?;
|
|
|
|
}
|
2021-10-28 04:06:03 +00:00
|
|
|
|
2022-03-28 04:27:07 +00:00
|
|
|
Ok((config, operation))
|
2020-06-07 00:54:06 +00:00
|
|
|
}
|