diff --git a/CHANGELOG-Japanese.md b/CHANGELOG-Japanese.md index 561bffbd..5b8fbc00 100644 --- a/CHANGELOG-Japanese.md +++ b/CHANGELOG-Japanese.md @@ -12,7 +12,7 @@ **バグ修正:** -- XXX +- cargo runコマンドでhayabusaを実行するとconfigフォルダの読み込みエラーが発生する問題を修正した。 (#618) (@hitenkoku) ## v1.4.1 [2022/06/30] diff --git a/CHANGELOG.md b/CHANGELOG.md index ea9409cd..4dbdf956 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ **Bug Fixes:** -- XXX +- Fixed error due to the files in the config folder cannot be read. (#618) (@hitenkoku) ## v1.4.1 [2022/06/30] diff --git a/Cargo.lock b/Cargo.lock index 9a44a3db..72706daf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -673,7 +673,7 @@ dependencies = [ [[package]] name = "hayabusa" -version = "1.4.1" +version = "1.4.2-dev" dependencies = [ "base64", "bytesize", diff --git a/src/afterfact.rs b/src/afterfact.rs index b2cf7f25..152fe67f 100644 --- a/src/afterfact.rs +++ b/src/afterfact.rs @@ -65,8 +65,7 @@ lazy_static! { /// level_color.txtファイルを読み込み対応する文字色のマッピングを返却する関数 pub fn set_output_color() -> HashMap { let read_result = utils::read_csv( - CURRENT_EXE_PATH - .join("config/level_color.txt") + utils::check_setting_path(&CURRENT_EXE_PATH.to_path_buf(), "config/level_color.txt") .to_str() .unwrap(), ); diff --git a/src/detections/configs.rs b/src/detections/configs.rs index 8603300e..31dd83ee 100644 --- a/src/detections/configs.rs +++ b/src/detections/configs.rs @@ -241,16 +241,20 @@ impl ConfigReader<'_> { args: parse, headless_help: String::default(), event_timeline_config: load_eventcode_info( - CURRENT_EXE_PATH - .join("rules/config/statistics_event_info.txt") - .to_str() - .unwrap(), + utils::check_setting_path( + &CURRENT_EXE_PATH.to_path_buf(), + "rules/config/statistics_event_info.txt", + ) + .to_str() + .unwrap(), ), target_eventids: load_target_ids( - CURRENT_EXE_PATH - .join("rules/config/target_event_IDs.txt") - .to_str() - .unwrap(), + utils::check_setting_path( + &CURRENT_EXE_PATH.to_path_buf(), + "rules/config/target_event_IDs.txt", + ) + .to_str() + .unwrap(), ), } } diff --git a/src/detections/print.rs b/src/detections/print.rs index 4d0daf2d..96014349 100644 --- a/src/detections/print.rs +++ b/src/detections/print.rs @@ -54,18 +54,19 @@ lazy_static! { pub static ref STATISTICS_FLAG: bool = configs::CONFIG.read().unwrap().args.statistics; pub static ref LOGONSUMMARY_FLAG: bool = configs::CONFIG.read().unwrap().args.logon_summary; pub static ref TAGS_CONFIG: HashMap = Message::create_output_filter_config( - CURRENT_EXE_PATH - .join("config/output_tag.txt") + utils::check_setting_path(&CURRENT_EXE_PATH.to_path_buf(), "config/output_tag.txt") .to_str() .unwrap(), true, configs::CONFIG.read().unwrap().args.all_tags ); pub static ref CH_CONFIG: HashMap = Message::create_output_filter_config( - CURRENT_EXE_PATH - .join("rules/config/channel_abbreviations.txt") - .to_str() - .unwrap(), + utils::check_setting_path( + &CURRENT_EXE_PATH.to_path_buf(), + "rules/config/channel_abbreviations.txt" + ) + .to_str() + .unwrap(), false, configs::CONFIG.read().unwrap().args.all_tags ); diff --git a/src/detections/utils.rs b/src/detections/utils.rs index 18754e3d..1dec5959 100644 --- a/src/detections/utils.rs +++ b/src/detections/utils.rs @@ -4,6 +4,8 @@ extern crate regex; use crate::detections::configs; use crate::detections::configs::CURRENT_EXE_PATH; +use std::path::Path; +use std::path::PathBuf; use termcolor::Color; @@ -69,8 +71,7 @@ pub fn value_to_string(value: &Value) -> Option { pub fn read_txt(filename: &str) -> Result, String> { let filepath = if filename.starts_with("./") { - CURRENT_EXE_PATH - .join(filename) + check_setting_path(&CURRENT_EXE_PATH.to_path_buf(), filename) .to_str() .unwrap() .to_string() @@ -376,9 +377,20 @@ pub fn make_ascii_titlecase(s: &mut str) -> String { } } +/// base_path/path が存在するかを確認し、存在しなければカレントディレクトリを参照するpathを返す関数 +pub fn check_setting_path(base_path: &Path, path: &str) -> PathBuf { + if base_path.join(path).exists() { + base_path.join(path) + } else { + Path::new(path).to_path_buf() + } +} + #[cfg(test)] mod tests { - use crate::detections::utils::{self, make_ascii_titlecase}; + use std::path::Path; + + use crate::detections::utils::{self, check_setting_path, make_ascii_titlecase}; use regex::Regex; use serde_json::Value; @@ -540,4 +552,31 @@ mod tests { ); assert_eq!(make_ascii_titlecase("β".to_string().as_mut()), "β"); } + + #[test] + /// 与えられたパスからファイルの存在確認ができているかのテスト + fn test_check_setting_path() { + let exist_path = Path::new("./test_files").to_path_buf(); + let not_exist_path = Path::new("not_exist_path").to_path_buf(); + assert_eq!( + check_setting_path(¬_exist_path, "rules") + .to_str() + .unwrap(), + "rules" + ); + assert_eq!( + check_setting_path(¬_exist_path, "fake") + .to_str() + .unwrap(), + "fake" + ); + assert_eq!( + check_setting_path(&exist_path, "rules").to_str().unwrap(), + exist_path.join("rules").to_str().unwrap() + ); + assert_eq!( + check_setting_path(&exist_path, "fake").to_str().unwrap(), + "fake" + ); + } } diff --git a/src/main.rs b/src/main.rs index ffe09ab7..ad182426 100644 --- a/src/main.rs +++ b/src/main.rs @@ -79,10 +79,12 @@ impl App { fn exec(&mut self) { if *PIVOT_KEYWORD_LIST_FLAG { load_pivot_keywords( - CURRENT_EXE_PATH - .join("config/pivot_keywords.txt") - .to_str() - .unwrap(), + utils::check_setting_path( + &CURRENT_EXE_PATH.to_path_buf(), + "config/pivot_keywords.txt", + ) + .to_str() + .unwrap(), ); } @@ -136,28 +138,23 @@ impl App { return; } // 実行時のexeファイルのパスをベースに変更する必要があるためデフォルトの値であった場合はそのexeファイルと同一階層を探すようにする - if !CURRENT_EXE_PATH.join("config").exists() { + if !CURRENT_EXE_PATH.join("config").exists() && !Path::new("./config").exists() { AlertMessage::alert( "Hayabusa could not find the config directory.\nPlease make sure that it is in the same directory as the hayabusa executable." ) .ok(); return; } - // ワーキングディレクトリ以外からの実行の際にrules-configオプションの指定がないとエラーが発生することを防ぐための処理 - if configs::CONFIG - .read() - .unwrap() - .args - .config - .to_str() - .unwrap() - == "./rules/config" - { - configs::CONFIG.write().unwrap().args.config = CURRENT_EXE_PATH.join("rules/config"); + // カレントディレクトリ以外からの実行の際にrules-configオプションの指定がないとエラーが発生することを防ぐための処理 + if configs::CONFIG.read().unwrap().args.config == Path::new("./rules/config") { + configs::CONFIG.write().unwrap().args.config = + utils::check_setting_path(&CURRENT_EXE_PATH.to_path_buf(), "./rules/config"); } - // ワーキングディレクトリ以外からの実行の際にrules-configオプションの指定がないとエラーが発生することを防ぐための処理 - if configs::CONFIG.read().unwrap().args.rules.to_str().unwrap() == "./rules" { - configs::CONFIG.write().unwrap().args.rules = CURRENT_EXE_PATH.join("rules"); + + // カレントディレクトリ以外からの実行の際にrulesオプションの指定がないとエラーが発生することを防ぐための処理 + if configs::CONFIG.read().unwrap().args.rules == Path::new("./rules") { + configs::CONFIG.write().unwrap().args.rules = + utils::check_setting_path(&CURRENT_EXE_PATH.to_path_buf(), "./rules"); } if let Some(csv_path) = &configs::CONFIG.read().unwrap().args.output { @@ -258,10 +255,12 @@ impl App { .unwrap(); let level_tuning_config_path = match level_tuning_val { Some(path) => path.to_owned(), - _ => CURRENT_EXE_PATH - .join("./rules/config/level_tuning.txt") - .display() - .to_string(), + _ => utils::check_setting_path( + &CURRENT_EXE_PATH.to_path_buf(), + "./rules/config/level_tuning.txt", + ) + .display() + .to_string(), }; if Path::new(&level_tuning_config_path).exists() { @@ -463,7 +462,10 @@ impl App { } fn print_contributors(&self) { - match fs::read_to_string(CURRENT_EXE_PATH.join("contributors.txt")) { + match fs::read_to_string(utils::check_setting_path( + &CURRENT_EXE_PATH.to_path_buf(), + "contributors.txt", + )) { Ok(contents) => { write_color_buffer( &BufferWriter::stdout(ColorChoice::Always), @@ -712,7 +714,7 @@ impl App { /// output logo fn output_logo(&self) { - let fp = CURRENT_EXE_PATH.join("art/logo.txt"); + let fp = utils::check_setting_path(&CURRENT_EXE_PATH.to_path_buf(), "art/logo.txt"); let content = fs::read_to_string(fp).unwrap_or_default(); let output_color = if configs::CONFIG.read().unwrap().args.no_color { None @@ -739,7 +741,7 @@ impl App { match eggs.get(exec_datestr) { None => {} Some(path) => { - let egg_path = CURRENT_EXE_PATH.join(path); + let egg_path = utils::check_setting_path(&CURRENT_EXE_PATH.to_path_buf(), path); let content = fs::read_to_string(egg_path).unwrap_or_default(); write_color_buffer( &BufferWriter::stdout(ColorChoice::Always),