use std::{io, process::Command, str};
#[derive(Debug, Default, Clone)]
pub struct LsbRelease {
pub id: String,
pub desc: String,
pub version: String,
pub code_name: String,
}
pub fn info() -> io::Result<LsbRelease> {
let output = Command::new("lsb_release").args(["-a"]).output()?;
if !output.status.success() {
return Err(io::Error::new(io::ErrorKind::Other, unsafe {
str::from_utf8_unchecked(&output.stderr)
}));
}
let text = unsafe { str::from_utf8_unchecked(&output.stdout) };
let lines = text.split('\n').collect::<Vec<&str>>();
let mut info = LsbRelease::default();
for line in lines {
let split = line.split('\t').collect::<Vec<&str>>();
if split.len() != 2 {
continue;
}
if split[0] == "Distributor ID:" {
info.id = split[1].to_owned();
} else if split[0] == "Description:" {
info.desc = split[1].to_owned();
} else if split[0] == "Release:" {
info.version = split[1].to_owned();
} else if split[0] == "Codename:" {
info.code_name = split[1].to_owned();
}
}
Ok(info)
}