Skip to main content

bn/commands/
locks.rs

1//! `bn locks` — View and manage file locks.
2
3use std::path::Path;
4
5use anyhow::Result;
6
7use crate::locks;
8
9/// List all active file locks.
10pub fn cmd_locks(beans_dir: &Path) -> Result<()> {
11    let active = locks::list_locks(beans_dir)?;
12
13    if active.is_empty() {
14        eprintln!("No active file locks.");
15        return Ok(());
16    }
17
18    eprintln!("{} active lock(s):\n", active.len());
19    for lock in &active {
20        let age = chrono::Utc::now().timestamp() - lock.info.locked_at;
21        let age_str = if age < 60 {
22            format!("{}s", age)
23        } else if age < 3600 {
24            format!("{}m", age / 60)
25        } else {
26            format!("{}h", age / 3600)
27        };
28
29        eprintln!(
30            "  {} — bean {} (pid {}, {})",
31            lock.info.file_path, lock.info.bean_id, lock.info.pid, age_str
32        );
33    }
34
35    Ok(())
36}
37
38/// Force-clear all file locks.
39pub fn cmd_locks_clear(beans_dir: &Path) -> Result<()> {
40    let cleared = locks::clear_all(beans_dir)?;
41    if cleared == 0 {
42        eprintln!("No locks to clear.");
43    } else {
44        eprintln!("Cleared {} lock(s).", cleared);
45    }
46    Ok(())
47}