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
use regex::Regex;
use std::process::Command;
pub struct LsbRelease {
pub distro: Option<String>,
pub version: Option<String>
}
pub fn retrieve() -> Option<LsbRelease> {
let output = match Command::new("lsb_release").arg("-a").output() {
Ok(o) => o,
Err(_) =>return None
};
let stdout = String::from_utf8_lossy(&output.stdout);
Some(parse(stdout.to_string()))
}
pub fn is_available() -> bool {
match Command::new("lsb_release").output() {
Ok(_) => true,
Err(_) => false
}
}
pub fn parse(file: String) -> LsbRelease {
let distrib_regex = Regex::new(r"Distributor ID:\s*(\w+)").unwrap();
let distrib_release_regex = Regex::new(r"Release:\s*([\w\.]+)").unwrap();
let distro = match distrib_regex.captures_iter(&file).next() {
Some(m) => {
match m.get(1) {
Some(distro) => {
Some(distro.as_str().to_owned())
},
None => None
}
},
None => None
};
let version = match distrib_release_regex.captures_iter(&file).next() {
Some(m) => {
match m.get(1) {
Some(version) => Some(version.as_str().to_owned()),
None => None
}
},
None => None
};
LsbRelease {
distro: distro,
version: version
}
}