summaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2020-01-23 10:54:09 +0100
committerDaniel Mueller <deso@posteo.net>2021-01-10 10:03:09 -0800
commit9d3c50ca3f9ff0bf12b1b25344959ed333ffbdee (patch)
treee1887917dc1eb94a23642ebe26b2f6c02b705188 /src/config.rs
parent887f3cb9aab12b390ae9efe09c1ff7f972e51c35 (diff)
downloadnitrocli-9d3c50ca3f9ff0bf12b1b25344959ed333ffbdee.tar.gz
nitrocli-9d3c50ca3f9ff0bf12b1b25344959ed333ffbdee.tar.bz2
Implement configuration handling
This patch implements basic configuration handling that reads a configuration file and stores the parsed data in the ExecCtx and RunCtx structs. It supports three configuration items: - model (previously only --model) - no_cache (previously only NITROCLI_NO_CACHE) - verbosity (previously only --verbose)
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs
new file mode 100644
index 0000000..850d217
--- /dev/null
+++ b/src/config.rs
@@ -0,0 +1,70 @@
+// config.rs
+
+// *************************************************************************
+// * Copyright (C) 2020 Daniel Mueller (deso@posteo.net) *
+// * *
+// * This program is free software: you can redistribute it and/or modify *
+// * it under the terms of the GNU General Public License as published by *
+// * the Free Software Foundation, either version 3 of the License, or *
+// * (at your option) any later version. *
+// * *
+// * This program is distributed in the hope that it will be useful, *
+// * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+// * GNU General Public License for more details. *
+// * *
+// * You should have received a copy of the GNU General Public License *
+// * along with this program. If not, see <http://www.gnu.org/licenses/>. *
+// *************************************************************************
+
+use std::fs;
+use std::path;
+
+use crate::args;
+
+use anyhow::Context as _;
+
+/// The configuration for nitrocli, usually read from configuration
+/// files and environment variables.
+#[derive(Clone, Copy, Debug, Default, PartialEq, 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.
+ #[serde(default)]
+ pub no_cache: bool,
+ /// The log level.
+ #[serde(default)]
+ pub verbosity: u8,
+}
+
+impl Config {
+ pub fn load() -> anyhow::Result<Self> {
+ load_user_config().map(|o| o.unwrap_or_default())
+ }
+
+ pub fn update(&mut self, args: &args::Args) {
+ if args.model.is_some() {
+ self.model = args.model;
+ }
+ if args.verbose > 0 {
+ self.verbosity = args.verbose;
+ }
+ }
+}
+
+fn load_user_config() -> anyhow::Result<Option<Config>> {
+ let path = path::Path::new("config.toml");
+ if path.is_file() {
+ read_config_file(&path).map(Some)
+ } else {
+ Ok(None)
+ }
+}
+
+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()))?;
+ toml::from_str(&s)
+ .with_context(|| format!("Failed to parse configuration file '{}'", path.display()))
+}