Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions apps/dustfril-cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use clap::{Parser, Subcommand};
use clap::{Args, Parser, Subcommand};

/// DustFril CLI
#[derive(Parser, Debug)]
#[derive(Parser)]
#[command(name = "dfr", version, about = "Rust artifact analyzer and cleaner")]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
}

/// Available commands
#[derive(Subcommand, Debug)]
#[derive(Subcommand)]
pub enum Commands {
/// Scan Rust artifacts
Scan,
Expand All @@ -18,5 +18,12 @@ pub enum Commands {
Analyze,

/// Clean artifacts
Clean,
Clean(CleanArgs),
}

#[derive(Args)]
pub struct CleanArgs {
// Preview cleanup operations without deleting files.
#[arg(long)]
pub dry_run: bool,
Comment on lines +24 to +28
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Add help text for --dry-run.

This flag is central to the safety story, but without a field doc comment it shows up in --help with no explanation.

Suggested fix
 #[derive(Args)]
 pub struct CleanArgs {
+    /// Preview cleanup operations without deleting files.
     #[arg(long)]
     pub dry_run: bool,
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#[derive(Args)]
pub struct CleanArgs {
#[arg(long)]
pub dry_run: bool,
#[derive(Args)]
pub struct CleanArgs {
/// Preview cleanup operations without deleting files.
#[arg(long)]
pub dry_run: bool,
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/dustfril-cli/src/cli.rs` around lines 24 - 27, Add a help string for the
dry_run flag on the CleanArgs struct so the CLI help shows why it exists; update
the dry_run field (CleanArgs::dry_run) to include either a field doc comment or
an #[arg(help = "...")] attribute that explains it performs a non-destructive
dry run and does not actually delete files (e.g., "Perform a dry run; show what
would be removed without deleting anything"), leaving the flag name #[arg(long)]
unchanged.

}
104 changes: 103 additions & 1 deletion apps/dustfril-cli/src/commands/clean.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,105 @@
use std::path::Path;

use dustfril_core::{
analyzer, cleaner, detector,
models::{CleanupPlan, CleanupResult},
};

// dry-run
pub fn dry_run() {
let plan = build_cleanup_plan();

if plan.candidates.is_empty() {
println!("No cleanup candidates found.");

return;
}

print_cleanup_plan(&plan);

println!("No files were deleted.");
}

use std::io::{self, Write};

fn build_cleanup_plan() -> CleanupPlan {
let scan_result = detector::scan(Path::new("."));

let analysis = analyzer::analyze(scan_result);

cleaner::create_cleanup_plan(analysis)
}

fn confirm_cleanup() -> bool {
print!("Continue? (y/N): ");

// Flush stdout to ensure the prompt is displayed before reading input
io::stdout().flush().expect("Failed to flush stdout");

let mut input = String::new();

io::stdin()
.read_line(&mut input)
.expect("Failed to read input");

matches!(input.trim(), "y" | "Y")
}

fn print_cleanup_plan(plan: &CleanupPlan) {
println!("Cleanup Preview\n");

for candidate in &plan.candidates {
println!("[{}]", candidate.artifact_type);

println!(" Path: {}", candidate.path.display());

println!(" Size: {}\n", analyzer::format_size(candidate.size_bytes));
}

println!("Total Reclaimable Space\n");

println!(
" {}\n",
analyzer::format_size(plan.reclaimable_size_bytes())
);
}

fn print_cleanup_result(result: &CleanupResult) {
println!("Cleanup completed.");

println!("Deleted: {}", result.deleted_paths.len());

println!("Failed: {}", result.failed_paths.len());

println!("Freed: {}", analyzer::format_size(result.freed_size_bytes,));

if !result.deleted_paths.is_empty() {
println!("Deleted\n");

for path in &result.deleted_paths {
println!(" {}", path.display());
}

println!();
}
}

pub fn execute() {
println!("Clean command is not implemented yet.");
let plan = build_cleanup_plan();

if plan.candidates.is_empty() {
println!("No cleanup candidates found.");
return;
}

print_cleanup_plan(&plan);

if !confirm_cleanup() {
println!("Cleanup cancelled.");
return;
}

let result = cleaner::execute_cleanup(&plan);

print_cleanup_result(&result);
}
8 changes: 6 additions & 2 deletions apps/dustfril-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ fn main() {
commands::analyze::execute();
}

Commands::Clean => {
commands::clean::execute();
Commands::Clean(args) => {
if args.dry_run {
commands::clean::dry_run();
} else {
commands::clean::execute();
}
}
}
}
27 changes: 27 additions & 0 deletions crates/dustfril-core/src/cleaner/executor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::fs;

use crate::models::{ArtifactType, CleanupPlan, CleanupResult};

pub fn execute_cleanup(plan: &CleanupPlan) -> CleanupResult {
let mut result = CleanupResult {
deleted_paths: vec![],
failed_paths: vec![],
freed_size_bytes: 0,
};

for candidate in &plan.candidates {
match candidate.artifact_type {
ArtifactType::Target | ArtifactType::CargoRegistry | ArtifactType::CargoGit => {
if fs::remove_dir_all(&candidate.path).is_ok() {
result.deleted_paths.push(candidate.path.clone());

result.freed_size_bytes += candidate.size_bytes;
} else {
result.failed_paths.push(candidate.path.clone());
}
}
}
}

result
}
8 changes: 8 additions & 0 deletions crates/dustfril-core/src/cleaner/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
//! Cleaner module.
mod executor;
mod plan;

#[cfg(test)]
mod tests;

pub use executor::execute_cleanup;
pub use plan::create_cleanup_plan;
24 changes: 24 additions & 0 deletions crates/dustfril-core/src/cleaner/plan.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::models::{AnalysisResult, CleanupCandidate, CleanupPlan, CleanupRecommendation};

pub fn create_cleanup_plan(analysis: AnalysisResult) -> CleanupPlan {
let mut plan = CleanupPlan::default();

for artifact in analysis.artifacts {
if artifact.recommendation == CleanupRecommendation::SafeToClean {
// Flatten the analysis into a cleanup candidate
plan.candidates.push(CleanupCandidate {
path: artifact.artifact.path.clone(),

artifact_type: artifact.artifact.artifact_type.clone(),

size_bytes: artifact.size_bytes,

age_days: artifact.age_days,

recommendation: artifact.recommendation,
});
}
}

plan
}
143 changes: 143 additions & 0 deletions crates/dustfril-core/src/cleaner/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
use std::fs;
use std::path::PathBuf;
use tempfile::TempDir;

use crate::{
cleaner::{create_cleanup_plan, execute_cleanup},
models::*,
};

#[test]
fn create_empty_cleanup_plan() {
let plan = create_cleanup_plan(AnalysisResult::default());

assert!(plan.candidates.is_empty());

assert_eq!(plan.reclaimable_size_bytes(), 0);
}

#[test]
fn safe_to_clean_becomes_candidate() {
let artifact = ArtifactAnalysis {
artifact: ArtifactLocation {
path: PathBuf::from("target"),

artifact_type: ArtifactType::Target,
},

size_bytes: 100,

last_modified: None,

age_days: Some(200),

recommendation: CleanupRecommendation::SafeToClean,
};

let analysis = AnalysisResult {
artifacts: vec![artifact],

total_size_bytes: 100,
};

let plan = create_cleanup_plan(analysis);

assert_eq!(plan.candidates.len(), 1,);

assert_eq!(plan.reclaimable_size_bytes(), 100,);
}

#[test]
fn keep_is_not_candidate() {
let artifact = ArtifactAnalysis {
artifact: ArtifactLocation {
path: PathBuf::from("target"),

artifact_type: ArtifactType::Target,
},

size_bytes: 100,

last_modified: None,

age_days: Some(5),

recommendation: CleanupRecommendation::Keep,
};

let analysis = AnalysisResult {
artifacts: vec![artifact],

total_size_bytes: 100,
};

let plan = create_cleanup_plan(analysis);

assert!(plan.candidates.is_empty());
}

#[test]
fn execute_cleanup_removes_target_directory() {
let temp_dir = TempDir::new().unwrap();

let target_dir = temp_dir.path().join("target");

fs::create_dir_all(&target_dir).unwrap();

fs::write(target_dir.join("test.bin"), b"hello").unwrap();

assert!(target_dir.exists());

let candidate = CleanupCandidate {
path: target_dir.clone(),

artifact_type: ArtifactType::Target,

size_bytes: 5,

age_days: Some(100),

recommendation: CleanupRecommendation::SafeToClean,
};

let plan = CleanupPlan {
candidates: vec![candidate],
};

let result = execute_cleanup(&plan);

let size_bytes = CleanupPlan::reclaimable_size_bytes(&plan);

assert!(!target_dir.exists());
assert_eq!(result.deleted_paths.len(), 1);
assert_eq!(result.failed_paths.len(), 0);
assert_eq!(size_bytes, 5);
}

#[test]
fn cleanup_reports_failed_path() {
let temp_dir = TempDir::new().unwrap();
let missing = temp_dir.path().join("missing");

let candidate = CleanupCandidate {
path: missing,
artifact_type: ArtifactType::Target,
size_bytes: 100,
age_days: None,
recommendation: CleanupRecommendation::SafeToClean,
};

let plan = CleanupPlan {
candidates: vec![candidate],
};

let result = execute_cleanup(&plan);

assert_eq!(result.deleted_paths.len(), 0);

assert_eq!(result.failed_paths.len(), 1);

assert_eq!(result.freed_size_bytes, 0);

assert_eq!(plan.reclaimable_size_bytes(), 100);
}
16 changes: 16 additions & 0 deletions crates/dustfril-core/src/models/cleanup_candidate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::path::PathBuf;

use crate::models::{ArtifactType, CleanupRecommendation};

#[derive(Debug)]
pub struct CleanupCandidate {
pub path: PathBuf,

pub artifact_type: ArtifactType,

pub size_bytes: u64,

pub age_days: Option<u64>,

pub recommendation: CleanupRecommendation,
}
15 changes: 15 additions & 0 deletions crates/dustfril-core/src/models/cleanup_plan.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::models::CleanupCandidate;

#[derive(Debug, Default)]
pub struct CleanupPlan {
pub candidates: Vec<CleanupCandidate>,
}

impl CleanupPlan {
pub fn reclaimable_size_bytes(&self) -> u64 {
self.candidates
.iter()
.map(|candidate| candidate.size_bytes)
.sum()
}
}
Loading