From 692237b53248f42e9bea426fed5804e01d6c20ed Mon Sep 17 00:00:00 2001 From: hanbings Date: Fri, 10 May 2024 11:03:14 +0800 Subject: [PATCH 1/5] Add -ok and -okdir support. --- src/find/matchers/exec.rs | 18 +++++++++++++++++- src/find/matchers/mod.rs | 5 +++-- tests/exec_unit_tests.rs | 6 ++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/find/matchers/exec.rs b/src/find/matchers/exec.rs index 23055e06..4916b5b1 100644 --- a/src/find/matchers/exec.rs +++ b/src/find/matchers/exec.rs @@ -6,7 +6,7 @@ use std::error::Error; use std::ffi::OsString; -use std::io::{stderr, Write}; +use std::io::{stderr, stdin, stdout, Write}; use std::path::Path; use std::process::Command; @@ -21,6 +21,7 @@ pub struct SingleExecMatcher { executable: String, args: Vec, exec_in_parent_dir: bool, + interactive: bool, } impl SingleExecMatcher { @@ -28,6 +29,7 @@ impl SingleExecMatcher { executable: &str, args: &[&str], exec_in_parent_dir: bool, + interactive: bool, ) -> Result> { let transformed_args = args .iter() @@ -46,6 +48,7 @@ impl SingleExecMatcher { executable: executable.to_string(), args: transformed_args, exec_in_parent_dir, + interactive, }) } } @@ -63,6 +66,19 @@ impl Matcher for SingleExecMatcher { file_info.path().to_path_buf() }; + // support interactive exec + if self.interactive { + let tips = format!("{} ... {} > ? [y/n]: ", self.executable, path_to_file.to_string_lossy()); + write!(stdout(), "{}", tips).unwrap(); + stdout().flush().unwrap(); + + let mut input = String::new(); + let _result = stdin().read_line(&mut input).unwrap(); + if input.trim().eq("n") { + return true; + } + } + for arg in &self.args { match *arg { Arg::LiteralArg(ref a) => command.arg(a.as_os_str()), diff --git a/src/find/matchers/mod.rs b/src/find/matchers/mod.rs index dfe21fbf..e150fccf 100644 --- a/src/find/matchers/mod.rs +++ b/src/find/matchers/mod.rs @@ -596,7 +596,7 @@ fn build_matcher_tree( Some(SizeMatcher::new(size, &unit)?.into_box()) } "-empty" => Some(EmptyMatcher::new().into_box()), - "-exec" | "-execdir" => { + "-exec" | "-execdir" | "-ok" | "-okdir" => { let mut arg_index = i + 1; while arg_index < args.len() && args[arg_index] != ";" { if args[arg_index - 1] == "{}" && args[arg_index] == "+" { @@ -616,9 +616,10 @@ fn build_matcher_tree( let expression = args[i]; let executable = args[i + 1]; let exec_args = &args[i + 2..arg_index]; + let interactive = expression == "-ok" || expression == "-okdir"; i = arg_index; Some( - SingleExecMatcher::new(executable, exec_args, expression == "-execdir")? + SingleExecMatcher::new(executable, exec_args, expression == "-execdir", interactive)? .into_box(), ) } diff --git a/tests/exec_unit_tests.rs b/tests/exec_unit_tests.rs index 7f1502c4..133c42f3 100644 --- a/tests/exec_unit_tests.rs +++ b/tests/exec_unit_tests.rs @@ -34,6 +34,7 @@ fn matching_executes_code() { &path_to_testing_commandline(), &[temp_dir_path.as_ref(), "abc", "{}", "xyz"], false, + false, ) .expect("Failed to create matcher"); let deps = FakeDependencies::new(); @@ -65,6 +66,7 @@ fn matching_executes_code_in_files_directory() { &path_to_testing_commandline(), &[temp_dir_path.as_ref(), "abc", "{}", "xyz"], true, + false, ) .expect("Failed to create matcher"); let deps = FakeDependencies::new(); @@ -96,6 +98,7 @@ fn matching_embedded_filename() { &path_to_testing_commandline(), &[temp_dir_path.as_ref(), "abc{}x{}yz"], false, + false ) .expect("Failed to create matcher"); let deps = FakeDependencies::new(); @@ -129,6 +132,7 @@ fn execdir_in_current_directory() { &path_to_testing_commandline(), &[temp_dir_path.as_ref(), "abc", "{}", "xyz"], true, + false, ) .expect("Failed to create matcher"); let deps = FakeDependencies::new(); @@ -167,6 +171,7 @@ fn execdir_in_root_directory() { &path_to_testing_commandline(), &[temp_dir_path.as_ref(), "abc", "{}", "xyz"], true, + false, ) .expect("Failed to create matcher"); let deps = FakeDependencies::new(); @@ -205,6 +210,7 @@ fn matching_fails_if_executable_fails() { "xyz", ], true, + false, ) .expect("Failed to create matcher"); let deps = FakeDependencies::new(); From 50ebd88117842eb86a5c870e5b2c20c687a67672 Mon Sep 17 00:00:00 2001 From: hanbings Date: Fri, 10 May 2024 13:22:36 +0800 Subject: [PATCH 2/5] Run cargo clippy. --- src/find/matchers/exec.rs | 11 +++++++---- src/find/matchers/mod.rs | 9 +++++++-- tests/exec_unit_tests.rs | 2 +- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/find/matchers/exec.rs b/src/find/matchers/exec.rs index 4916b5b1..1377cdf9 100644 --- a/src/find/matchers/exec.rs +++ b/src/find/matchers/exec.rs @@ -6,7 +6,7 @@ use std::error::Error; use std::ffi::OsString; -use std::io::{stderr, stdin, stdout, Write}; +use std::io::{stderr, stdin, Write}; use std::path::Path; use std::process::Command; @@ -68,9 +68,12 @@ impl Matcher for SingleExecMatcher { // support interactive exec if self.interactive { - let tips = format!("{} ... {} > ? [y/n]: ", self.executable, path_to_file.to_string_lossy()); - write!(stdout(), "{}", tips).unwrap(); - stdout().flush().unwrap(); + let tips = format!( + "{} ... {} > ? [y/n]: ", + self.executable, + path_to_file.to_string_lossy() + ); + print!("{}", tips); let mut input = String::new(); let _result = stdin().read_line(&mut input).unwrap(); diff --git a/src/find/matchers/mod.rs b/src/find/matchers/mod.rs index e150fccf..07ecaf13 100644 --- a/src/find/matchers/mod.rs +++ b/src/find/matchers/mod.rs @@ -619,8 +619,13 @@ fn build_matcher_tree( let interactive = expression == "-ok" || expression == "-okdir"; i = arg_index; Some( - SingleExecMatcher::new(executable, exec_args, expression == "-execdir", interactive)? - .into_box(), + SingleExecMatcher::new( + executable, + exec_args, + expression == "-execdir", + interactive, + )? + .into_box(), ) } #[cfg(unix)] diff --git a/tests/exec_unit_tests.rs b/tests/exec_unit_tests.rs index 133c42f3..c2ad0dec 100644 --- a/tests/exec_unit_tests.rs +++ b/tests/exec_unit_tests.rs @@ -98,7 +98,7 @@ fn matching_embedded_filename() { &path_to_testing_commandline(), &[temp_dir_path.as_ref(), "abc{}x{}yz"], false, - false + false, ) .expect("Failed to create matcher"); let deps = FakeDependencies::new(); From cdcda98aed9133334615b790d70b14abb040b1c6 Mon Sep 17 00:00:00 2001 From: hanbings Date: Fri, 10 May 2024 13:46:18 +0800 Subject: [PATCH 3/5] Fix -ok and -okdir result inversion. --- src/find/matchers/exec.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/find/matchers/exec.rs b/src/find/matchers/exec.rs index 1377cdf9..249087b0 100644 --- a/src/find/matchers/exec.rs +++ b/src/find/matchers/exec.rs @@ -6,7 +6,7 @@ use std::error::Error; use std::ffi::OsString; -use std::io::{stderr, stdin, Write}; +use std::io::{stderr, stdin, stdout, Write}; use std::path::Path; use std::process::Command; @@ -73,12 +73,13 @@ impl Matcher for SingleExecMatcher { self.executable, path_to_file.to_string_lossy() ); - print!("{}", tips); + write!(stdout(), "{}", tips).unwrap(); + stdout().flush().unwrap(); let mut input = String::new(); let _result = stdin().read_line(&mut input).unwrap(); - if input.trim().eq("n") { - return true; + if !input.trim().contains("y") { + return false; } } From bdbcca99ad862e72f66e37c34377062c14d0691e Mon Sep 17 00:00:00 2001 From: hanbings Date: Fri, 10 May 2024 13:52:55 +0800 Subject: [PATCH 4/5] Run cargo clippy. --- src/find/matchers/exec.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/find/matchers/exec.rs b/src/find/matchers/exec.rs index 249087b0..df3f6473 100644 --- a/src/find/matchers/exec.rs +++ b/src/find/matchers/exec.rs @@ -73,12 +73,13 @@ impl Matcher for SingleExecMatcher { self.executable, path_to_file.to_string_lossy() ); + #[allow(clippy::explicit_write)] write!(stdout(), "{}", tips).unwrap(); stdout().flush().unwrap(); let mut input = String::new(); let _result = stdin().read_line(&mut input).unwrap(); - if !input.trim().contains("y") { + if !input.trim().contains('y') { return false; } } From 18b88ffb595e36730ebaf8baf40f5704ac419086 Mon Sep 17 00:00:00 2001 From: hanbings Date: Fri, 10 May 2024 14:03:48 +0800 Subject: [PATCH 5/5] Fix -ok and -okdir abnormal parameter detection. --- src/find/matchers/exec.rs | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/find/matchers/exec.rs b/src/find/matchers/exec.rs index df3f6473..29a64960 100644 --- a/src/find/matchers/exec.rs +++ b/src/find/matchers/exec.rs @@ -66,24 +66,6 @@ impl Matcher for SingleExecMatcher { file_info.path().to_path_buf() }; - // support interactive exec - if self.interactive { - let tips = format!( - "{} ... {} > ? [y/n]: ", - self.executable, - path_to_file.to_string_lossy() - ); - #[allow(clippy::explicit_write)] - write!(stdout(), "{}", tips).unwrap(); - stdout().flush().unwrap(); - - let mut input = String::new(); - let _result = stdin().read_line(&mut input).unwrap(); - if !input.trim().contains('y') { - return false; - } - } - for arg in &self.args { match *arg { Arg::LiteralArg(ref a) => command.arg(a.as_os_str()), @@ -104,6 +86,25 @@ impl Matcher for SingleExecMatcher { } } } + + // support interactive exec + if self.interactive { + let tips = format!( + "{} ... {} > ? [y/n]: ", + self.executable, + path_to_file.to_string_lossy() + ); + #[allow(clippy::explicit_write)] + write!(stdout(), "{}", tips).unwrap(); + stdout().flush().unwrap(); + + let mut input = String::new(); + let _result = stdin().read_line(&mut input).unwrap(); + if !input.trim().contains('y') { + return false; + } + } + match command.status() { Ok(status) => status.success(), Err(e) => {