From 553bbac36bdc483135a7053ca64507e01397e5e1 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Mon, 9 Sep 2024 23:03:49 +0200 Subject: add package manager recognition --- src/config.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index 13dd0e4..46d2204 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,3 +1,5 @@ +use std::{fs::create_dir_all, io::Write, path::PathBuf}; + use config::{File, Map}; use serde::{Deserialize, Serialize}; use tracing::{debug, trace}; @@ -6,7 +8,6 @@ use tracing::{debug, trace}; #[serde(default)] pub struct Config { pub root: String, - pub user: Vec, pub directories: Vec, pub custom_directories: Map, pub device: String, @@ -16,7 +17,6 @@ impl Default for Config { fn default() -> Self { Self { root: "/mnt/backup".to_string(), - user: vec![], directories: vec![], custom_directories: Map::new(), device: gethostname::gethostname() @@ -27,10 +27,16 @@ impl Default for Config { } impl Config { - pub fn load() -> Result { + pub fn load(path: Option) -> core::result::Result { debug!("load config"); + let source = if let Some(source) = path { + source + } else { + Self::get_location() + }; + let config = config::Config::builder() - .add_source(File::with_name("config.toml").required(false)) + .add_source(File::with_name(&source.to_string_lossy()).required(false)) .add_source(config::Environment::with_prefix("FXBAUP").separator("_")) .build()?; @@ -39,4 +45,20 @@ impl Config { cfg } + + pub fn generate() -> crate::error::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())?; + + 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 + } } -- cgit v1.2.3