lsb_release/lib.rs
1// Copyright (c) 2023 Nathan Sizemore <nathanrsizemore@gmail.com>
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21use std::{io, process::Command, str};
22
23#[derive(Debug, Default, Clone)]
24pub struct LsbRelease {
25 pub id: String,
26 pub desc: String,
27 pub version: String,
28 pub code_name: String,
29}
30
31pub fn info() -> io::Result<LsbRelease> {
32 let output = Command::new("lsb_release").args(["-a"]).output()?;
33 if !output.status.success() {
34 return Err(io::Error::new(io::ErrorKind::Other, unsafe {
35 str::from_utf8_unchecked(&output.stderr)
36 }));
37 }
38
39 let text = unsafe { str::from_utf8_unchecked(&output.stdout) };
40 let lines = text.split('\n').collect::<Vec<&str>>();
41 let mut info = LsbRelease::default();
42 for line in lines {
43 let split = line.split('\t').collect::<Vec<&str>>();
44 if split.len() != 2 {
45 continue;
46 }
47
48 if split[0] == "Distributor ID:" {
49 info.id = split[1].to_owned();
50 } else if split[0] == "Description:" {
51 info.desc = split[1].to_owned();
52 } else if split[0] == "Release:" {
53 info.version = split[1].to_owned();
54 } else if split[0] == "Codename:" {
55 info.code_name = split[1].to_owned();
56 }
57 }
58
59 Ok(info)
60}