use clap::Parser;
use rocketmq_error::Result as CanonicalResult;
use crate::commands::CommandExecute;
use rocketmq_admin_core::client_adapter::services::controller::ControllerConfigQueryRequest;
use rocketmq_admin_core::client_adapter::services::controller::ControllerConfigQueryResult;
use rocketmq_admin_core::client_adapter::services::controller::ControllerService;
#[derive(Debug, Clone, Parser)]
pub struct GetControllerConfigSubCommand {
#[arg(
short = 'a',
long = "controllerAddress",
value_name = "HOST:PORT[;HOST:PORT...]",
required = true,
help = "Controller address list, eg: '192.168.0.1:9878;192.168.0.2:9878'"
)]
controller_address: String,
}
impl CommandExecute for GetControllerConfigSubCommand {
async fn execute(
&self,
credentials: Option<rocketmq_admin_core::core::security::AdminCredentials>,
client_runtime: std::sync::Arc<rocketmq_admin_core::client_adapter::ClientRuntime>,
) -> CanonicalResult<()> {
let result = ControllerService::query_controller_config_by_request_with_credentials(
self.request()?,
credentials,
client_runtime.clone(),
)
.await?;
Self::print_result(&result);
Ok(())
}
}
impl GetControllerConfigSubCommand {
fn request(&self) -> CanonicalResult<ControllerConfigQueryRequest> {
ControllerConfigQueryRequest::try_new(self.controller_address.clone())
}
fn print_result(result: &ControllerConfigQueryResult) {
for (controller_addr, config) in &result.controller_configs {
println!("============{}============", controller_addr);
for (key, value) in config {
println!("{:<50}= {}", key, value);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn get_controller_config_sub_command_parse_request() {
let cmd = GetControllerConfigSubCommand::try_parse_from([
"getControllerConfig",
"-a",
" 127.0.0.1:9878 ;127.0.0.2:9878 ",
])
.unwrap();
assert_eq!(
cmd.request()
.unwrap()
.controller_servers()
.iter()
.map(|addr| addr.as_str())
.collect::<Vec<_>>(),
vec!["127.0.0.1:9878", "127.0.0.2:9878"]
);
}
}