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
use std::process::Command;
use std::fs;
use std::convert::AsRef;
use std::path::Path;
mod lsb_release;
#[derive(Debug)]
#[derive(PartialEq)]
pub enum OSType {
Unknown,
Redhat,
OSX,
Ubuntu
}
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_os_x() -> bool {
match Command::new("sw_vers").output() {
Ok(output) => output.status.success(),
Err(_) => false
}
}
fn lsb_release() -> OSType {
match lsb_release::from_file("/etc/lsb-release") {
Ok(release) => {
if release.distro == Some("Ubuntu".to_string()) {
OSType::Ubuntu
}
else {
OSType::Unknown
}
},
Err(_) => OSType::Unknown
}
}
pub fn current_platform() -> OSType {
if is_os_x() {
OSType::OSX
}
else if file_exists("/etc/lsb-release") {
lsb_release()
}
else if file_exists("/etc/redhat-release") || file_exists("/etc/centos-release") {
OSType::Redhat
}
else {
OSType::Unknown
}
}