feat: use /etc/os-release for detecting distro, use /etc/issue as fallback

This commit is contained in:
Gabriele Musco 2024-04-14 16:16:47 +02:00
parent 72d5b784a7
commit 984cbd7d32
No known key found for this signature in database
GPG key ID: 1068D795C80E51DE
3 changed files with 91 additions and 31 deletions

View file

@ -9,7 +9,6 @@ use crate::{
use std::collections::HashMap;
fn wivrn_deps() -> Vec<Dependency> {
// TODO: populate!
vec![
dep_cmake(),
dep_ninja(),

View file

@ -1,5 +1,5 @@
use crate::file_utils::get_reader;
use std::io::Read;
use std::io::{BufRead, Read};
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum LinuxDistro {
@ -14,38 +14,75 @@ pub enum LinuxDistro {
impl LinuxDistro {
pub fn get() -> Option<Self> {
if let Some(mut reader) = get_reader("/etc/issue") {
let mut buf = String::default();
if reader.read_to_string(&mut buf).is_ok() {
buf = buf.trim().to_lowercase();
if buf.contains("arch linux")
|| buf.contains("manjaro")
|| buf.contains("steamos")
|| buf.contains("steam os")
{
return Some(Self::Arch);
}
if buf.contains("debian")
|| buf.contains("ubuntu")
|| buf.contains("mint")
|| buf.contains("elementary")
|| buf.contains("pop")
{
return Some(Self::Debian);
}
if buf.contains("fedora") || buf.contains("nobara") {
return Some(Self::Fedora);
}
if buf.contains("gentoo") {
return Some(Self::Gentoo);
}
if buf.contains("alpine") || buf.contains("postmarket") {
return Some(Self::Alpine);
}
Self::get_from_etc_os_release().or_else(|| Self::get_from_etc_issue())
}
// TODO: detect suse, sles, rhel, nix
fn get_from_etc_os_release() -> Option<Self> {
Self::get_from_etc_os_release_file("/etc/os-release")
}
fn get_from_etc_os_release_file(fp: &str) -> Option<Self> {
if let Some(mut reader) = get_reader(fp) {
let mut buf = String::new();
loop {
match reader.read_line(&mut buf) {
Ok(0) => break,
Ok(_) => {
if buf.starts_with("NAME=\"") {
let name = buf
.split("=")
.last()
.unwrap_or_default()
.to_string()
.trim_matches('"')
.to_lowercase();
return Self::name_matcher(&name);
}
}
Err(_) => break,
}
}
}
None
}
fn get_from_etc_issue() -> Option<Self> {
if let Some(mut reader) = get_reader("/etc/issue") {
let mut buf = String::new();
if reader.read_to_string(&mut buf).is_ok() {
buf = buf.trim().to_lowercase();
}
}
None
}
fn name_matcher(s: &str) -> Option<Self> {
if s.contains("arch linux")
|| s.contains("manjaro")
|| s.contains("steamos")
|| s.contains("steam os")
{
return Some(Self::Arch);
}
if s.contains("debian")
|| s.contains("ubuntu")
|| s.contains("mint")
|| s.contains("elementary")
|| s.contains("pop")
{
return Some(Self::Debian);
}
if s.contains("fedora") || s.contains("nobara") {
return Some(Self::Fedora);
}
if s.contains("gentoo") {
return Some(Self::Gentoo);
}
if s.contains("alpine") || s.contains("postmarket") {
return Some(Self::Alpine);
}
// TODO: detect suse, sles, rhel, nix
None
}
@ -61,3 +98,16 @@ impl LinuxDistro {
}
}
}
#[cfg(test)]
mod tests {
use super::LinuxDistro;
#[test]
fn can_detect_arch_linux_from_etc_os_release() {
assert_eq!(
LinuxDistro::get_from_etc_os_release_file("./test/files/archlinux-os-release"),
Some(LinuxDistro::Arch)
)
}
}

View file

@ -0,0 +1,11 @@
NAME="Arch Linux"
PRETTY_NAME="Arch Linux"
ID=arch
BUILD_ID=rolling
ANSI_COLOR="38;2;23;147;209"
HOME_URL="https://archlinux.org/"
DOCUMENTATION_URL="https://wiki.archlinux.org/"
SUPPORT_URL="https://bbs.archlinux.org/"
BUG_REPORT_URL="https://gitlab.archlinux.org/groups/archlinux/-/issues"
PRIVACY_POLICY_URL="https://terms.archlinux.org/docs/privacy-policy/"
LOGO=archlinux-logo