Skip to main content

robinpath_modules/modules/
os_mod.rs

1use robinpath::{RobinPath, Value};
2
3pub fn register(rp: &mut RobinPath) {
4    rp.register_builtin("os.hostname", |_args, _| {
5        Ok(Value::String(hostname()))
6    });
7
8    rp.register_builtin("os.platform", |_args, _| {
9        Ok(Value::String(std::env::consts::OS.to_string()))
10    });
11
12    rp.register_builtin("os.arch", |_args, _| {
13        Ok(Value::String(std::env::consts::ARCH.to_string()))
14    });
15
16    rp.register_builtin("os.cpuCount", |_args, _| {
17        Ok(Value::Number(num_cpus() as f64))
18    });
19
20    rp.register_builtin("os.homeDir", |_args, _| {
21        match std::env::var("HOME").or_else(|_| std::env::var("USERPROFILE")) {
22            Ok(home) => Ok(Value::String(home)),
23            Err(_) => Ok(Value::Null),
24        }
25    });
26
27    rp.register_builtin("os.tempDir", |_args, _| {
28        Ok(Value::String(
29            std::env::temp_dir().to_string_lossy().to_string(),
30        ))
31    });
32
33    rp.register_builtin("os.userInfo", |_args, _| {
34        let mut obj = indexmap::IndexMap::new();
35        let username = std::env::var("USER")
36            .or_else(|_| std::env::var("USERNAME"))
37            .unwrap_or_default();
38        let homedir = std::env::var("HOME")
39            .or_else(|_| std::env::var("USERPROFILE"))
40            .unwrap_or_default();
41        let shell = std::env::var("SHELL").unwrap_or_default();
42        obj.insert("username".to_string(), Value::String(username));
43        obj.insert("homedir".to_string(), Value::String(homedir));
44        obj.insert("shell".to_string(), Value::String(shell));
45        Ok(Value::Object(obj))
46    });
47
48    rp.register_builtin("os.type", |_args, _| {
49        let os_type = if cfg!(target_os = "windows") {
50            "Windows_NT"
51        } else if cfg!(target_os = "macos") {
52            "Darwin"
53        } else {
54            "Linux"
55        };
56        Ok(Value::String(os_type.to_string()))
57    });
58
59    rp.register_builtin("os.eol", |_args, _| {
60        if cfg!(target_os = "windows") {
61            Ok(Value::String("\r\n".to_string()))
62        } else {
63            Ok(Value::String("\n".to_string()))
64        }
65    });
66
67    rp.register_builtin("os.cwd", |_args, _| {
68        match std::env::current_dir() {
69            Ok(p) => Ok(Value::String(p.to_string_lossy().to_string())),
70            Err(e) => Err(format!("os.cwd error: {}", e)),
71        }
72    });
73
74    rp.register_builtin("os.env", |args, _| {
75        let name = args.first().map(|v| v.to_display_string());
76        match name {
77            Some(n) => Ok(Value::String(std::env::var(&n).unwrap_or_default())),
78            None => {
79                let mut obj = indexmap::IndexMap::new();
80                for (k, v) in std::env::vars() {
81                    obj.insert(k, Value::String(v));
82                }
83                Ok(Value::Object(obj))
84            }
85        }
86    });
87}
88
89fn hostname() -> String {
90    #[cfg(target_os = "windows")]
91    {
92        std::env::var("COMPUTERNAME").unwrap_or_else(|_| "unknown".to_string())
93    }
94    #[cfg(not(target_os = "windows"))]
95    {
96        std::fs::read_to_string("/etc/hostname")
97            .map(|s| s.trim().to_string())
98            .unwrap_or_else(|_| "unknown".to_string())
99    }
100}
101
102fn num_cpus() -> usize {
103    std::thread::available_parallelism()
104        .map(|n| n.get())
105        .unwrap_or(1)
106}