1use crate::{is_linux, InfoTrait};
2use std::error::Error;
3use std::process::Command;
4
5#[derive(Default, Clone, Debug)]
6pub struct HostInfo {
7 pub distro: Option<String>,
8 pub os: Option<String>,
9 pub architecture: Option<String>,
10 pub vendor: Option<String>,
11 pub model: Option<String>,
12 pub desktop_env: Option<String>,
13 pub session: Option<String>,
14 pub win_manager: Option<String>,
15}
16
17impl HostInfo {
18 pub fn get_distro() -> Result<String, Box<dyn Error>> {
19 let command = Command::new("hostname").output()?;
20
21 Ok(std::str::from_utf8(&command.stdout)?.trim().to_string())
22 }
23}
24
25impl InfoTrait for HostInfo {
26 fn get() -> Result<Self, Box<dyn Error>> {
27 let _ = is_linux()?;
28 let mut host = Self::default();
29
30 let command = Command::new("hostnamectl").output()?;
31
32 std::str::from_utf8(&command.stdout)?
33 .trim()
34 .split('\n')
35 .for_each(|i| {
36 let inf = i.split(':').collect::<Vec<&str>>();
37 if inf.len() > 1 {
38 let key = inf[0].trim();
39 let val = inf[1]
40 .replace("kB", "")
41 .replace("\n", "")
42 .trim()
43 .to_string();
44
45 match key {
46 "Operating System" => {
47 host.os = Some(val);
48 }
49 "Architecture" => {
50 host.architecture = Some(val);
51 }
52 "Hardware Vendor" => {
53 host.vendor = Some(val);
54 }
55 "Hardware Model" => host.model = Some(val),
56 &_ => (),
57 }
58 }
59 });
60
61 host.distro = Some(Self::get_distro()?);
62 host.desktop_env = Some(std::env::var("DESKTOP_SESSION")?.trim().to_string());
63 host.win_manager = Some(std::env::var("USER")?.trim().to_string());
64 host.session = Some(std::env::var("XDG_SESSION_TYPE")?.trim().to_string());
65 Ok(host)
66 }
67}