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}
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85
86    #[test]
87    fn clean_diagnostics_no_dir_is_ok() {
88        let temp_dir = tempfile::tempdir().expect("temp dir");
89        let result = clean_diagnostics(temp_dir.path(), false, false);
90        assert!(result.is_ok());
91        assert!(!temp_dir.path().join(".ralph/diagnostics").exists());
92    }
93
94    #[test]
95    fn clean_diagnostics_dry_run_keeps_dir() {
96        let temp_dir = tempfile::tempdir().expect("temp dir");
97        let diagnostics_dir = temp_dir.path().join(".ralph/diagnostics");
98        std::fs::create_dir_all(&diagnostics_dir).expect("create diagnostics");
99        std::fs::write(diagnostics_dir.join("session.log"), "data").expect("write log");
100
101        clean_diagnostics(temp_dir.path(), false, true).expect("dry run");
102        assert!(diagnostics_dir.exists());
103    }
104
105    #[test]
106    fn clean_diagnostics_deletes_dir() {
107        let temp_dir = tempfile::tempdir().expect("temp dir");
108        let diagnostics_dir = temp_dir.path().join(".ralph/diagnostics");
109        std::fs::create_dir_all(&diagnostics_dir).expect("create diagnostics");
110        std::fs::write(diagnostics_dir.join("session.log"), "data").expect("write log");
111
112        clean_diagnostics(temp_dir.path(), false, false).expect("clean diagnostics");
113        assert!(!diagnostics_dir.exists());
114    }
115}