vx_cli/commands/
execute.rs1use crate::ui::UI;
4use vx_core::{PluginRegistry, Result, ToolContext, VxError};
5
6pub async fn handle(
8 registry: &PluginRegistry,
9 tool_name: &str,
10 args: &[String],
11 use_system_path: bool,
12) -> Result<()> {
13 let exit_code = execute_tool(registry, tool_name, args, use_system_path).await?;
14 if exit_code != 0 {
15 std::process::exit(exit_code);
16 }
17 Ok(())
18}
19
20pub async fn execute_tool(
22 registry: &PluginRegistry,
23 tool_name: &str,
24 args: &[String],
25 use_system_path: bool,
26) -> Result<i32> {
27 UI::debug(&format!("Executing: {} {}", tool_name, args.join(" ")));
28
29 let mut context = ToolContext::new(
31 tool_name.to_string(),
32 "latest".to_string(), args.to_vec(),
34 );
35 context.use_system_path = use_system_path;
36 context.working_directory = std::env::current_dir().ok();
37
38 if !use_system_path && registry.supports_tool(tool_name) {
40 if let Some(tool) = registry.get_tool(tool_name) {
42 UI::debug(&format!("Using vx-managed tool: {}", tool_name));
43
44 let result = tool.execute(args, &context).await?;
46 return Ok(result.exit_code);
47 }
48 }
49
50 if use_system_path {
52 UI::debug(&format!("Using system PATH for: {}", tool_name));
53 } else {
54 UI::warn(&format!(
55 "Tool '{}' not found in vx registry, falling back to system PATH",
56 tool_name
57 ));
58 }
59
60 let status = std::process::Command::new(tool_name)
61 .args(args)
62 .status()
63 .map_err(|_| VxError::ToolNotFound {
64 tool_name: tool_name.to_string(),
65 })?;
66
67 Ok(status.code().unwrap_or(1))
68}