Skip to main content

orch/
cmd_cache.rs

1use std::collections::HashMap;
2use std::sync::Mutex;
3
4static CMD_CACHE: std::sync::LazyLock<Mutex<HashMap<String, bool>>> =
5    std::sync::LazyLock::new(|| Mutex::new(HashMap::new()));
6
7/// Check if a command exists in PATH, caching the result to avoid repeated
8/// filesystem scans (which can be expensive when called frequently).
9pub fn command_exists(cmd: &str) -> bool {
10    let mut cache = CMD_CACHE
11        .lock()
12        .unwrap_or_else(std::sync::PoisonError::into_inner);
13    if let Some(v) = cache.get(cmd) {
14        return *v;
15    }
16    let ok = which::which(cmd).is_ok();
17    cache.insert(cmd.to_string(), ok);
18    ok
19}
20
21#[cfg(test)]
22pub fn reset_for_test() {
23    CMD_CACHE
24        .lock()
25        .unwrap_or_else(std::sync::PoisonError::into_inner)
26        .clear();
27}