Added function for reading server config dirs. It's not complete yet.

This commit is contained in:
Thelie 2021-05-12 00:13:16 +02:00
parent 716584841e
commit 78d5bafae3
2 changed files with 26 additions and 8 deletions

View file

@ -16,16 +16,34 @@
*/ */
use std::{ use std::{
path::Path, path::PathBuf,
io::Read, io::Read,
fs::read_dir, fs::read_dir,
}; };
use toml::value::Value; use toml::value::Value;
fn parse_server_dir(mut config: Value, server_config_path: PathBuf)
-> Result<Value, Box<dyn std::error::Error>> {
let mut config = config.as_table_mut().unwrap().clone();
if server_config_path.is_dir() {
for entry in read_dir(server_config_path)? {
let entry = entry?;
match entry.file_name().to_str().ok_or("Could not read file name")? {
"config.toml" => (),
"clients.toml" => (),
"channels.toml" => (),
_ => (),
}
}
}
Ok(Value::from(config))
}
/// parses a toml config file and sets some default values /// parses a toml config file and sets some default values
/// if the corresponding fields are not set. /// if the corresponding fields are not set.
// TODO: Document config file options once they are stable. // TODO: Document config file options once they are stable.
pub fn get_main_config(config_path: &Path) pub fn get_main_config(config_path: PathBuf)
-> Result<Value, Box<dyn std::error::Error>> { -> Result<Value, Box<dyn std::error::Error>> {
// I once read that this multiple let style // I once read that this multiple let style
@ -55,14 +73,14 @@ pub fn get_main_config(config_path: &Path)
Ok(Value::from(config)) Ok(Value::from(config))
} }
pub fn get_server_config (config: Value, config_path: &Path) pub fn get_server_config(mut config: Value, config_path: PathBuf)
-> Result<Value, Box<dyn std::error::Error>> { -> Result<Value, Box<dyn std::error::Error>> {
if config_path.is_dir() { if config_path.is_dir() {
for entry in read_dir(config_path)? { for entry in read_dir(config_path)? {
let entry = entry?; let entry = entry?;
let server_path = entry.path(); let server_path = entry.path();
if server_path.is_dir(){ if server_path.is_dir(){
config = server_config_helper(config, server_path)?; config = parse_server_dir(config, server_path)?;
} }
} }
} }

View file

@ -21,7 +21,7 @@ pub mod config;
use std::{ use std::{
format, format,
env::args, env::args,
path::Path, path::PathBuf,
collections::VecDeque, collections::VecDeque,
}; };
use irc_proto::command::{ use irc_proto::command::{
@ -155,14 +155,14 @@ fn main() {
let mut config; let mut config;
match config_path { match config_path {
Some(p) => config = config::get_main_config(&Path::from(p)) Some(p) => config = config::get_main_config(PathBuf::from(p))
.expect("Could not get config"), .expect("Could not get config"),
None => config = config::get_main_config(&Path::from("/etc/Mention2Mail/config.toml")) None => config = config::get_main_config(PathBuf::from("/etc/Mention2Mail/config.toml"))
.expect("Could not get default config in /etc/Mention2Mail/default.toml"), .expect("Could not get default config in /etc/Mention2Mail/default.toml"),
} }
match server_conf_path { match server_conf_path {
Some(p) => config = config::get_server_config(config, p) Some(p) => config = config::get_server_config(config, PathBuf::from(p))
.expect("Could not get server config."), .expect("Could not get server config."),
None => (), None => (),
} }