git_adr/cli/
rm.rs

1//! Remove an ADR.
2
3use anyhow::Result;
4use clap::Args as ClapArgs;
5use colored::Colorize;
6use std::io::{self, Write};
7
8use crate::core::{ConfigManager, Git, NotesManager};
9
10/// Arguments for the rm command.
11#[derive(ClapArgs, Debug)]
12pub struct Args {
13    /// ADR ID to remove.
14    pub adr_id: String,
15
16    /// Skip confirmation prompt.
17    #[arg(long, short)]
18    pub force: bool,
19}
20
21/// Run the rm command.
22///
23/// # Errors
24///
25/// Returns an error if removal fails.
26pub fn run(args: Args) -> Result<()> {
27    let git = Git::new();
28    git.check_repository()?;
29
30    let config = ConfigManager::new(git.clone()).load()?;
31    let notes = NotesManager::new(git, config);
32
33    // Find the ADR to confirm it exists
34    let adrs = notes.list()?;
35    let adr = adrs
36        .into_iter()
37        .find(|a| a.id == args.adr_id || a.id.contains(&args.adr_id))
38        .ok_or_else(|| anyhow::anyhow!("ADR not found: {}", args.adr_id))?;
39
40    eprintln!("{} ADR: {} - {}", "→".blue(), adr.id, adr.frontmatter.title);
41    eprintln!("  Status: {}", adr.frontmatter.status);
42
43    // Confirm deletion unless --force
44    if !args.force {
45        eprint!("Are you sure you want to remove this ADR? [y/N] ");
46        io::stderr().flush()?;
47
48        let mut input = String::new();
49        io::stdin().read_line(&mut input)?;
50
51        if !input.trim().eq_ignore_ascii_case("y") {
52            eprintln!("{} Aborted", "!".yellow());
53            return Ok(());
54        }
55    }
56
57    // Delete the ADR
58    notes.delete(&adr.id)?;
59
60    eprintln!("{} ADR removed: {}", "✓".green(), adr.id);
61
62    Ok(())
63}