From 65b38e5bb6a8826c5fea68da45925eb0f3a32485 Mon Sep 17 00:00:00 2001 From: exin Date: Sun, 1 Jan 2023 12:13:42 -0600 Subject: [PATCH] feat: add codename detection * test: add codename detection --- os_info/src/linux/file_release.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/os_info/src/linux/file_release.rs b/os_info/src/linux/file_release.rs index 5c534aa8..815a93d1 100644 --- a/os_info/src/linux/file_release.rs +++ b/os_info/src/linux/file_release.rs @@ -42,11 +42,13 @@ fn retrieve(distributions: &[ReleaseInfo], root: &str) -> Option { let version = (release_info.version)(&file_content); let edition = (release_info.edition)(&file_content); + let codename = (release_info.codename)(&file_content); return Some(Info { os_type: os_type.unwrap(), version: version.unwrap_or(Version::Unknown), edition, + codename, bitness: Bitness::Unknown, ..Default::default() }); @@ -70,6 +72,9 @@ struct ReleaseInfo<'a> { /// A closure that determines the os edition (variant) from the release file contents. edition: for<'b> fn(&'b str) -> Option, + + /// A closure that determines the os codename from the release file contents. + codename: for<'b> fn(&'b str) -> Option, } impl fmt::Debug for ReleaseInfo<'_> { @@ -156,6 +161,7 @@ static DISTRIBUTIONS: [ReleaseInfo; 6] = [ .map(Version::from_string) }, edition: |release| Matcher::KeyValue { key: "VARIANT" }.find(release), + codename: |release| Matcher::KeyValue { key: "VERSION_CODENAME" }.find(release).filter(|v| !v.is_empty()), }, // Older distributions must have their specific release file parsed. ReleaseInfo { @@ -169,6 +175,7 @@ static DISTRIBUTIONS: [ReleaseInfo; 6] = [ .map(Version::from_string) }, edition: |_| None, + codename: |_| None, }, ReleaseInfo { path: "etc/centos-release", @@ -179,6 +186,7 @@ static DISTRIBUTIONS: [ReleaseInfo; 6] = [ .map(Version::from_string) }, edition: |_| None, + codename: |_| None, }, ReleaseInfo { path: "etc/fedora-release", @@ -189,12 +197,14 @@ static DISTRIBUTIONS: [ReleaseInfo; 6] = [ .map(Version::from_string) }, edition: |_| None, + codename: |_| None, }, ReleaseInfo { path: "etc/alpine-release", os_type: |_| Some(Type::Alpine), version: |release| Matcher::AllTrimmed.find(release).map(Version::from_string), edition: |_| None, + codename: |_| None, }, ReleaseInfo { path: "etc/redhat-release", @@ -205,6 +215,7 @@ static DISTRIBUTIONS: [ReleaseInfo; 6] = [ .map(Version::from_string) }, edition: |_| None, + codename: |_| None, }, ]; @@ -341,6 +352,7 @@ mod tests { Some(Info { os_type: Type::Mint, version: Version::Semantic(20, 0, 0), + codename: Some("ulyana".to_owned()), ..Default::default() }), ), @@ -349,6 +361,7 @@ mod tests { Some(Info { os_type: Type::NixOS, version: Version::Custom("21.05pre275822.916ee862e87".to_owned()), + codename: Some("okapi".to_owned()), ..Default::default() }), ), @@ -369,6 +382,7 @@ mod tests { Some(Info { os_type: Type::Pop, version: Version::Semantic(22, 4, 0), + codename: Some("jammy".to_string()), ..Default::default() }), ), @@ -377,6 +391,7 @@ mod tests { Some(Info { os_type: Type::Raspbian, version: Version::Semantic(10, 0, 0), + codename: Some("buster".to_owned()), ..Default::default() }), ), @@ -410,6 +425,7 @@ mod tests { Some(Info { os_type: Type::Solus, version: Version::Semantic(4, 3, 0), + codename: Some("fortitude".to_string()), ..Default::default() }), ), @@ -434,6 +450,7 @@ mod tests { Some(Info { os_type: Type::Ubuntu, version: Version::Semantic(18, 10, 0), + codename: Some("cosmic".to_string()), ..Default::default() }), ),