yolk/script/
sysinfo.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
use rhai::{CustomType, TypeBuilder};
use std::path::PathBuf;

#[derive(Debug, Clone, CustomType)]
pub struct SystemInfo {
    #[rhai_type(readonly)]
    hostname: String,
    #[rhai_type(readonly)]
    username: String,
    #[rhai_type(readonly)]
    distro: String,
    #[rhai_type(readonly)]
    device_name: String,
    #[rhai_type(readonly)]
    arch: String,
    #[rhai_type(readonly)]
    desktop_env: String,
    #[rhai_type(readonly)]
    platform: String,
    #[rhai_type(readonly)]
    paths: SystemInfoPaths,
}

#[derive(Debug, Clone, CustomType)]
pub struct SystemInfoPaths {
    #[rhai_type(readonly)]
    cache_dir: PathBuf,
    #[rhai_type(readonly)]
    config_dir: PathBuf,
    #[rhai_type(readonly)]
    home_dir: PathBuf,
}

impl SystemInfo {
    pub fn generate() -> Self {
        #[cfg(test)]
        return Self::canonical();
        #[cfg(not(test))]
        Self {
            hostname: whoami::fallible::hostname().unwrap_or_else(|_| "unknown".to_string()),
            username: whoami::username(),
            distro: whoami::distro().to_string(),
            device_name: whoami::fallible::devicename().unwrap_or_else(|_| "unknown".to_string()),
            arch: whoami::arch().to_string(),
            desktop_env: whoami::desktop_env().to_string(),
            platform: whoami::platform().to_string(),
            paths: SystemInfoPaths {
                cache_dir: dirs::cache_dir().unwrap_or_else(|| "unknown".into()),
                config_dir: dirs::config_dir().unwrap_or_else(|| "unknown".into()),
                home_dir: dirs::home_dir().unwrap_or_else(|| "unknown".into()),
            },
        }
    }

    pub fn canonical() -> Self {
        Self {
            hostname: "canonical-hostname".to_string(),
            username: "canonical-username".to_string(),
            paths: SystemInfoPaths {
                cache_dir: (PathBuf::from("/canonical/cache")),
                config_dir: (PathBuf::from("/canonical/config")),
                home_dir: (PathBuf::from("/canonical/home")),
            },
            distro: "distro".to_string(),
            device_name: "devicename".to_string(),
            arch: "x86_64".to_string(),
            desktop_env: "gnome".to_string(),
            platform: "linux".to_string(),
        }
    }
}