From edf7af1573d0469a02ecb4d8f7d2274f9cc935f9 Mon Sep 17 00:00:00 2001 From: Iru Sensei Date: Sun, 23 Jul 2023 15:22:42 +0200 Subject: [PATCH] feat: verify config file exists and can be read --- src/main.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/main.rs b/src/main.rs index 6e8994e..41a904b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,9 @@ use nostr_rs_relay::server::start_server; use std::sync::mpsc as syncmpsc; use std::sync::mpsc::{Receiver as MpscReceiver, Sender as MpscSender}; use std::thread; +use std::path::Path; +use std::fs; +use std::process; #[cfg(not(target_env = "msvc"))] use tikv_jemallocator::Jemalloc; use tracing::info; @@ -24,6 +27,31 @@ fn main() { // get config file name from args let config_file_arg = args.config; + // Quits if config file path is unreadable or does not exist + let config_file_path = config_file_arg.as_ref().map(|x| &**x).unwrap(); + let path = Path::new(&config_file_path); + + if !path.exists() { + eprintln!("Config file not found: {}", &config_file_path); + process::exit(1); + } + + if !path.is_file() { + eprintln!("Invalid config file path: {}", &config_file_path); + process::exit(1); + } + + if let Err(err) = fs::metadata(&path) { + eprintln!("Error while accessing file metadata: {}", err); + process::exit(1); + } + + if let Err(err) = fs::File::open(&path) { + eprintln!("Config file is not readable: {}", err); + process::exit(1); + } + + let mut _log_guard: Option = None; // configure settings from the config file (defaults to config.toml)