Skip to main content

ralph_cli/
lib.rs

1use anyhow::{Context, Result};
2use std::fs;
3use std::path::Path;
4
5mod colors {
6    pub const DIM: &str = "\x1b[2m";
7    pub const RESET: &str = "\x1b[0m";
8    pub const CYAN: &str = "\x1b[36m";
9    pub const GREEN: &str = "\x1b[32m";
10}
11
12/// Clean diagnostic logs from .ralph/diagnostics directory
13pub fn clean_diagnostics(workspace_root: &Path, use_colors: bool, dry_run: bool) -> Result<()> {
14    let diagnostics_dir = workspace_root.join(".ralph/diagnostics");
15
16    // Check if directory exists
17    if !diagnostics_dir.exists() {
18        if use_colors {
19            println!(
20                "{}Nothing to clean:{} Directory '{}' does not exist",
21                colors::DIM,
22                colors::RESET,
23                diagnostics_dir.display()
24            );
25        } else {
26            println!(
27                "Nothing to clean: Directory '{}' does not exist",
28                diagnostics_dir.display()
29            );
30        }
31        return Ok(());
32    }
33
34    // Dry run mode - list what would be deleted
35    if dry_run {
36        if use_colors {
37            println!(
38                "{}Dry run mode:{} Would delete directory and all contents:",
39                colors::CYAN,
40                colors::RESET
41            );
42        } else {
43            println!("Dry run mode: Would delete directory and all contents:");
44        }
45        println!("  {}", diagnostics_dir.display());
46
47        // List directory contents (simplified for lib - just show count)
48        if let Ok(entries) = fs::read_dir(&diagnostics_dir) {
49            let count = entries.count();
50            println!("  ({} session directories)", count);
51        }
52
53        return Ok(());
54    }
55
56    // Perform actual deletion
57    fs::remove_dir_all(&diagnostics_dir).with_context(|| {
58        format!(
59            "Failed to delete directory '{}'. Check permissions and try again.",
60            diagnostics_dir.display()
61        )
62    })?;
63
64    // Success message
65    if use_colors {
66        println!(
67            "{}✓{} Cleaned: Deleted '{}' and all contents",
68            colors::GREEN,
69            colors::RESET,
70            diagnostics_dir.display()
71        );
72    } else {
73        println!(
74            "Cleaned: Deleted '{}' and all contents",
75            diagnostics_dir.display()
76        );
77    }
78
79    Ok(())
80}