Skip to content

Commit 38ebaf2

Browse files
committed
[#98] Execute commands found in separate binaries
1 parent a465646 commit 38ebaf2

File tree

4 files changed

+78
-8
lines changed

4 files changed

+78
-8
lines changed

iceoryx2-cli/iox2/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ human-panic = "1.2.3"
1515
better-panic = "0.3.0"
1616
clap = { version = "4.4.18", features = ["derive"] }
1717
colored = "2.0"
18+
thiserror = "1.0.56"

iceoryx2-cli/iox2/src/cli.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ pub struct Cli {
1616

1717
#[arg(short, long, help = "Run development commands")]
1818
pub dev: bool,
19+
20+
#[arg(hide = true, required = false)]
21+
pub external_command: Vec<String>,
1922
}
2023

2124
fn help_template() -> &'static str {

iceoryx2-cli/iox2/src/commands.rs

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@ use colored::*;
22
use std::env;
33
use std::fs;
44
use std::path::PathBuf;
5+
use std::process::{Command, Stdio};
6+
use thiserror::Error;
7+
8+
#[derive(Clone, Debug)]
9+
struct CommandInfo {
10+
name: String,
11+
path: PathBuf,
12+
is_development: bool,
13+
}
14+
15+
#[derive(Error, Debug)]
16+
pub enum ExecutionError {
17+
#[error("Command not found: {0}")]
18+
NotFound(String),
19+
#[error("Execution failed: {0}")]
20+
Failed(String),
21+
}
522

623
pub fn list() {
724
println!("Installed Commands:");
@@ -11,20 +28,13 @@ pub fn list() {
1128
" {}",
1229
format!(
1330
"{}{}",
31+
if command.is_development { "(dev) " } else { "" },
1432
command.name.bold(),
15-
if command.is_development { " (dev)" } else { "" }
1633
)
1734
);
1835
}
1936
}
2037

21-
#[derive(Clone, Debug)]
22-
struct CommandInfo {
23-
name: String,
24-
path: PathBuf,
25-
is_development: bool,
26-
}
27-
2838
fn find() -> Vec<CommandInfo> {
2939
let development_commands = find_command_binaries_in_development_dirs();
3040
let installed_commands = find_command_binaries_in_system_path();
@@ -107,3 +117,44 @@ fn is_valid_command_binary(path: &PathBuf) -> bool {
107117
.starts_with("iox2-")
108118
&& path.extension().is_none() // Exclude files with extensions (e.g. '.d')
109119
}
120+
121+
pub fn execute_external_command(
122+
command_name: &str,
123+
args: &[String],
124+
dev_flag_present: bool,
125+
) -> Result<(), ExecutionError> {
126+
let available_commands = find();
127+
if let Some(command_info) = available_commands.into_iter().find(|c| {
128+
&c.name == command_name
129+
&& if dev_flag_present {
130+
c.is_development == true
131+
} else {
132+
if c.is_development {
133+
println!(
134+
"Development version of {} found but --dev flag is not set.",
135+
command_name
136+
)
137+
}
138+
false
139+
}
140+
}) {
141+
execute(&command_info, Some(args))
142+
} else {
143+
Err(ExecutionError::NotFound(command_name.to_string()))
144+
}
145+
}
146+
147+
fn execute(command_info: &CommandInfo, args: Option<&[String]>) -> Result<(), ExecutionError> {
148+
let mut command = Command::new(&command_info.path);
149+
command.stdout(Stdio::inherit()).stderr(Stdio::inherit());
150+
if let Some(arguments) = args {
151+
command.args(arguments);
152+
}
153+
match command.status() {
154+
Ok(_) => Ok(()),
155+
Err(e) => Err(ExecutionError::Failed(format!(
156+
"Failed to execute command: {}",
157+
e
158+
))),
159+
}
160+
}

iceoryx2-cli/iox2/src/main.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,20 @@ fn main() {
2727

2828
if cli.list {
2929
commands::list();
30+
} else if !cli.external_command.is_empty() {
31+
let command_name = &cli.external_command[0];
32+
let command_args = &cli.external_command[1..];
33+
match commands::execute_external_command(command_name, command_args, cli.dev) {
34+
Ok(()) => {
35+
// Command executed successfully, nothing to do
36+
}
37+
Err(commands::ExecutionError::NotFound(_)) => {
38+
// Command not found, print help
39+
println!("Command not found. See all installed commands with --list.");
40+
}
41+
Err(commands::ExecutionError::Failed(_)) => {
42+
println!("Command found but execution failed ...");
43+
}
44+
}
3045
}
3146
}

0 commit comments

Comments
 (0)