rocketmq-admin-cli 1.0.0

Command-line interface for managing RocketMQ-Rust and Apache RocketMQ clusters
// Copyright 2026 The RocketMQ Rust Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::commands::CommandExecute;
use clap::Parser;
use rocketmq_admin_core::client_adapter::services::controller::ControllerConfigUpdateRequest;
use rocketmq_admin_core::client_adapter::services::controller::ControllerService;
use rocketmq_error::Result as CanonicalResult;

#[derive(Debug, Clone, Parser)]
pub struct UpdateControllerConfigSubCommand {
    #[arg(
        short = 'a',
        long = "controllerAddress",
        value_name = "HOST:PORT",
        required = true,
        help = "Address of the controller to query"
    )]
    controller_address: String,

    #[arg(short = 'k', long = "key", required = true, help = "config key")]
    key: String,

    #[arg(short = 'v', long = "value", required = true, help = "config value")]
    value: String,
}

impl CommandExecute for UpdateControllerConfigSubCommand {
    async fn execute(
        &self,
        credentials: Option<rocketmq_admin_core::core::security::AdminCredentials>,
        client_runtime: std::sync::Arc<rocketmq_admin_core::client_adapter::ClientRuntime>,
    ) -> CanonicalResult<()> {
        ControllerService::update_controller_config_by_request_with_credentials(
            self.request()?,
            credentials,
            client_runtime.clone(),
        )
        .await?;
        println!("updated controller config successfully");
        Ok(())
    }
}

impl UpdateControllerConfigSubCommand {
    fn request(&self) -> CanonicalResult<ControllerConfigUpdateRequest> {
        ControllerConfigUpdateRequest::try_new(self.controller_address.clone(), self.key.clone(), self.value.clone())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn update_controller_config_sub_command_builds_core_request() {
        let cmd = UpdateControllerConfigSubCommand::try_parse_from([
            "updateControllerConfig",
            "-a",
            " 127.0.0.1:9878 ;127.0.0.2:9878 ",
            "-k",
            " maxElectTimeMs ",
            "-v",
            " 5000 ",
        ])
        .unwrap();

        let request = cmd.request().unwrap();
        assert_eq!(
            request
                .controller_servers()
                .iter()
                .map(|addr| addr.as_str())
                .collect::<Vec<_>>(),
            vec!["127.0.0.1:9878", "127.0.0.2:9878"]
        );
        assert_eq!(request.properties().get("maxElectTimeMs").unwrap().as_str(), "5000");
    }
}