Skip to content

Commit 8ea1acb

Browse files
authored
Merge pull request #27 from liushuyu/main
macchina-read: more robust distribution detection using os-release crate
2 parents 7ad46cb + dd3041e commit 8ea1acb

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

macchina-read/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ windows = "0.4.0"
1717
[target.'cfg(any(target_os = "linux", target_os = "netbsd"))'.dependencies]
1818
nix = "0.20.0"
1919

20+
[target.'cfg(target_os = "linux")'.dependencies]
21+
os-release = "0.1"
22+
2023
[target.'cfg(target_os = "macos")'.dependencies]
2124
core-foundation = "0.9.1"
2225
mach = "0.3.2"

macchina-read/src/shared/mod.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,10 @@ pub(crate) fn uptime() -> Result<String, ReadoutError> {
3131
/// Read distribution name from `/etc/os-release`
3232
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
3333
pub(crate) fn distribution() -> Result<String, ReadoutError> {
34-
let content = fs::File::open("/etc/os-release")?;
34+
use os_release::OsRelease;
35+
let content = OsRelease::new()?;
3536

36-
let head = Command::new("head")
37-
.args(&["-n", "1"])
38-
.stdin(Stdio::from(content))
39-
.stdout(Stdio::piped())
40-
.spawn()
41-
.expect("ERROR: failed to start \"head\" process");
42-
43-
let output = head
44-
.wait_with_output()
45-
.expect("ERROR: failed to wait for \"head\" process to exit");
46-
47-
let distribution =
48-
String::from_utf8(output.stdout).expect("ERROR: \"ps\" process stdout was not valid UTF-8");
49-
50-
Ok(extra::pop_newline(
51-
distribution.replace("\"", "").replace("NAME=", ""),
52-
))
37+
Ok(content.name)
5338
}
5439

5540
/// Read desktop environment name from `DESKTOP_SESSION` environment variable

0 commit comments

Comments
 (0)