Skip to content

Commit

Permalink
[eclipse-iceoryx#98] List available cli commands
Browse files Browse the repository at this point in the history
  • Loading branch information
orecham committed Jan 28, 2024
1 parent ec90d5b commit 7dc925e
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 11 deletions.
2 changes: 1 addition & 1 deletion iceoryx2-cli/iox2-introspect/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn main() {
println!("Hello, world!");
println!("Not implemented. Stay tuned !");
}
2 changes: 1 addition & 1 deletion iceoryx2-cli/iox2-processes/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn main() {
println!("Hello, world!");
println!("Not implemented. Stay tuned !");
}
2 changes: 1 addition & 1 deletion iceoryx2-cli/iox2-pub/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn main() {
println!("Hello, world!");
println!("Not implemented. Stay tuned !");
}
2 changes: 1 addition & 1 deletion iceoryx2-cli/iox2-rpc/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn main() {
println!("Hello, world!");
println!("Not implemented. Stay tuned !");
}
4 changes: 1 addition & 3 deletions iceoryx2-cli/iox2-services/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
fn main() {
println!("Hello, world!");
println!("Not implemented. Stay tuned !");
}


2 changes: 1 addition & 1 deletion iceoryx2-cli/iox2-sub/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn main() {
println!("Hello, world!");
println!("Not implemented. Stay tuned !");
}
2 changes: 0 additions & 2 deletions iceoryx2-cli/iox2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ repository = { workspace = true }
rust-version = { workspace = true }
version = { workspace = true }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
79 changes: 78 additions & 1 deletion iceoryx2-cli/iox2/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,80 @@
use std::env;
use std::fs;
use std::path::PathBuf;

fn find_command_binaries_in_development_dirs() -> Vec<String> {
let mut commands = Vec::new();
let current_exe = match env::current_exe() {
Ok(exe) => exe,
Err(_) => return commands,
};
let target_dir_name = if cfg!(debug_assertions) {
"debug"
} else {
"release"
};
let target_dir = current_exe
.parent()
.unwrap()
.parent()
.unwrap()
.join(target_dir_name);

if let Ok(entries) = fs::read_dir(&target_dir) {
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()) {
let stripped = command_name.strip_prefix("iox2-").unwrap_or(command_name);
commands.push(stripped.to_string());
}
}
}
}

commands
}

fn find_command_binaries_in_system_path() -> Vec<String> {
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(command_name.to_string());
}
}
}
}
}
}

commands
}

fn is_valid_command_binary(path: &PathBuf) -> bool {
path.is_file()
&& path
.file_stem()
.unwrap()
.to_str()
.unwrap()
.starts_with("iox2-")
&& path.extension().is_none() // Exclude files with extensions (e.g. '.d')
}

fn main() {
println!("Hello, world!")
let mut commands = find_command_binaries_in_development_dirs();
if commands.is_empty() {
commands = find_command_binaries_in_system_path();
}

println!("Available commands:");
for command in commands {
println!("- {}", command);
}
}

0 comments on commit 7dc925e

Please sign in to comment.