forked from Dimillian/CodexMonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaemon_binary.rs
More file actions
100 lines (86 loc) · 2.89 KB
/
daemon_binary.rs
File metadata and controls
100 lines (86 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use std::path::PathBuf;
pub(crate) fn daemon_binary_candidates() -> &'static [&'static str] {
if cfg!(windows) {
&["codex_monitor_daemon.exe", "codex-monitor-daemon.exe"]
} else {
&["codex_monitor_daemon", "codex-monitor-daemon"]
}
}
fn daemon_search_dirs(executable_dir: &std::path::Path) -> Vec<PathBuf> {
let mut dirs = Vec::new();
let mut push_unique = |path: PathBuf| {
if !dirs.iter().any(|entry| entry == &path) {
dirs.push(path);
}
};
push_unique(executable_dir.to_path_buf());
#[cfg(target_os = "macos")]
{
if let Some(contents_dir) = executable_dir.parent() {
push_unique(contents_dir.join("Resources"));
}
push_unique(PathBuf::from("/opt/homebrew/bin"));
push_unique(PathBuf::from("/usr/local/bin"));
}
#[cfg(target_os = "linux")]
{
push_unique(PathBuf::from("/usr/local/bin"));
push_unique(PathBuf::from("/usr/bin"));
push_unique(PathBuf::from("/usr/sbin"));
}
dirs
}
pub(crate) fn resolve_daemon_binary_path() -> Result<PathBuf, String> {
let mut attempted_paths: Vec<PathBuf> = Vec::new();
let current_exe = std::env::current_exe().map_err(|err| err.to_string())?;
let parent = current_exe
.parent()
.ok_or_else(|| "Unable to resolve executable directory".to_string())?;
let candidate_names = daemon_binary_candidates();
if let Ok(explicit_raw) = std::env::var("CODEX_MONITOR_DAEMON_PATH") {
let explicit = explicit_raw.trim();
if !explicit.is_empty() {
let explicit_path = PathBuf::from(explicit);
if explicit_path.is_file() {
return Ok(explicit_path);
}
if explicit_path.is_dir() {
for name in candidate_names {
let candidate = explicit_path.join(name);
if candidate.is_file() {
return Ok(candidate);
}
attempted_paths.push(candidate);
}
} else {
attempted_paths.push(explicit_path);
}
}
}
for search_dir in daemon_search_dirs(parent) {
for name in candidate_names {
let candidate = search_dir.join(name);
if candidate.is_file() {
return Ok(candidate);
}
attempted_paths.push(candidate);
}
}
let attempted = attempted_paths
.iter()
.map(|path| path.display().to_string())
.collect::<Vec<_>>()
.join(", ");
Err(format!(
"Unable to locate daemon binary (tried: {})",
attempted
))
}
#[cfg(test)]
mod tests {
use super::daemon_binary_candidates;
#[test]
fn daemon_binary_candidates_prioritize_underscored_name() {
assert!(daemon_binary_candidates()[0].starts_with("codex_monitor_daemon"));
}
}