-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.rs
89 lines (74 loc) · 2.47 KB
/
utils.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use std::{io::Read, path::Path, process::Command};
pub fn get_os_string() -> String {
#[cfg(target_os = "linux")]
{
if Path::new("/.dockerenv").exists() {
return "Docker Container".to_string();
}
if let Ok(mut file) = std::fs::File::open("/proc/sys/kernel/osrelease") {
let mut buf = String::new();
if file.read_to_string(&mut buf).is_ok()
// Depending on the WSl version, "microsoft" may start with uppercase or lowercase
&& (buf.contains("microsoft") || buf.contains("Microsoft"))
{
return "Windows Subsystem for Linux".to_string();
}
}
let output = Command::new("sh")
.arg("-c")
.arg("grep ID /etc/os-release | awk -F= \'$1==\"ID\" {print}\'")
.output()
.expect("failed to execute process");
let os_string = String::from_utf8_lossy(&output.stdout);
let (_, distro) = os_string
.split_once('\n')
.expect("Couldn't split OS string")
.0
.split_once('=')
.expect("Couldn't split OS string");
distro.to_string()
}
#[cfg(target_os = "windows")]
{
let output = Command::new("wmic")
.arg("os")
.arg("get")
.arg("Caption")
.output()
.expect("failed to execute process");
let cow_string = String::from_utf8_lossy(&output.stdout);
let string = cow_string.replace("Caption", "").replace("Microsoft", "");
string.trim().to_owned()
}
#[cfg(target_os = "freebsd")]
{
"FreeBSD".to_string()
}
}
pub fn get_version_string() -> String {
#[cfg(target_os = "linux")]
{
let output = Command::new("sh")
.arg("-c")
.arg("git rev-parse --short main")
.output()
.expect("failed to execute process");
let os_string = String::from_utf8_lossy(&output.stdout);
os_string.trim().to_owned()
}
#[cfg(target_os = "windows")]
{
let output = Command::new("git")
.arg("rev-parse")
.arg("--short")
.arg("main")
.output()
.expect("failed to execute process");
let os_string = String::from_utf8_lossy(&output.stdout);
os_string.trim().to_owned()
}
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
{
"Unknown".to_string()
}
}