write example if not found

This commit is contained in:
blek 2023-10-21 01:31:08 +10:00
parent e9f146a0a2
commit fada0ebc6c
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 18 additions and 0 deletions

View File

@ -5,6 +5,8 @@
use std::{env::var, net::SocketAddr, path::Path, fs};
pub const DEFAULT_CONFIG: &'static str = include_str!("../config/filed.toml.example");
#[derive(Debug, Clone)]
pub struct Redis {
pub pass: String,
@ -60,7 +62,23 @@ pub fn loadenv() -> Result<Env, Box<dyn std::error::Error>> {
confpath: {
let spath: String = get_var("CONF_FILE").unwrap_or("/etc/filed/filed.toml".into());
let path = Path::new(&spath);
let mut dirpath = path.clone().components();
dirpath.next_back();
let dirpath = dirpath.as_path();
if ! path.is_file() {
if dirpath.is_dir() {
log::info!("Config file does not exist, but found config directory. Trying to write the example");
let wrote = fs::write(path, DEFAULT_CONFIG.clone());
if wrote.is_err() {
log::info!("Could not write example because of the following error: {:?}", wrote.unwrap_err());
log::info!("Giving up");
} else {
log::info!("Wrote example to {}", spath);
}
}
return Err(format!("CONF_FILE is {}, which is not a file!", spath).into())
}
spath