From 7fffb58b6bf6be555959de28523320d51e4b93f3 Mon Sep 17 00:00:00 2001 From: "Victor M. Alvarez" Date: Wed, 9 Oct 2024 12:50:17 +0200 Subject: [PATCH] fix(cli): print paths relative to the target path. When the target path passed to the CLI is a relative one, file paths printed by the CLI are relative to the target path. Closes #212 --- cli/src/commands/scan.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/cli/src/commands/scan.rs b/cli/src/commands/scan.rs index da51050a..e54c13c5 100644 --- a/cli/src/commands/scan.rs +++ b/cli/src/commands/scan.rs @@ -243,6 +243,8 @@ pub fn exec_scan(args: &ArgMatches) -> anyhow::Result<()> { walk::ParWalker::path(target_path) }; + let canonical_target_path = target_path.canonicalize()?; + if let Some(num_threads) = num_threads { w.num_threads(*num_threads); } @@ -305,11 +307,20 @@ pub fn exec_scan(args: &ArgMatches) -> anyhow::Result<()> { let now = Instant::now(); + // When the target path passed in the command line is an absolute + // path, all file paths are printed as absolute paths, if not, they + // are printed as paths relative to the target path. + let printable_path = if target_path.is_absolute() { + file_path.as_path() + } else { + file_path.strip_prefix(&canonical_target_path)? + }; + state .files_in_progress .lock() .unwrap() - .push((file_path.clone(), now)); + .push((printable_path.to_path_buf(), now)); let scan_options = all_metadata.iter().fold( ScanOptions::new(), @@ -326,11 +337,15 @@ pub fn exec_scan(args: &ArgMatches) -> anyhow::Result<()> { .files_in_progress .lock() .unwrap() - .retain(|(p, _)| !file_path.eq(p)); + .retain(|(p, _)| !printable_path.eq(p)); let scan_results = scan_results?; - let matched_count = - process_scan_results(args, &file_path, &scan_results, output); + let matched_count = process_scan_results( + args, + printable_path, + &scan_results, + output, + ); state.num_scanned_files.fetch_add(1, Ordering::Relaxed); if matched_count > 0 {