add default builder method

This commit is contained in:
OMGeeky
2024-04-21 22:33:08 +02:00
parent 8864594583
commit 87a67bd757
2 changed files with 33 additions and 2 deletions

View File

@@ -1,10 +1,18 @@
[package]
name = "twba-backup-config"
version = "0.1.3"
version = "0.1.4"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1.0", features = ["derive"] }
confique = { version = "0.2" , features= ["json5","toml"]}
confique = { version = "0.2", features = ["json5", "toml"] }
tracing = { version = "0.1", optional = true }
shellexpand = { version = "3.1", optional = true }
[features]
default = ["tracing", "env", "home"]
env = []
tracing = ["dep:tracing"]
home = ["dep:shellexpand"]

View File

@@ -127,3 +127,26 @@ pub struct Twitch {
#[config(default = 50)]
pub downloader_thread_count: u64,
}
pub fn get_default_builder() -> confique::Builder<Conf> {
#[cfg(feature = "tracing")]
tracing::info!("building default config");
let conf = Conf::builder();
#[cfg(feature = "env")]
let conf = conf.env().file(
std::env::var("TWBA_CONFIG")
.map(|v| {
#[cfg(feature = "tracing")]
tracing::info!("using '{}' as primary config source after env", v);
v
})
.unwrap_or_else(|_| {
#[cfg(feature = "tracing")]
tracing::warn!("could not get config location from env");
"./settings.toml".to_string()
}),
);
#[cfg(feature = "home")]
let conf = conf.file(shellexpand::tilde("~/twba/config.toml").into_owned());
conf
}