vx_cli/commands/
execute.rs

1//! Execute command implementation - Transparent proxy for tool execution
2
3use crate::ui::UI;
4use vx_core::{PluginRegistry, Result, ToolProxy, VxError};
5
6/// Handle the execute command
7pub 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
20/// Execute tool with given arguments using the transparent proxy system
21pub 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    if use_system_path {
30        // Use system PATH directly
31        UI::debug(&format!("Using system PATH for: {}", tool_name));
32        return execute_system_tool(tool_name, args).await;
33    }
34
35    // Use the transparent proxy system
36    match ToolProxy::new() {
37        Ok(proxy) => {
38            UI::debug(&format!("Using vx transparent proxy for: {}", tool_name));
39
40            // Check if tool is available
41            if !proxy.is_tool_available(tool_name).await {
42                UI::warn(&format!(
43                    "Tool '{}' not found in vx or system PATH",
44                    tool_name
45                ));
46                UI::hint(&format!(
47                    "Try running 'vx install {}' to install it",
48                    tool_name
49                ));
50                UI::hint("Or use --use-system-path to search system PATH only");
51                return Err(VxError::ToolNotFound {
52                    tool_name: tool_name.to_string(),
53                });
54            }
55
56            // Show which version will be used
57            if let Ok(version) = proxy.get_effective_version(tool_name).await {
58                UI::debug(&format!("Using {} version: {}", tool_name, version));
59            }
60
61            // Execute through proxy
62            proxy.execute_tool(tool_name, args).await
63        }
64        Err(e) => {
65            UI::warn(&format!("Failed to create tool proxy: {}", e));
66            UI::info("Falling back to system PATH execution");
67            execute_system_tool(tool_name, args).await
68        }
69    }
70}
71
72/// Execute a tool using system PATH
73async fn execute_system_tool(tool_name: &str, args: &[String]) -> Result<i32> {
74    let status = std::process::Command::new(tool_name)
75        .args(args)
76        .status()
77        .map_err(|_| VxError::ToolNotFound {
78            tool_name: tool_name.to_string(),
79        })?;
80
81    Ok(status.code().unwrap_or(1))
82}