-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit-id:94d1add0
- Loading branch information
Showing
4 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use camino::Utf8PathBuf; | ||
use clap::Parser; | ||
|
||
/// Arguments accepted by the `clean` subcommand. | ||
#[derive(Parser, Debug)] | ||
pub struct CleanArgs { | ||
/// Root directory to search for files to clean. | ||
/// From this directory, all subdirectories are searched recursively. | ||
#[arg(short, long, default_value = ".")] | ||
pub root_dir: Utf8PathBuf, | ||
|
||
/// File name of a file to clean. It should also include the extension. | ||
#[arg(short, long, default_value = "coverage.lcov")] | ||
pub files_to_delete: Utf8PathBuf, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use anyhow::{Context, Result}; | ||
use cairo_coverage_args::clean::CleanArgs; | ||
use std::fs; | ||
use walkdir::WalkDir; | ||
|
||
/// Run the `cairo-coverage clean` command with [`CleanArgs`]. | ||
/// This command deletes all files with the name specified in `files_to_delete` in the `root_dir` recursively. | ||
pub fn run( | ||
CleanArgs { | ||
root_dir, | ||
files_to_delete, | ||
}: CleanArgs, | ||
) -> Result<()> { | ||
let target_file_name = files_to_delete | ||
.file_name() | ||
.context("Failed to obtain the file name from `files_to_delete`.")?; | ||
|
||
WalkDir::new(root_dir) | ||
.into_iter() | ||
.filter_map(Result::ok) | ||
.filter(|e| e.file_type().is_file()) | ||
.try_for_each(|entry| -> Result<()> { | ||
let path = entry.path(); | ||
|
||
if let Some(file_name) = path.file_name() { | ||
if file_name == target_file_name { | ||
println!("Deleting file: {}", path.display()); | ||
fs::remove_file(path) | ||
.with_context(|| format!("Failed to delete file: {}", path.display()))?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
})?; | ||
|
||
println!("Cleanup complete."); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters