From d44041040d755306c39d6de8da5b42d7ded6808c Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Wed, 25 Sep 2024 15:13:34 +0200 Subject: added notifications and improved stuff --- src/config.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index 46d2204..848be19 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,6 +4,8 @@ use config::{File, Map}; use serde::{Deserialize, Serialize}; use tracing::{debug, trace}; +use crate::{error::{Error, Result}, packages::Manager}; + #[derive(Debug, Serialize, Deserialize)] #[serde(default)] pub struct Config { @@ -11,6 +13,7 @@ pub struct Config { pub directories: Vec, pub custom_directories: Map, pub device: String, + pub package_manager: Manager, } impl Default for Config { @@ -22,17 +25,18 @@ impl Default for Config { device: gethostname::gethostname() .into_string() .expect("invalid hostname string"), + package_manager: Manager::from_sys().expect("couldn't get package manager"), } } } impl Config { - pub fn load(path: Option) -> core::result::Result { + pub fn load(path: Option) -> Result { debug!("load config"); let source = if let Some(source) = path { source } else { - Self::get_location() + Self::get_location()? }; let config = config::Config::builder() @@ -40,14 +44,14 @@ impl Config { .add_source(config::Environment::with_prefix("FXBAUP").separator("_")) .build()?; - let cfg = config.try_deserialize(); + let cfg = config.try_deserialize()?; trace!(?cfg, "loaded config"); - cfg + Ok(cfg) } - pub fn generate() -> crate::error::Result<()> { - let loc = Self::get_location(); + pub fn generate() -> Result<()> { + let loc = Self::get_location()?; create_dir_all(loc.parent().unwrap())?; let mut f = std::fs::File::create(loc)?; f.write_all(toml::to_string(&Self::default())?.as_bytes())?; @@ -55,10 +59,12 @@ impl Config { Ok(()) } - fn get_location() -> PathBuf { - let mut conf_dir = dirs::config_local_dir().unwrap(); - conf_dir.push("arbs"); - conf_dir.push("config.toml"); - conf_dir + fn get_location() -> Result { + let Some(mut config_dir) = dirs::config_dir() else { + return Err(Error::NoSysDir); + }; + config_dir.push(env!("CARGO_PKG_NAME")); + config_dir.push("config.toml"); + Ok(config_dir) } } -- cgit v1.2.3