|
| 1 | +//! Linux distro detection via `/etc/os-release`. |
| 2 | +//! |
| 3 | +//! Provides distro family classification and package manager install commands. |
| 4 | +//! Detected once and cached for the lifetime of the process. |
| 5 | +
|
| 6 | +use std::sync::OnceLock; |
| 7 | + |
| 8 | +/// Parsed Linux distribution info from `/etc/os-release`. |
| 9 | +#[derive(Debug)] |
| 10 | +pub struct LinuxDistro { |
| 11 | + pub id: String, |
| 12 | + pub id_like: Vec<String>, |
| 13 | + pub pretty_name: String, |
| 14 | +} |
| 15 | + |
| 16 | +/// High-level distro family, determines the package manager. |
| 17 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 18 | +pub enum DistroFamily { |
| 19 | + Debian, |
| 20 | + Fedora, |
| 21 | + Arch, |
| 22 | + Suse, |
| 23 | + Unknown, |
| 24 | +} |
| 25 | + |
| 26 | +static DETECTED: OnceLock<Option<LinuxDistro>> = OnceLock::new(); |
| 27 | + |
| 28 | +impl LinuxDistro { |
| 29 | + /// Returns the detected distro, reading `/etc/os-release` once. |
| 30 | + /// Returns `None` if the file is missing or unparseable. |
| 31 | + #[cfg(target_os = "linux")] |
| 32 | + pub fn detect() -> Option<&'static Self> { |
| 33 | + DETECTED |
| 34 | + .get_or_init(|| { |
| 35 | + let content = std::fs::read_to_string("/etc/os-release").ok()?; |
| 36 | + Self::parse(&content) |
| 37 | + }) |
| 38 | + .as_ref() |
| 39 | + } |
| 40 | + |
| 41 | + /// Parses the content of an os-release file. |
| 42 | + fn parse(content: &str) -> Option<Self> { |
| 43 | + let mut id = String::new(); |
| 44 | + let mut id_like = String::new(); |
| 45 | + let mut pretty_name = String::new(); |
| 46 | + |
| 47 | + for line in content.lines() { |
| 48 | + if let Some(val) = line.strip_prefix("ID=") { |
| 49 | + id = val.trim_matches('"').to_lowercase(); |
| 50 | + } else if let Some(val) = line.strip_prefix("ID_LIKE=") { |
| 51 | + id_like = val.trim_matches('"').to_lowercase(); |
| 52 | + } else if let Some(val) = line.strip_prefix("PRETTY_NAME=") { |
| 53 | + pretty_name = val.trim_matches('"').to_string(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if id.is_empty() { |
| 58 | + return None; |
| 59 | + } |
| 60 | + |
| 61 | + Some(Self { |
| 62 | + id, |
| 63 | + id_like: id_like.split_whitespace().map(String::from).collect(), |
| 64 | + pretty_name, |
| 65 | + }) |
| 66 | + } |
| 67 | + |
| 68 | + /// Classifies this distro into a package-manager family. |
| 69 | + pub fn family(&self) -> DistroFamily { |
| 70 | + let tokens: Vec<&str> = std::iter::once(self.id.as_str()) |
| 71 | + .chain(self.id_like.iter().map(String::as_str)) |
| 72 | + .collect(); |
| 73 | + |
| 74 | + for t in &tokens { |
| 75 | + if *t == "debian" || *t == "ubuntu" { |
| 76 | + return DistroFamily::Debian; |
| 77 | + } |
| 78 | + if *t == "fedora" || *t == "rhel" || *t == "centos" { |
| 79 | + return DistroFamily::Fedora; |
| 80 | + } |
| 81 | + if *t == "arch" { |
| 82 | + return DistroFamily::Arch; |
| 83 | + } |
| 84 | + if *t == "suse" || *t == "opensuse" || t.starts_with("opensuse") { |
| 85 | + return DistroFamily::Suse; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + DistroFamily::Unknown |
| 90 | + } |
| 91 | + |
| 92 | + /// Returns the distro-specific install command for the given package, or `None` if unknown. |
| 93 | + pub fn install_command(&self, package: &str) -> Option<String> { |
| 94 | + match self.family() { |
| 95 | + DistroFamily::Debian => Some(format!("sudo apt install {}", package)), |
| 96 | + DistroFamily::Fedora => Some(format!("sudo dnf install {}", package)), |
| 97 | + DistroFamily::Arch => Some(format!("sudo pacman -S {}", package)), |
| 98 | + DistroFamily::Suse => Some(format!("sudo zypper install {}", package)), |
| 99 | + DistroFamily::Unknown => None, |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +#[cfg(test)] |
| 105 | +mod tests { |
| 106 | + use super::*; |
| 107 | + |
| 108 | + fn distro(content: &str) -> Option<LinuxDistro> { |
| 109 | + LinuxDistro::parse(content) |
| 110 | + } |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn test_ubuntu() { |
| 114 | + let d = distro("ID=ubuntu\nID_LIKE=debian\nPRETTY_NAME=\"Ubuntu 22.04 LTS\"\n").unwrap(); |
| 115 | + assert_eq!(d.id, "ubuntu"); |
| 116 | + assert_eq!(d.id_like, vec!["debian"]); |
| 117 | + assert_eq!(d.pretty_name, "Ubuntu 22.04 LTS"); |
| 118 | + assert_eq!(d.family(), DistroFamily::Debian); |
| 119 | + assert_eq!(d.install_command("smbclient").unwrap(), "sudo apt install smbclient"); |
| 120 | + } |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn test_fedora() { |
| 124 | + let d = distro("ID=fedora\nVERSION_ID=39\nPRETTY_NAME=\"Fedora Linux 39\"\n").unwrap(); |
| 125 | + assert_eq!(d.family(), DistroFamily::Fedora); |
| 126 | + assert_eq!( |
| 127 | + d.install_command("samba-client").unwrap(), |
| 128 | + "sudo dnf install samba-client" |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + #[test] |
| 133 | + fn test_arch() { |
| 134 | + let d = distro("ID=arch\nBUILD_ID=rolling\nPRETTY_NAME=\"Arch Linux\"\n").unwrap(); |
| 135 | + assert_eq!(d.family(), DistroFamily::Arch); |
| 136 | + assert_eq!(d.install_command("smbclient").unwrap(), "sudo pacman -S smbclient"); |
| 137 | + } |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn test_opensuse() { |
| 141 | + let d = distro("ID=opensuse-tumbleweed\nID_LIKE=\"suse\"\nPRETTY_NAME=\"openSUSE Tumbleweed\"\n").unwrap(); |
| 142 | + assert_eq!(d.family(), DistroFamily::Suse); |
| 143 | + assert_eq!( |
| 144 | + d.install_command("samba-client").unwrap(), |
| 145 | + "sudo zypper install samba-client" |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + #[test] |
| 150 | + fn test_rhel_derivative() { |
| 151 | + let d = distro("ID=rocky\nID_LIKE=\"rhel centos fedora\"\nPRETTY_NAME=\"Rocky Linux 9\"\n").unwrap(); |
| 152 | + assert_eq!(d.family(), DistroFamily::Fedora); |
| 153 | + assert_eq!(d.id_like, vec!["rhel", "centos", "fedora"]); |
| 154 | + } |
| 155 | + |
| 156 | + #[test] |
| 157 | + fn test_linux_mint() { |
| 158 | + let d = distro("ID=linuxmint\nID_LIKE=ubuntu\nPRETTY_NAME=\"Linux Mint 21\"\n").unwrap(); |
| 159 | + assert_eq!(d.family(), DistroFamily::Debian); |
| 160 | + } |
| 161 | + |
| 162 | + #[test] |
| 163 | + fn test_unknown_distro() { |
| 164 | + let d = distro("ID=nixos\nPRETTY_NAME=\"NixOS 23.11\"\n").unwrap(); |
| 165 | + assert_eq!(d.family(), DistroFamily::Unknown); |
| 166 | + assert!(d.install_command("smbclient").is_none()); |
| 167 | + } |
| 168 | + |
| 169 | + #[test] |
| 170 | + fn test_empty_content() { |
| 171 | + assert!(distro("").is_none()); |
| 172 | + } |
| 173 | + |
| 174 | + #[test] |
| 175 | + fn test_quoted_id() { |
| 176 | + let d = distro("ID=\"ubuntu\"\nPRETTY_NAME=\"Ubuntu\"\n").unwrap(); |
| 177 | + assert_eq!(d.id, "ubuntu"); |
| 178 | + assert_eq!(d.family(), DistroFamily::Debian); |
| 179 | + } |
| 180 | + |
| 181 | + #[test] |
| 182 | + fn test_different_packages() { |
| 183 | + let d = distro("ID=ubuntu\nID_LIKE=debian\n").unwrap(); |
| 184 | + assert_eq!(d.install_command("gvfs-smb").unwrap(), "sudo apt install gvfs-smb"); |
| 185 | + assert_eq!(d.install_command("gio").unwrap(), "sudo apt install gio"); |
| 186 | + } |
| 187 | +} |
0 commit comments