@@ -2,6 +2,23 @@ use colored::*;
2
2
use std:: env;
3
3
use std:: fs;
4
4
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
+ }
5
22
6
23
pub fn list ( ) {
7
24
println ! ( "Installed Commands:" ) ;
@@ -11,20 +28,13 @@ pub fn list() {
11
28
" {}" ,
12
29
format!(
13
30
"{}{}" ,
31
+ if command. is_development { "(dev) " } else { "" } ,
14
32
command. name. bold( ) ,
15
- if command. is_development { " (dev)" } else { "" }
16
33
)
17
34
) ;
18
35
}
19
36
}
20
37
21
- #[ derive( Clone , Debug ) ]
22
- struct CommandInfo {
23
- name : String ,
24
- path : PathBuf ,
25
- is_development : bool ,
26
- }
27
-
28
38
fn find ( ) -> Vec < CommandInfo > {
29
39
let development_commands = find_command_binaries_in_development_dirs ( ) ;
30
40
let installed_commands = find_command_binaries_in_system_path ( ) ;
@@ -107,3 +117,44 @@ fn is_valid_command_binary(path: &PathBuf) -> bool {
107
117
. starts_with ( "iox2-" )
108
118
&& path. extension ( ) . is_none ( ) // Exclude files with extensions (e.g. '.d')
109
119
}
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
+ }
0 commit comments