Skip to content

Commit

Permalink
[eclipse-iceoryx#98] Move methods to find commands into a separate mo…
Browse files Browse the repository at this point in the history
…dule
  • Loading branch information
orecham committed May 22, 2024
1 parent f688df6 commit f2782ee
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 69 deletions.
2 changes: 2 additions & 0 deletions iceoryx2-cli/iox2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ rust-version = { workspace = true }
version = { workspace = true }

[dependencies]
human-panic = "1.2.3"
better-panic = "0.3.0"
75 changes: 75 additions & 0 deletions iceoryx2-cli/iox2/src/commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use std::env;
use std::fs;
use std::path::PathBuf;

pub fn find() -> Vec<String> {
let mut commands = find_command_binaries_in_development_dirs();
if commands.is_empty() {
commands = find_command_binaries_in_system_path();
}
commands
}

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')
}
87 changes: 18 additions & 69 deletions iceoryx2-cli/iox2/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,80 +1,29 @@
use std::env;
use std::fs;
use std::path::PathBuf;
#[cfg(not(debug_assertions))]
use human_panic::setup_panic;

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);
#[cfg(debug_assertions)]
extern crate better_panic;

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')
}
mod commands;

fn main() {
let mut commands = find_command_binaries_in_development_dirs();
if commands.is_empty() {
commands = find_command_binaries_in_system_path();
#[cfg(not(debug_assertions))]
{
setup_panic!();
}
#[cfg(debug_assertions)]
{
better_panic::Settings::debug()
.most_recent_first(false)
.lineno_suffix(true)
.verbosity(better_panic::Verbosity::Full)
.install();
}

let commands = commands::find();

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

0 comments on commit f2782ee

Please sign in to comment.