diff --git a/src/dependencies/wivrn_deps.rs b/src/dependencies/wivrn_deps.rs index 394e269..a1e2c75 100644 --- a/src/dependencies/wivrn_deps.rs +++ b/src/dependencies/wivrn_deps.rs @@ -9,7 +9,6 @@ use crate::{ use std::collections::HashMap; fn wivrn_deps() -> Vec { - // TODO: populate! vec![ dep_cmake(), dep_ninja(), diff --git a/src/linux_distro.rs b/src/linux_distro.rs index b641119..38a0e00 100644 --- a/src/linux_distro.rs +++ b/src/linux_distro.rs @@ -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 { - 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::get_from_etc_os_release_file("/etc/os-release") + } + + fn get_from_etc_os_release_file(fp: &str) -> Option { + 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 { + 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 { + 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) + ) + } +} diff --git a/test/files/archlinux-os-release b/test/files/archlinux-os-release new file mode 100644 index 0000000..7f8c926 --- /dev/null +++ b/test/files/archlinux-os-release @@ -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