Skip to main content

frm/commands/
cli_cmd.rs

1// Copyright (c) 2025-2026 Michael S. Klishin and Contributors
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9#[cfg(unix)]
10use std::os::unix::process::CommandExt;
11#[cfg(windows)]
12use std::process;
13use std::process::Command;
14
15use crate::Result;
16use crate::common::cli_tools::RABBITMQ_CLI_TOOLS;
17use crate::errors::Error;
18use crate::paths::Paths;
19use crate::version::Version;
20
21#[cfg(unix)]
22pub fn run(paths: &Paths, version: &Version, tool: &str, args: &[String]) -> Result<()> {
23    if !paths.version_installed(version) {
24        return Err(Error::VersionNotInstalled(version.clone()));
25    }
26
27    if !RABBITMQ_CLI_TOOLS.contains(&tool) {
28        return Err(Error::UnknownTool(format!(
29            "'{}'. Valid tools: {}",
30            tool,
31            RABBITMQ_CLI_TOOLS.join(", ")
32        )));
33    }
34
35    let tool_path = paths.version_sbin_dir(version).join(tool);
36    if !tool_path.exists() {
37        return Err(Error::FileNotFound(tool_path.display().to_string()));
38    }
39
40    let err = Command::new(&tool_path).args(args).exec();
41
42    Err(Error::CommandFailed(format!(
43        "failed to execute {}: {}",
44        tool_path.display(),
45        err
46    )))
47}
48
49#[cfg(windows)]
50pub fn run(paths: &Paths, version: &Version, tool: &str, args: &[String]) -> Result<()> {
51    if !paths.version_installed(version) {
52        return Err(Error::VersionNotInstalled(version.clone()));
53    }
54
55    if !RABBITMQ_CLI_TOOLS.contains(&tool) {
56        return Err(Error::UnknownTool(format!(
57            "'{}'. Valid tools: {}",
58            tool,
59            RABBITMQ_CLI_TOOLS.join(", ")
60        )));
61    }
62
63    let tool_path = paths.version_sbin_dir(version).join(tool);
64    if !tool_path.exists() {
65        return Err(Error::FileNotFound(tool_path.display().to_string()));
66    }
67
68    let status = Command::new(&tool_path).args(args).status().map_err(|e| {
69        Error::CommandFailed(format!("failed to execute {}: {}", tool_path.display(), e))
70    })?;
71
72    process::exit(status.code().unwrap_or(1));
73}