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
33 changes: 17 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion apps/dustfril-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ path = "src/main.rs"

[dependencies]
dustfril-core = { path = "../../crates/dustfril-core" }
chrono = "0.4"
clap = { version = "4", features = ["derive"] }

clap = { version = "4", features = ["derive"] }
22 changes: 17 additions & 5 deletions apps/dustfril-cli/src/commands/analyze.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use dustfril_core::api;
use dustfril_core::models::{AnalysisResult, CleanupRecommendation};
use dustfril_core::{api, format};

use crate::cli::PathArgs;
use crate::format;
use crate::shared::path::{resolve_path, validate_path};

fn print_summary(analysis: &AnalysisResult) {
Expand Down Expand Up @@ -59,12 +60,25 @@ pub fn execute(args: PathArgs) {
let path = resolve_path(&args.path);

if !validate_path(&path) {
eprintln!("Invalid path");
return;
}

let scan_result = api::scan(&path, args.global);
let scan_result = match api::scan(&path, args.global) {
Ok(res) => res,
Err(e) => {
eprintln!("Scan failed: {}", e);
return;
}
};

let analysis_result = api::analyze(scan_result);
let analysis_result = match api::analyze(scan_result) {
Ok(res) => res,
Err(e) => {
eprintln!("Analysis failed: {}", e);
return;
}
};

if analysis_result.artifacts.is_empty() {
println!("No Rust artifacts found.");
Expand All @@ -75,9 +89,7 @@ pub fn execute(args: PathArgs) {

for artifact in &analysis_result.artifacts {
println!("[{}]", artifact.artifact.artifact_type);

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

println!(" Size: {}", format::format_size(artifact.size_bytes));

println!(
Expand Down
41 changes: 28 additions & 13 deletions apps/dustfril-cli/src/commands/clean.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
use dustfril_core::{
api, format,
api,
error::DustError,
models::{CleanupPlan, CleanupResult},
};

use crate::{cli::CleanArgs, shared::path::resolve_path};
use crate::{cli::CleanArgs, format, shared::path::resolve_path};

// dry-run
pub fn dry_run(args: &CleanArgs) {
let plan = build_cleanup_plan(args);
let plan = match build_cleanup_plan(args) {
Ok(plan) => plan,
Err(e) => {
eprintln!("Scan failed: {}", e);
return;
}
};

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(args: &CleanArgs) -> CleanupPlan {
fn build_cleanup_plan(args: &CleanArgs) -> Result<CleanupPlan, DustError> {
let path = resolve_path(&args.path_args.path);

let scan_result = api::scan(&path, args.path_args.global);
let scan = api::scan(&path, args.path_args.global)?;
let plan = api::clean::build_plan(scan)?;

api::clean::build_plan(scan_result)
Ok(plan)
}

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

Expand Down Expand Up @@ -80,9 +84,14 @@ fn print_cleanup_result(result: &CleanupResult) {
println!();
}
}

pub fn execute(args: &CleanArgs) {
let plan = build_cleanup_plan(args);
let plan = match build_cleanup_plan(args) {
Ok(plan) => plan,
Err(e) => {
eprintln!("Scan failed: {}", e);
return;
}
};

if plan.candidates.is_empty() {
println!("No cleanup candidates found.");
Expand All @@ -96,7 +105,13 @@ pub fn execute(args: &CleanArgs) {
return;
}

let result = api::clean::execute(&plan);
let result = match api::clean::execute(&plan) {
Ok(res) => res,
Err(e) => {
eprintln!("Cleanup failed: {}", e);
return;
}
};

print_cleanup_result(&result);
}
9 changes: 8 additions & 1 deletion apps/dustfril-cli/src/commands/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ pub fn execute(args: PathArgs) {
let path = resolve_path(&args.path);

if !validate_path(&path) {
eprintln!("Invalid path");
return;
}

let result = api::scan(&path, args.global);
let result = match api::scan(&path, args.global) {
Ok(res) => res,
Err(e) => {
eprintln!("Scan failed: {}", e);
return;
}
};

if result.artifacts.is_empty() {
println!("No Rust artifacts found.");
Expand Down
5 changes: 5 additions & 0 deletions apps/dustfril-cli/src/format/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod size_format;
pub mod time_format;

pub use size_format::*;
pub use time_format::*;
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,24 @@ pub fn format_size(bytes: u64) -> String {
format!("{:.0} B", bytes)
}
}

#[test]
fn format_size_bytes() {
assert_eq!(format_size(512), "512 B");
}

#[test]
fn format_size_kilobytes() {
assert_eq!(format_size(1024), "1.00 KB");
assert_eq!(format_size(2048), "2.00 KB");
}

#[test]
fn format_size_megabytes() {
assert_eq!(format_size(1024 * 1024), "1.00 MB");
}

#[test]
fn format_size_gigabytes() {
assert_eq!(format_size(1024 * 1024 * 1024), "1.00 GB");
}
1 change: 1 addition & 0 deletions apps/dustfril-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod cli;
mod commands;
mod format;
mod shared;

use clap::Parser;
Expand Down
4 changes: 2 additions & 2 deletions crates/dustfril-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2024"

[dependencies]
chrono = "0.4"
serde = { version = "1", features = ["derive"] }

[dev-dependencies]
tempfile = "3.0"
tempfile = "3.0"
5 changes: 3 additions & 2 deletions crates/dustfril-core/src/analyzer/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use crate::{
analyzer::{
calculate_age_days, calculate_directory_size, find_latest_modified, recommend_cleanup,
},
error::DustResult,
models::{AnalysisResult, ArtifactAnalysis, ScanResult},
};

pub fn analyze(scan_result: ScanResult) -> AnalysisResult {
pub fn analyze(scan_result: ScanResult) -> DustResult<AnalysisResult> {
let mut result = AnalysisResult::default();

for artifact in scan_result.artifacts {
Expand All @@ -29,5 +30,5 @@ pub fn analyze(scan_result: ScanResult) -> AnalysisResult {
.artifacts
.sort_by_key(|b| std::cmp::Reverse(b.size_bytes));

result
Ok(result)
}
Loading