Skip to content

Commit

Permalink
[eclipse-iceoryx#98] Change C++-style parsing of system path to Rust …
Browse files Browse the repository at this point in the history
…functional style
  • Loading branch information
orecham committed May 22, 2024
1 parent 38ebaf2 commit 725966d
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions iceoryx2-cli/iox2/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,27 +84,30 @@ fn find_command_binaries_in_development_dirs() -> Vec<CommandInfo> {
}

fn find_command_binaries_in_system_path() -> Vec<CommandInfo> {
let mut commands = Vec::new();
if let Ok(path_var) = env::var("PATH") {
for path in env::split_paths(&path_var) {
if let Ok(entries) = fs::read_dir(path) {
for entry in entries.filter_map(Result::ok) {
let path = entry.path();
if is_valid_command_binary(&path) {
if let Some(command_name) = path.file_name().and_then(|n| n.to_str()) {
commands.push(CommandInfo {
name: command_name.to_string(),
path,
is_development: false,
});
}
}
}
env::var("PATH")
.ok()
.into_iter()
.flat_map(|path_var| env::split_paths(&path_var).collect::<Vec<_>>())
.flat_map(|path: PathBuf| {
fs::read_dir(path)
.into_iter()
.flat_map(|read_dir| read_dir.filter_map(Result::ok))
})
.filter_map(|entry| {
let path = entry.path();
if is_valid_command_binary(&path) {
path.file_name()
.and_then(|n| n.to_str())
.map(|command_name| CommandInfo {
name: command_name.to_string(),
path: path.clone(),
is_development: false,
})
} else {
None
}
}
}

commands
})
.collect()
}

fn is_valid_command_binary(path: &PathBuf) -> bool {
Expand Down

0 comments on commit 725966d

Please sign in to comment.