Skip to content

Commit

Permalink
feat: add codename detection
Browse files Browse the repository at this point in the history
* test: add codename detection
  • Loading branch information
exincore committed Jan 1, 2023
1 parent ac83b79 commit 65b38e5
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions os_info/src/linux/file_release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ fn retrieve(distributions: &[ReleaseInfo], root: &str) -> Option<Info> {

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()
});
Expand All @@ -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<String>,

/// A closure that determines the os codename from the release file contents.
codename: for<'b> fn(&'b str) -> Option<String>,
}

impl fmt::Debug for ReleaseInfo<'_> {
Expand Down Expand Up @@ -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 {
Expand All @@ -169,6 +175,7 @@ static DISTRIBUTIONS: [ReleaseInfo; 6] = [
.map(Version::from_string)
},
edition: |_| None,
codename: |_| None,
},
ReleaseInfo {
path: "etc/centos-release",
Expand All @@ -179,6 +186,7 @@ static DISTRIBUTIONS: [ReleaseInfo; 6] = [
.map(Version::from_string)
},
edition: |_| None,
codename: |_| None,
},
ReleaseInfo {
path: "etc/fedora-release",
Expand All @@ -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",
Expand All @@ -205,6 +215,7 @@ static DISTRIBUTIONS: [ReleaseInfo; 6] = [
.map(Version::from_string)
},
edition: |_| None,
codename: |_| None,
},
];

Expand Down Expand Up @@ -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()
}),
),
Expand All @@ -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()
}),
),
Expand All @@ -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()
}),
),
Expand All @@ -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()
}),
),
Expand Down Expand Up @@ -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()
}),
),
Expand All @@ -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()
}),
),
Expand Down

0 comments on commit 65b38e5

Please sign in to comment.