Refactored User Config loading, closes #3
All checks were successful
Run Tests on Code / run-tests (push) Successful in 11s
All checks were successful
Run Tests on Code / run-tests (push) Successful in 11s
This commit is contained in:
parent
e1260877b6
commit
20af5172b4
1 changed files with 58 additions and 27 deletions
85
src/main.rs
85
src/main.rs
|
@ -1,8 +1,7 @@
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fmt::{Display, format, Formatter};
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::net::{IpAddr, Ipv6Addr};
|
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 actix_web::{web, App, HttpResponse, HttpServer, get, Responder, HttpRequest};
|
||||||
use log::{LevelFilter};
|
use log::{LevelFilter};
|
||||||
use systemd_journal_logger::{connected_to_journal, JournalLog};
|
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)
|
// query every home directory for a config file (just attempt a load, an empty config is perfectly fine)
|
||||||
// merge all configs into one
|
// merge all configs into one
|
||||||
let mut user_config = UserConfig::default();
|
let mut user_config = UserConfig::default();
|
||||||
for entry in fs::read_dir("/home").expect("home directory is expected") {
|
let root_contents = match fs::read_dir("/") {
|
||||||
let entry = entry.expect("home directory is expected to have at least one directory");
|
Ok(contents) => { contents }
|
||||||
let path = entry.path();
|
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() {
|
if path.is_dir() {
|
||||||
let config_path = format!("{}/.config/{}/config.toml", path.display().to_string().as_str(), env!("CARGO_PKG_NAME"));
|
match path.display().to_string().as_str() {
|
||||||
let mut path_config: UserConfig = match confy::load_path(config_path) {
|
"/root" => {
|
||||||
Ok(data) => data,
|
let mut root_configs = Self::load_user_config_directory(path);
|
||||||
Err(e) => {
|
user_config.domain_configs.append(&mut root_configs.domain_configs);
|
||||||
match &e {
|
},
|
||||||
confy::ConfyError::GeneralLoadError(os_error) => {
|
"/home" => {
|
||||||
if os_error.raw_os_error() == Some(13) {
|
match fs::read_dir(path) {
|
||||||
let msg = format!("Missing read permissions for {}, skipping", path.display().to_string().as_str());
|
Ok(home_contents) => {
|
||||||
warn!(msg);
|
home_contents.for_each(|home_folder| {
|
||||||
UserConfig::default()
|
let home_folder_path = home_folder.expect("Unexpected Error while Unwrapping the listed Home Dir Entry").path();
|
||||||
}
|
if home_folder_path.is_dir() {
|
||||||
else {
|
let mut user_folder_configs = Self::load_user_config_directory(home_folder_path);
|
||||||
error!(e);
|
user_config.domain_configs.append(&mut user_folder_configs.domain_configs);
|
||||||
return Err(Box::new(e));
|
}
|
||||||
}
|
})
|
||||||
},
|
},
|
||||||
_ => {
|
Err(e) => {
|
||||||
error!(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 etc_path = format!("/etc/{}", env!("CARGO_PKG_NAME"));
|
||||||
let usr_path = format!("/usr/local/share/{}", 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()) {
|
match confy::load_path(path.clone()) {
|
||||||
Ok(data) => {
|
Ok(data) => {
|
||||||
let msg = format!("Using {}", path);
|
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]
|
#[actix_web::main]
|
||||||
|
|
Loading…
Reference in a new issue