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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#[macro_use]
extern crate lazy_static;
use std::collections::BTreeMap;
use std::fs::File;
use std::io::{self, BufRead, BufReader};
use std::iter::FromIterator;
use std::path::Path;
lazy_static! {
pub static ref OS_RELEASE: io::Result<OsRelease> = OsRelease::new();
}
macro_rules! map_keys {
($item:expr, { $($pat:expr => $field:expr),+ }) => {{
$(
if $item.starts_with($pat) {
$field = parse_line($item, $pat.len()).into();
continue;
}
)+
}};
}
fn is_enclosed_with(line: &str, pattern: char) -> bool {
line.starts_with(pattern) && line.ends_with(pattern)
}
fn parse_line(line: &str, skip: usize) -> &str {
let line = line[skip..].trim();
if is_enclosed_with(line, '"') || is_enclosed_with(line, '\'') {
&line[1..line.len() - 1]
} else {
line
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct OsRelease {
pub bug_report_url: String,
pub home_url: String,
pub id_like: String,
pub id: String,
pub name: String,
pub pretty_name: String,
pub privacy_policy_url: String,
pub support_url: String,
pub version_codename: String,
pub version_id: String,
pub version: String,
pub extra: BTreeMap<String, String>
}
impl OsRelease {
pub fn new() -> io::Result<OsRelease> {
let file = BufReader::new(open("/etc/os-release")?);
Ok(OsRelease::from_iter(file.lines().flat_map(|line| line)))
}
pub fn new_from<P: AsRef<Path>>(path: P) -> io::Result<OsRelease> {
let file = BufReader::new(open(&path)?);
Ok(OsRelease::from_iter(file.lines().flat_map(|line| line)))
}
}
impl FromIterator<String> for OsRelease {
fn from_iter<I: IntoIterator<Item = String>>(lines: I) -> Self {
let mut os_release = Self::default();
for line in lines {
let line = line.trim();
map_keys!(line, {
"NAME=" => os_release.name,
"VERSION=" => os_release.version,
"ID=" => os_release.id,
"ID_LIKE=" => os_release.id_like,
"PRETTY_NAME=" => os_release.pretty_name,
"VERSION_ID=" => os_release.version_id,
"HOME_URL=" => os_release.home_url,
"SUPPORT_URL=" => os_release.support_url,
"BUG_REPORT_URL=" => os_release.bug_report_url,
"PRIVACY_POLICY_URL=" => os_release.privacy_policy_url,
"VERSION_CODENAME=" => os_release.version_codename
});
if let Some(pos) = line.find('=') {
if line.len() > pos+1 {
os_release.extra.insert(line[..pos].to_owned(), line[pos+1..].to_owned());
}
}
}
os_release
}
}
fn open<P: AsRef<Path>>(path: P) -> io::Result<File> {
File::open(&path).map_err(|why| io::Error::new(
io::ErrorKind::Other,
format!("unable to open file at {:?}: {}", path.as_ref(), why)
))
}
#[cfg(test)]
mod tests {
use super::*;
const EXAMPLE: &str = r#"NAME="Pop!_OS"
VERSION="18.04 LTS"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Pop!_OS 18.04 LTS"
VERSION_ID="18.04"
HOME_URL="https://system76.com/pop"
SUPPORT_URL="http://support.system76.com"
BUG_REPORT_URL="https://github.com/pop-os/pop/issues"
PRIVACY_POLICY_URL="https://system76.com/privacy"
VERSION_CODENAME=bionic
EXTRA_KEY=thing
ANOTHER_KEY="#;
#[test]
fn os_release() {
let os_release = OsRelease::from_iter(EXAMPLE.lines().map(|x| x.into()));
assert_eq!(
os_release,
OsRelease {
name: "Pop!_OS".into(),
version: "18.04 LTS".into(),
id: "ubuntu".into(),
id_like: "debian".into(),
pretty_name: "Pop!_OS 18.04 LTS".into(),
version_id: "18.04".into(),
home_url: "https://system76.com/pop".into(),
support_url: "http://support.system76.com".into(),
bug_report_url: "https://github.com/pop-os/pop/issues".into(),
privacy_policy_url: "https://system76.com/privacy".into(),
version_codename: "bionic".into(),
extra: {
let mut map = BTreeMap::new();
map.insert("EXTRA_KEY".to_owned(), "thing".to_owned());
map
}
}
)
}
}