//! Where the backend programs are. //! //! The service installs each into `bin\\` (the component manager); a program //! found there wins. Elsewhere (development on Linux) the one on PATH is taken. use std::path::{Path, PathBuf}; pub struct Components { bin: PathBuf, } impl Components { pub fn new(bin: PathBuf) -> Self { Self { bin } } /// The program `name` (without `.exe`) of `component`. pub fn find(&self, component: &str, name: &str) -> Option { let file = if cfg!(windows) { format!("{name}.exe") } else { name.to_owned() }; let installed = self.bin.join(component).join(&file); if installed.is_file() { return Some(installed); } let path = std::env::var_os("PATH")?; std::env::split_paths(&path).map(|dir| dir.join(&file)).find(|p| p.is_file()) } pub fn sing_box(&self) -> Option { self.find("sing-box", "sing-box") } pub fn xray(&self) -> Option { self.find("xray", "xray") } pub fn bin_dir(&self) -> &Path { &self.bin } }