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
use std::process::Command;
use std::fs;
use std::convert::AsRef;
use std::path::Path;
mod lsb_release;
mod windows_ver;
#[derive(Debug)]
#[derive(PartialEq)]
pub enum OSType {
Unknown,
Redhat,
OSX,
Ubuntu,
Debian,
Windows
}
fn file_exists<P: AsRef<Path>>(path: P) -> bool {
let metadata = fs::metadata(path);
match metadata {
Ok(md) => md.is_dir() || md.is_file(),
Err(_) => false
}
}
fn is_windows() -> bool {
if cfg!(target_os="windows") {
return true;
} else {
return false;
}
}
fn is_os_x() -> bool {
match Command::new("sw_vers").output() {
Ok(output) => output.status.success(),
Err(_) => false
}
}
fn lsb_release() -> OSType {
match lsb_release::retrieve() {
Some(release) => {
if release.distro == Some("Ubuntu".to_string()) {
OSType::Ubuntu
}
else if release.distro == Some("Debian".to_string()) {
OSType::Debian
}
else {
OSType::Unknown
}
},
None => OSType::Unknown
}
}
pub fn current_platform() -> OSType {
if is_os_x() {
OSType::OSX
}
else if is_windows() {
OSType::Windows
}
else if lsb_release::is_available() {
lsb_release()
}
else if file_exists("/etc/redhat-release") || file_exists("/etc/centos-release") {
OSType::Redhat
}
else {
OSType::Unknown
}
}