aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2020-01-23 14:04:42 +0100
committerDaniel Mueller <deso@posteo.net>2021-01-10 10:15:44 -0800
commita7668265d507a7214b312c5dce874f0185b7b09a (patch)
treeea6252f123910a2f4bad5eac78f33f055af738da /src/config.rs
parent9d3c50ca3f9ff0bf12b1b25344959ed333ffbdee (diff)
downloadnitrocli-a7668265d507a7214b312c5dce874f0185b7b09a.tar.gz
nitrocli-a7668265d507a7214b312c5dce874f0185b7b09a.tar.bz2
Use envy to parse environment variables for Config
This patch uses the envy crate to parse the environment. A variable NITROCLI_KEY can be used to overwrite the configuration for *key*. This has the side effect that the NITROCLI_NO_CACHE variable is evaluated as a boolean variable (instead of only checking whether it is set). We also accept two new variables, NITROCLI_MODEL and NITROCLI_VERBOSITY.
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs20
1 files changed, 18 insertions, 2 deletions
diff --git a/src/config.rs b/src/config.rs
index 850d217..2e8f3ba 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -26,21 +26,31 @@ use anyhow::Context as _;
/// The configuration for nitrocli, usually read from configuration
/// files and environment variables.
-#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Deserialize)]
+#[derive(Clone, Copy, Debug, Default, PartialEq, merge::Merge, serde::Deserialize)]
pub struct Config {
/// The model to connect to.
pub model: Option<args::DeviceModel>,
/// Whether to bypass the cache for all secrets or not.
+ #[merge(strategy = merge::bool::overwrite_false)]
#[serde(default)]
pub no_cache: bool,
/// The log level.
+ #[merge(strategy = merge::num::overwrite_zero)]
#[serde(default)]
pub verbosity: u8,
}
impl Config {
pub fn load() -> anyhow::Result<Self> {
- load_user_config().map(|o| o.unwrap_or_default())
+ use merge::Merge as _;
+
+ let mut config = Config::default();
+ if let Some(user_config) = load_user_config()? {
+ config.merge(user_config);
+ }
+ config.merge(load_env_config()?);
+
+ Ok(config)
}
pub fn update(&mut self, args: &args::Args) {
@@ -62,6 +72,12 @@ fn load_user_config() -> anyhow::Result<Option<Config>> {
}
}
+fn load_env_config() -> anyhow::Result<Config> {
+ envy::prefixed("NITROCLI_")
+ .from_env()
+ .context("Failed to parse environment variables")
+}
+
pub fn read_config_file(path: &path::Path) -> anyhow::Result<Config> {
let s = fs::read_to_string(path)
.with_context(|| format!("Failed to read configuration file '{}'", path.display()))?;