Skip to main content

orch/
cmd_cache.rs

1use std::collections::HashMap;
2use std::sync::LazyLock;
3use std::sync::Mutex;
4use std::sync::PoisonError;
5
6static CMD_CACHE: LazyLock<Mutex<HashMap<String, bool>>> =
7    LazyLock::new(|| Mutex::new(HashMap::new()));
8
9/// Check if a command exists in PATH, caching the result to avoid repeated
10/// filesystem scans (which can be expensive when called frequently).
11pub fn command_exists(cmd: &str) -> bool {
12    let mut cache = CMD_CACHE.lock().unwrap_or_else(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(PoisonError::into_inner)
26        .clear();
27}