rocketmq-admin-cli 1.0.0

Command-line interface for managing RocketMQ-Rust and Apache RocketMQ clusters
// Copyright 2023 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 clap::Parser;
use rocketmq_admin_core::client_adapter::services::consumer::ConsumerService;
use rocketmq_admin_core::client_adapter::services::consumer::StartMonitoringRequest;

use crate::commands::CommandExecute;

#[derive(Debug, Clone, Parser)]
pub struct StartMonitoringSubCommand {
    #[arg(
        short = 'n',
        long = "namesrvAddr",
        required = false,
        help = "Name server address list, eg: '192.168.0.1:9876;192.168.0.2:9876'"
    )]
    namesrv_addr: Option<String>,
}

impl StartMonitoringSubCommand {
    fn request(&self) -> StartMonitoringRequest {
        StartMonitoringRequest::new(self.namesrv_addr.clone())
    }
}

impl CommandExecute for StartMonitoringSubCommand {
    async fn execute(
        &self,
        credentials: Option<rocketmq_admin_core::core::security::AdminCredentials>,
        client_runtime: std::sync::Arc<rocketmq_admin_core::client_adapter::ClientRuntime>,
    ) -> rocketmq_error::Result<()> {
        let result = ConsumerService::start_monitoring_by_request_with_credentials(
            self.request(),
            credentials,
            client_runtime.clone(),
        )
        .await?;
        for event in result.events {
            println!("{event:?}");
        }
        Ok(())
    }
}

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

    #[test]
    fn start_monitoring_sub_command_builds_request() {
        let command = StartMonitoringSubCommand::try_parse_from(["startMonitoring", "-n", " 127.0.0.1:9876 "]).unwrap();

        let request = command.request();

        assert_eq!(request.namesrv_addr(), Some("127.0.0.1:9876"));
    }
}