Skip to main content

oxide_cli/utils/
cleanup.rs

1use std::{fs, path::PathBuf};
2
3use anyhow::Result;
4
5use crate::CleanupState;
6
7pub fn setup_ctrlc_handler(
8  cleanup_state: CleanupState,
9  template_path_clone: PathBuf,
10) -> Result<()> {
11  ctrlc::set_handler(move || {
12    println!("\n⚠ Interrupted! Cleaning up...");
13
14    let cleanup_path = {
15      let guard = cleanup_state.lock().unwrap_or_else(|e| e.into_inner());
16
17      guard.clone()
18    };
19    if let Some(path) = cleanup_path
20      && path.exists()
21    {
22      if let Err(e) = fs::remove_dir_all(&path) {
23        println!("Failed to remove: {}", e);
24      }
25
26      let mut current = path.parent();
27      while let Some(parent) = current {
28        if parent == template_path_clone {
29          break;
30        }
31        if fs::remove_dir(parent).is_err() {
32          break;
33        }
34        current = parent.parent();
35      }
36      println!("✓ Removed incomplete template");
37    }
38    std::process::exit(1);
39  })?;
40
41  Ok(())
42}