Improve config file handling
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: Jacob Kiers <code@kiers.eu>
This commit is contained in:
Jacob Kiers 2024-02-22 23:07:11 +01:00
parent ee67f7883e
commit 9d9f89881d

View File

@ -7,11 +7,19 @@ use crate::config::ConfigV1;
use crate::servers::Server; use crate::servers::Server;
use log::{debug, error}; use log::{debug, error};
use std::env; use std::path::PathBuf;
use std::path::Path;
fn main() { fn main() {
let config_path = find_config(); let config_path = match find_config() {
Ok(p) => p,
Err(paths) => {
println!("Could not find config file. Tried paths:");
for p in paths {
println!("- {}", p);
}
std::process::exit(1);
}
};
let config = match ConfigV1::new(&config_path) { let config = match ConfigV1::new(&config_path) {
Ok(config) => config, Ok(config) => config,
@ -29,16 +37,28 @@ fn main() {
error!("Server ended with errors"); error!("Server ended with errors");
} }
fn find_config() -> String { fn find_config() -> Result<String, Vec<String>> {
let config_path = env::var("L4P_CONFIG").unwrap_or_else(|_| "/etc/l4p/l4p.yaml".to_string()); let possible_paths = ["/etc/l4p", ""];
let possible_names = ["l4p.yaml", "config.yaml"];
if Path::new(&config_path).exists() { let mut tried_paths = Vec::<String>::new();
return config_path;
for path in possible_paths
.iter()
.flat_map(|&path| {
possible_names
.iter()
.map(move |&file| PathBuf::new().join(path).join(file))
})
.collect::<Vec<PathBuf>>()
{
let path_str = path.to_string_lossy().to_string();
if path.exists() {
return Ok(path_str);
} }
if Path::new("config.yaml").exists() { tried_paths.push(path_str);
return String::from("config.yaml");
} }
String::from("") Err(tried_paths)
} }