sysmonk/resources/
system.rs

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
use crate::squire;
use serde::{Deserialize, Serialize};
use std::str;

#[derive(Deserialize, Serialize, Debug)]
pub struct OperatingSystem {
    pub name: String,
    pub architecture: String,
}

/// Function to get OS architecture.
///
/// # Returns
///
/// A string with the OS architecture.
fn unamem() -> String {
    // Get architecture using `uname -m` with fallback
    let result = squire::util::run_command("uname", &["-m"], true);
    match result {
        Ok(output) => output.to_lowercase(),
        Err(_) => {
            log::error!("Failed to execute command");
            "".to_string()
        }
    }
}

/// Function to get OS name.
///
/// # Returns
///
/// A string with the OS name.
fn unameu() -> String {
    // Get OS using `uname`
    let result = squire::util::run_command("uname", &[], true);
    match result {
        Ok(output) => output.to_uppercase(),
        Err(_) => {
            log::error!("Failed to execute command");
            std::env::consts::OS.to_uppercase()
        }
    }
}

/// Function to get OS architecture.
///
/// # Returns
///
/// A `OperatingSystem` struct with the OS name and architecture.
pub fn os_arch() -> OperatingSystem {
    let arch = match unamem() {
        arch if arch.contains("aarch64") || arch.contains("arm64") => "arm64",
        arch if arch.contains("64") => "amd64",
        arch if arch.contains("86") => "386",
        arch if arch.contains("armv5") => "armv5",
        arch if arch.contains("armv6") => "armv6",
        arch if arch.contains("armv7") => "armv7",
        _ => "",
    };
    let os = match unameu() {
        os if os.contains("DARWIN") => "darwin",
        os if os.contains("LINUX") => "linux",
        os if os.contains("FREEBSD") => "freebsd",
        os if os.contains("NETBSD") => "netbsd",
        os if os.contains("OPENBSD") => "openbsd",
        os if os.contains("WIN") || os.contains("MSYS") => "windows",
        _ => "",
    };
    OperatingSystem {
        name: os.to_string(),
        architecture: arch.to_string(),
    }
}