sysmonk/resources/
system.rs

1use crate::squire;
2use serde::{Deserialize, Serialize};
3use std::str;
4
5#[derive(Deserialize, Serialize, Debug)]
6pub struct OperatingSystem {
7    pub name: String,
8    pub architecture: String,
9}
10
11/// Function to get OS architecture.
12///
13/// # Returns
14///
15/// A string with the OS architecture.
16fn unamem() -> String {
17    // Get architecture using `uname -m` with fallback
18    let result = squire::util::run_command("uname", &["-m"], true);
19    match result {
20        Ok(output) => output.to_lowercase(),
21        Err(_) => {
22            log::error!("Failed to execute command");
23            "".to_string()
24        }
25    }
26}
27
28/// Function to get OS name.
29///
30/// # Returns
31///
32/// A string with the OS name.
33fn unameu() -> String {
34    // Get OS using `uname`
35    let result = squire::util::run_command("uname", &[], true);
36    match result {
37        Ok(output) => output.to_uppercase(),
38        Err(_) => {
39            log::error!("Failed to execute command");
40            std::env::consts::OS.to_uppercase()
41        }
42    }
43}
44
45/// Function to get OS architecture.
46///
47/// # Returns
48///
49/// A `OperatingSystem` struct with the OS name and architecture.
50pub fn os_arch() -> OperatingSystem {
51    let arch = match unamem() {
52        arch if arch.contains("aarch64") || arch.contains("arm64") => "arm64",
53        arch if arch.contains("64") => "amd64",
54        arch if arch.contains("86") => "386",
55        arch if arch.contains("armv5") => "armv5",
56        arch if arch.contains("armv6") => "armv6",
57        arch if arch.contains("armv7") => "armv7",
58        _ => "",
59    };
60    let os = match unameu() {
61        os if os.contains("DARWIN") => "darwin",
62        os if os.contains("LINUX") => "linux",
63        os if os.contains("FREEBSD") => "freebsd",
64        os if os.contains("NETBSD") => "netbsd",
65        os if os.contains("OPENBSD") => "openbsd",
66        os if os.contains("WIN") || os.contains("MSYS") => "windows",
67        _ => "",
68    };
69    OperatingSystem {
70        name: os.to_string(),
71        architecture: arch.to_string(),
72    }
73}