vx_cli/commands/
execute.rs

1//! Execute command implementation
2
3use crate::ui::UI;
4use vx_core::{PluginRegistry, Result, ToolContext, 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
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    // Create execution context
30    let mut context = ToolContext::new(
31        tool_name.to_string(),
32        "latest".to_string(), // Default version for now
33        args.to_vec(),
34    );
35    context.use_system_path = use_system_path;
36    context.working_directory = std::env::current_dir().ok();
37
38    // Check if tool is supported by vx
39    if !use_system_path && registry.supports_tool(tool_name) {
40        // Try to get the tool from registry
41        if let Some(tool) = registry.get_tool(tool_name) {
42            UI::debug(&format!("Using vx-managed tool: {}", tool_name));
43
44            // Execute using the tool's implementation
45            let result = tool.execute(args, &context).await?;
46            return Ok(result.exit_code);
47        }
48    }
49
50    // Fall back to system PATH execution
51    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}