-
Notifications
You must be signed in to change notification settings - Fork 1
Add part of Safe clean #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -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); | ||
| } |
This file contains hidden or 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 hidden or 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,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 | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1,9 @@ | ||
| //! Cleaner module. | ||
| mod executor; | ||
| mod plan; | ||
|
|
||
| #[cfg(test)] | ||
| mod tests; | ||
|
|
||
| pub use executor::execute_cleanup; | ||
| pub use plan::create_cleanup_plan; |
This file contains hidden or 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,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 | ||
| } |
This file contains hidden or 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,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); | ||
| } |
This file contains hidden or 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,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, | ||
| } |
This file contains hidden or 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 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() | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add help text for
--dry-run.This flag is central to the safety story, but without a field doc comment it shows up in
--helpwith no explanation.Suggested fix
#[derive(Args)] pub struct CleanArgs { + /// Preview cleanup operations without deleting files. #[arg(long)] pub dry_run: bool, }📝 Committable suggestion
🤖 Prompt for AI Agents