From 20af5172b4dcba63153f3049d31da3b0a6ff6a01 Mon Sep 17 00:00:00 2001 From: Neshura Date: Wed, 10 Apr 2024 18:55:17 +0200 Subject: [PATCH] Refactored User Config loading, closes #3 --- src/main.rs | 85 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 58 insertions(+), 27 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5ff6651..494cd6e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,7 @@ use std::error::Error; -use std::fmt::{Display, format, Formatter}; use std::fs; use std::net::{IpAddr, Ipv6Addr}; -use std::path::Path; +use std::path::{Path, PathBuf}; use actix_web::{web, App, HttpResponse, HttpServer, get, Responder, HttpRequest}; use log::{LevelFilter}; use systemd_journal_logger::{connected_to_journal, JournalLog}; @@ -74,36 +73,42 @@ impl Config { // query every home directory for a config file (just attempt a load, an empty config is perfectly fine) // merge all configs into one let mut user_config = UserConfig::default(); - for entry in fs::read_dir("/home").expect("home directory is expected") { - let entry = entry.expect("home directory is expected to have at least one directory"); - let path = entry.path(); + let root_contents = match fs::read_dir("/") { + Ok(contents) => { contents } + Err(e) => { + error!(e); + return Err(Box::new(e)); + } + }; + + root_contents.for_each(|directory| { + let path = directory.expect("Unexpected Error while Unwrapping the listed Dir Entry").path(); if path.is_dir() { - let config_path = format!("{}/.config/{}/config.toml", path.display().to_string().as_str(), env!("CARGO_PKG_NAME")); - let mut path_config: UserConfig = match confy::load_path(config_path) { - Ok(data) => data, - Err(e) => { - match &e { - confy::ConfyError::GeneralLoadError(os_error) => { - if os_error.raw_os_error() == Some(13) { - let msg = format!("Missing read permissions for {}, skipping", path.display().to_string().as_str()); - warn!(msg); - UserConfig::default() - } - else { - error!(e); - return Err(Box::new(e)); - } + match path.display().to_string().as_str() { + "/root" => { + let mut root_configs = Self::load_user_config_directory(path); + user_config.domain_configs.append(&mut root_configs.domain_configs); + }, + "/home" => { + match fs::read_dir(path) { + Ok(home_contents) => { + home_contents.for_each(|home_folder| { + let home_folder_path = home_folder.expect("Unexpected Error while Unwrapping the listed Home Dir Entry").path(); + if home_folder_path.is_dir() { + let mut user_folder_configs = Self::load_user_config_directory(home_folder_path); + user_config.domain_configs.append(&mut user_folder_configs.domain_configs); + } + }) }, - _ => { + Err(e) => { error!(e); - return Err(Box::new(e)); } } - } - }; - user_config.domain_configs.append(&mut path_config.domain_configs) + }, + _ => {} + } } - } + }); let etc_path = format!("/etc/{}", env!("CARGO_PKG_NAME")); let usr_path = format!("/usr/local/share/{}", env!("CARGO_PKG_NAME")); @@ -123,7 +128,7 @@ impl Config { } }; - let path = format!("{}/config.toml", system_path.display().to_string().as_str()); + let path = format!("{}/config.toml", system_path.display()); match confy::load_path(path.clone()) { Ok(data) => { let msg = format!("Using {}", path); @@ -139,6 +144,32 @@ impl Config { } } } + + fn load_user_config_directory(path: PathBuf) -> UserConfig { + let config_path = format!("{}/.config/{}/domains.toml", path.display(), env!("CARGO_PKG_NAME")); + match confy::load_path(config_path) { + Ok(data) => data, + Err(e) => { + match &e { + confy::ConfyError::GeneralLoadError(os_error) => { + if os_error.raw_os_error() == Some(13) { + let msg = format!("Missing read permissions for {}, skipping", path.display().to_string().as_str()); + warn!(msg); + UserConfig::default() + } + else { + error!(e); + UserConfig::default() + } + }, + _ => { + error!(e); + UserConfig::default() + } + } + } + } + } } #[actix_web::main]