From 4244157809fc18698553a50177100226f908b80a Mon Sep 17 00:00:00 2001 From: DastInDark <2350416+hitenkoku@users.noreply.github.com> Date: Wed, 28 Sep 2022 01:22:53 +0900 Subject: [PATCH] refactoring output option file already exists #713 --- src/detections/utils.rs | 9 +++++++++ src/main.rs | 32 +++++++++++++++++--------------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/detections/utils.rs b/src/detections/utils.rs index 2e4dd4f6..c938775d 100644 --- a/src/detections/utils.rs +++ b/src/detections/utils.rs @@ -480,6 +480,15 @@ where } } +/// Check file path exist. If path is existed, output alert message. +pub fn check_file_expect_not_exist(path: &Path, exist_alert_str: String) -> bool { + let ret = path.exists(); + if ret { + AlertMessage::alert(&exist_alert_str).ok(); + } + ret +} + #[cfg(test)] mod tests { use std::path::Path; diff --git a/src/main.rs b/src/main.rs index 280770ce..b08f2659 100644 --- a/src/main.rs +++ b/src/main.rs @@ -219,20 +219,21 @@ impl App { pivot_key_unions.iter().for_each(|(key, _)| { let keywords_file_name = csv_path.as_path().display().to_string() + "-" + key + ".txt"; - if Path::new(&keywords_file_name).exists() { - AlertMessage::alert(&format!( + utils::check_file_expect_not_exist( + Path::new(&keywords_file_name), + format!( " The file {} already exists. Please specify a different filename.", &keywords_file_name - )) - .ok(); - } + ), + ); }); - if csv_path.exists() { - AlertMessage::alert(&format!( + if utils::check_file_expect_not_exist( + csv_path, + format!( " The file {} already exists. Please specify a different filename.", csv_path.as_os_str().to_str().unwrap() - )) - .ok(); + ), + ) { return; } } @@ -263,14 +264,15 @@ impl App { println!(); } - if let Some(path) = &configs::CONFIG.read().unwrap().args.html_report { + if let Some(html_path) = &configs::CONFIG.read().unwrap().args.html_report { // if already exists same html report file. output alert message and exit - if path.exists() { - AlertMessage::alert(&format!( + if utils::check_file_expect_not_exist( + html_path.as_path(), + format!( " The file {} already exists. Please specify a different filename.", - path.to_str().unwrap() - )) - .ok(); + html_path.to_str().unwrap() + ), + ) { return; } }