Skip to content

Commit

Permalink
Rewrite get_os_distro()
Browse files Browse the repository at this point in the history
  • Loading branch information
enkerewpo committed Dec 20, 2024
1 parent 370f819 commit 071e438
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
37 changes: 19 additions & 18 deletions compiler-cli/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,30 @@ pub fn get_project_root(path: Utf8PathBuf) -> Result<Utf8PathBuf, Error> {

#[inline]
pub fn get_os() -> OS {
OS::from(std::env::consts::OS)
gleam_core::error::str2os(std::env::consts::OS)
}

// Return the distro enum if /etc/os-release exists, otherwise return Other
pub fn get_os_distro() -> Distro {
if let OS::Linux = get_os() {
let os_release = std::fs::read_to_string("/etc/os-release");
match os_release {
Ok(release) => {
let mut distro = Distro::Other;
for line in release.lines() {
if line.starts_with("ID=") {
let distro_id = line.split('=').nth(1).unwrap_or("other");
distro = Distro::from(distro_id);
break;
}
}
distro
}
Err(_) => Distro::Other,
}
let path = Utf8Path::new("/etc/os-release");
if get_os() != OS::Linux || !path.exists() {
return Distro::Other;
}
let os_release = read(&path);
if os_release.is_err() {
return Distro::Other;
}
let os_release = os_release.unwrap_or_default();
let distro = os_release.lines().find(|line| line.starts_with("ID="));
if let Some(distro) = distro {
let id = distro
.split('=')
.nth(1)
.unwrap_or("other")
.replace("\"", "");
return gleam_core::error::str2distro(&id);
} else {
Distro::Other
return Distro::Other;
}
}

Expand Down
26 changes: 11 additions & 15 deletions compiler-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,14 +359,12 @@ pub enum OS {
Other,
}

impl From<&str> for OS {
fn from(env_os: &str) -> Self {
match env_os {
"linux" => OS::Linux,
"macos" => OS::MacOS,
"windows" => OS::Windows,
_ => OS::Other,
}
pub fn str2os(env_os: &str) -> OS {
match env_os {
"linux" => OS::Linux,
"macos" => OS::MacOS,
"windows" => OS::Windows,
_ => OS::Other,
}
}

Expand All @@ -377,13 +375,11 @@ pub enum Distro {
Other,
}

impl From<&str> for Distro {
fn from(distro_id: &str) -> Self {
match distro_id {
"ubuntu" => Distro::Ubuntu,
"debian" => Distro::Debian,
_ => Distro::Other,
}
pub fn str2distro(distro_id: &str) -> Distro {
match distro_id {
"ubuntu" => Distro::Ubuntu,
"debian" => Distro::Debian,
_ => Distro::Other,
}
}

Expand Down
4 changes: 2 additions & 2 deletions compiler-core/src/error/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn test_shell_program_not_found_error() {

for cmd in &cmds {
for os in &oses {
let os_enum = OS::from(*os);
let os_enum = str2os(os);
match os_enum {
OS::MacOS | OS::Windows => {
let err = Error::ShellProgramNotFound {
Expand All @@ -25,7 +25,7 @@ fn test_shell_program_not_found_error() {
}
OS::Linux => {
for distro in &distros {
let distro_enum = Distro::from(*distro);
let distro_enum = str2distro(distro);
let err = Error::ShellProgramNotFound {
program: cmd.to_string(),
os: os_enum,
Expand Down

0 comments on commit 071e438

Please sign in to comment.