microfetch/src/release.rs

43 lines
1.4 KiB
Rust
Raw Normal View History

2024-08-03 21:07:11 +00:00
use color_eyre::Result;
2024-08-03 17:42:48 +00:00
use std::fs::{self, read_to_string};
2024-08-04 09:44:12 +00:00
use std::io;
2024-08-03 17:42:48 +00:00
// Try to detect OS type as accurately as possible and without depending on uname.
// /etc/os-release should generally imply Linux, and /etc/bsd-release would imply BSD system.
2024-08-04 09:44:12 +00:00
fn detect_os() -> Result<&'static str, io::Error> {
2024-08-03 17:42:48 +00:00
if fs::metadata("/etc/os-release").is_ok() || fs::metadata("/usr/lib/os-release").is_ok() {
2024-08-04 09:44:12 +00:00
Ok("Linux")
2024-08-03 17:42:48 +00:00
} else if fs::metadata("/etc/rc.conf").is_ok() || fs::metadata("/etc/bsd-release").is_ok() {
2024-08-04 09:44:12 +00:00
Ok("BSD")
2024-08-03 17:42:48 +00:00
} else {
2024-08-04 09:44:12 +00:00
Ok("Unknown")
2024-08-03 17:42:48 +00:00
}
}
pub fn get_system_info() -> Result<String, io::Error> {
let system = detect_os()?;
2024-08-04 09:44:12 +00:00
let kernel_release = read_to_string("/proc/sys/kernel/osrelease")?;
let kernel_release = kernel_release.trim();
2024-08-03 17:42:48 +00:00
2024-08-04 09:44:12 +00:00
let architecture = read_to_string("/proc/sys/kernel/arch")?;
let architecture = architecture.trim();
2024-08-03 17:42:48 +00:00
2024-08-03 20:04:26 +00:00
let result = format!("{system} {kernel_release} ({architecture})");
2024-08-03 17:42:48 +00:00
Ok(result)
}
2024-08-03 21:05:57 +00:00
pub fn get_os_pretty_name() -> Result<String, io::Error> {
let os_release_content = read_to_string("/etc/os-release")?;
let pretty_name = os_release_content
.lines()
2024-08-03 17:42:48 +00:00
.find(|line| line.starts_with("PRETTY_NAME="))
.map(|line| {
line.trim_start_matches("PRETTY_NAME=")
.trim_matches('"')
.to_string()
});
2024-08-03 21:07:11 +00:00
Ok(pretty_name.unwrap_or_else(|| "Unknown".to_string()))
2024-08-03 17:42:48 +00:00
}