Skip to content

Commit

Permalink
Add find.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Nov 20, 2023
1 parent 2fe32c2 commit c0f421a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 40 deletions.
41 changes: 1 addition & 40 deletions crates/system-env/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::env::{self, consts};
use std::env::consts;
use std::fmt;

/// Architecture of the host environment.
Expand Down Expand Up @@ -167,42 +167,3 @@ impl fmt::Display for SystemOS {
write!(f, "{}", format!("{:?}", self).to_lowercase())
}
}

/// Return true if the provided program (without extension) is available
/// on `PATH`. Will use `PATHEXT` to cycle through known extensions.
#[cfg(windows)]
pub fn is_command_on_path(name: &str) -> bool {
let Ok(system_path) = env::var("PATH") else {
return false;
};
let Ok(path_ext) = env::var("PATHEXT") else {
return false;
};
let exts = path_ext.split(';').collect::<Vec<_>>();

for path_dir in env::split_paths(&system_path) {
for ext in &exts {
if path_dir.join(format!("{name}{ext}")).exists() {
return true;
}
}
}

false
}

/// Return true if the provided command is available on `PATH`.
#[cfg(not(windows))]
pub fn is_command_on_path(name: &str) -> bool {
let Ok(system_path) = env::var("PATH") else {
return false;
};

for path_dir in env::split_paths(&system_path) {
if path_dir.join(name).exists() {
return true;
}
}

false
}
51 changes: 51 additions & 0 deletions crates/system-env/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use std::env;
use std::path::PathBuf;

/// Return an absolute path to the provided program (without extension)
/// by checking `PATH` and cycling through `PATHEXT` extensions.
#[cfg(windows)]
pub fn find_command_on_path(name: &str) -> Option<PathBuf> {
let Ok(system_path) = env::var("PATH") else {
return None;
};
let Ok(path_ext) = env::var("PATHEXT") else {
return None;
};
let exts = path_ext.split(';').collect::<Vec<_>>();

for path_dir in env::split_paths(&system_path) {
for ext in &exts {
let path = path_dir.join(format!("{name}{ext}"));

if path.exists() {
return Some(path);
}
}
}

None
}

/// Return an absolute path to the provided command by checking `PATH`.
#[cfg(not(windows))]
pub fn find_command_on_path(name: &str) -> Option<PathBuf> {
let Ok(system_path) = env::var("PATH") else {
return None;
};

for path_dir in env::split_paths(&system_path) {
let path = path_dir.join(name);

if path.exists() {
return Some(path);
}
}

None
}

/// Return true if the provided command/program (without extension)
/// is available on `PATH`.
pub fn is_command_on_path(name: &str) -> bool {
find_command_on_path(name).is_some()
}
2 changes: 2 additions & 0 deletions crates/system-env/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
mod deps;
mod env;
mod error;
mod helpers;
mod pm;
mod pm_vendor;
mod system;

pub use deps::*;
pub use env::*;
pub use error::*;
pub use helpers::*;
pub use pm::*;
pub use pm_vendor::*;
pub use system::*;

0 comments on commit c0f421a

Please sign in to comment.