1use std::sync::OnceLock;
11
12static HOME_DIR: OnceLock<Option<String>> = OnceLock::new();
17
18pub fn capture_home_dir() {
21 let _ = HOME_DIR.set(
22 agentmux_common::data_paths::agentmux_root()
23 .ok()
24 .map(|p| p.to_string_lossy().into_owned()),
25 );
26}
27
28pub fn agentmux_home() -> Option<std::path::PathBuf> {
30 HOME_DIR.get().cloned().flatten().map(std::path::PathBuf::from)
31}
32
33#[derive(Debug, Clone, serde::Serialize, ts_rs::TS)]
35#[ts(export, export_to = "../../../frontend/types/rpc/")]
36#[serde(rename_all = "camelCase")]
37pub struct SrvInfo {
38 pub version: String,
39 pub platform: String,
41 pub user_name: String,
42 pub host_name: String,
43 pub home_dir: Option<String>,
45}
46
47pub fn srv_info(version: &str, host_name: &str) -> SrvInfo {
49 SrvInfo {
50 version: version.to_string(),
51 platform: agentmux_common::platform_name::platform_name().to_string(),
52 user_name: whoami::username(),
53 host_name: host_name.to_string(),
54 home_dir: HOME_DIR.get().cloned().flatten(),
55 }
56}
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61
62 #[test]
63 fn srv_info_names_the_machine_and_the_version() {
64 let info = serde_json::to_value(srv_info("1.2.3", "box")).unwrap();
65 assert_eq!(info["version"], "1.2.3");
66 assert_eq!(info["hostName"], "box");
67 assert_eq!(
68 info["platform"],
69 agentmux_common::platform_name::platform_name()
70 );
71 assert_eq!(info["userName"], whoami::username());
72 assert!(info.get("homeDir").is_some());
73 }
74}