Skip to content

Commit 034c0d1

Browse files
committed
[#98] Add command to display paths searched for command binaries
1 parent b9ff9f0 commit 034c0d1

File tree

3 files changed

+68
-23
lines changed

3 files changed

+68
-23
lines changed

iceoryx2-cli/iox2/src/cli.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ pub struct Cli {
1515
#[arg(short, long, help = "List all installed commands")]
1616
pub list: bool,
1717

18+
#[arg(
19+
short,
20+
long,
21+
help = "Display paths that will be checked for installed commands"
22+
)]
23+
pub paths: bool,
24+
1825
#[arg(
1926
short,
2027
long,

iceoryx2-cli/iox2/src/commands.rs

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use colored::*;
22
use std::env;
33
use std::fs;
4+
use std::io;
45
use std::path::PathBuf;
56
use std::process::{Command, Stdio};
67
use thiserror::Error;
@@ -43,35 +44,52 @@ pub fn list() {
4344
.for_each(|formatted_command| println!("{}", formatted_command));
4445
}
4546

47+
fn get_development_binaries_dir() -> Result<std::path::PathBuf, io::Error> {
48+
let current_exe = env::current_exe()?;
49+
50+
let build_type = if cfg!(debug_assertions) {
51+
"debug"
52+
} else {
53+
"release"
54+
};
55+
56+
let build_path = current_exe
57+
.parent()
58+
.and_then(|p| p.parent())
59+
.map(|p| p.join(build_type))
60+
.ok_or_else(|| {
61+
io::Error::new(
62+
io::ErrorKind::Other,
63+
"Failed to construct path of development dir",
64+
)
65+
})?;
66+
67+
Ok(build_path)
68+
}
69+
70+
fn get_installed_binaries_dirs() -> impl Iterator<Item = std::path::PathBuf> {
71+
env::var("PATH")
72+
.ok()
73+
.into_iter()
74+
.flat_map(|path_var| env::split_paths(&path_var).collect::<Vec<_>>())
75+
}
76+
4677
fn find() -> Vec<CommandInfo> {
47-
let development_commands = find_command_binaries_in_development_dirs();
48-
let installed_commands = find_command_binaries_in_system_path();
78+
let development_commands = find_development_command_binaries();
79+
let installed_commands = find_installed_command_binaries();
4980

5081
let mut all_commands = development_commands;
5182
all_commands.extend(installed_commands.iter().cloned());
5283
all_commands
5384
}
5485

55-
fn find_command_binaries_in_development_dirs() -> Vec<CommandInfo> {
56-
let current_exe = match env::current_exe() {
57-
Ok(exe) => exe,
86+
fn find_development_command_binaries() -> Vec<CommandInfo> {
87+
let development_binaries_dir = match get_development_binaries_dir() {
88+
Ok(location) => location,
5889
Err(_) => return Vec::new(),
5990
};
60-
let build_type = if cfg!(debug_assertions) {
61-
"debug"
62-
} else {
63-
"release"
64-
};
6591

66-
// Get the location of the binary directory for the build
67-
let binary_dir = current_exe
68-
.parent()
69-
.unwrap()
70-
.parent()
71-
.unwrap()
72-
.join(build_type);
73-
74-
fs::read_dir(binary_dir)
92+
fs::read_dir(development_binaries_dir)
7593
.ok()
7694
.into_iter()
7795
.flat_map(|entries| entries.filter_map(Result::ok))
@@ -92,11 +110,9 @@ fn find_command_binaries_in_development_dirs() -> Vec<CommandInfo> {
92110
.collect()
93111
}
94112

95-
fn find_command_binaries_in_system_path() -> Vec<CommandInfo> {
96-
env::var("PATH")
97-
.ok()
113+
fn find_installed_command_binaries() -> Vec<CommandInfo> {
114+
get_installed_binaries_dirs()
98115
.into_iter()
99-
.flat_map(|path_var| env::split_paths(&path_var).collect::<Vec<_>>())
100116
.flat_map(|path: PathBuf| {
101117
fs::read_dir(path)
102118
.into_iter()
@@ -130,6 +146,26 @@ fn is_valid_command_binary(path: &PathBuf) -> bool {
130146
&& path.extension().is_none() // Exclude files with extensions (e.g. '.d')
131147
}
132148

149+
pub fn paths() {
150+
let mut development_binaries_dirs = Vec::new();
151+
development_binaries_dirs.extend(get_development_binaries_dir().ok());
152+
153+
let mut installed_binaries_dirs = Vec::new();
154+
installed_binaries_dirs.extend(get_installed_binaries_dirs());
155+
156+
println!("{}", "Development Binary Paths:".bright_green().bold());
157+
for dir in &development_binaries_dirs {
158+
println!(" {}", dir.display().to_string().bold());
159+
}
160+
161+
println!();
162+
163+
println!("{}", "Installed Binary Paths:".bright_green().bold());
164+
for dir in &installed_binaries_dirs {
165+
println!(" {}", dir.display().to_string().bold());
166+
}
167+
}
168+
133169
pub fn execute_external_command(
134170
command_name: &str,
135171
args: &[String],

iceoryx2-cli/iox2/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ fn main() {
2727

2828
if cli.list {
2929
commands::list();
30+
} else if cli.paths {
31+
commands::paths();
3032
} else if !cli.external_command.is_empty() {
3133
let command_name = &cli.external_command[0];
3234
let command_args = &cli.external_command[1..];

0 commit comments

Comments
 (0)