Skip to content

Commit

Permalink
Add clean subcommand
Browse files Browse the repository at this point in the history
commit-id:94d1add0
  • Loading branch information
ksew1 committed Dec 18, 2024
1 parent 9b64636 commit 21b2c32
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
15 changes: 15 additions & 0 deletions crates/cairo-coverage-args/src/clean.rs
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,
}
5 changes: 5 additions & 0 deletions crates/cairo-coverage-args/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::clean::CleanArgs;
use crate::run::RunArgs;
use clap::{Parser, Subcommand};

pub mod clean;
pub mod run;

#[derive(Parser, Debug)]
Expand All @@ -18,6 +20,9 @@ pub struct CairoCoverageArgs {
/// Subcommand and its arguments.
#[derive(Subcommand, Debug)]
pub enum Command {
/// Clean up coverage files.
Clean(CleanArgs),

/// Run `cairo-coverage` tool.
Run(RunArgs),
}
38 changes: 38 additions & 0 deletions crates/cairo-coverage/src/commands/clean.rs
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(())
}
2 changes: 2 additions & 0 deletions crates/cairo-coverage/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod clean;
mod run;

use anyhow::Result;
Expand All @@ -6,6 +7,7 @@ use cairo_coverage_args::Command;
/// Run chosen [`Command`].
pub fn run(command: Command) -> Result<()> {
match command {
Command::Clean(args) => clean::run(args),
Command::Run(args) => run::run(args),
}
}

0 comments on commit 21b2c32

Please sign in to comment.