use clap::Parser;
use crate::commands::CommandExecute;
use crate::commands::CommonArgs;
use rocketmq_admin_core::client_adapter::services::topic::TopicListQueryRequest;
use rocketmq_admin_core::client_adapter::services::topic::TopicListResult;
use rocketmq_admin_core::client_adapter::services::topic::TopicService;
#[derive(Debug, Clone, Parser)]
pub struct TopicListSubCommand {
#[command(flatten)]
common_args: CommonArgs,
#[arg(
short = 'c',
long = "clusterName",
required = false,
help = "cluster whose topics are listed"
)]
cluster_name: Option<String>,
}
impl TopicListSubCommand {
fn request(&self) -> TopicListQueryRequest {
TopicListQueryRequest::new()
.with_optional_namesrv_addr(self.common_args.namesrv_addr.clone())
.with_optional_cluster_name(self.cluster_name.clone())
}
fn print_topics(&self, result: TopicListResult) {
if self.cluster_name.is_some() {
println!("#Cluster Name #Topic #Consumer Group");
for item in result.topics {
println!(
"{} {} {}",
item.cluster.as_ref().map(|value| value.as_str()).unwrap_or(""),
item.topic,
item.consumer_group.as_ref().map(|value| value.as_str()).unwrap_or("")
);
}
return;
}
for item in result.topics {
println!("{}", item.topic);
}
}
}
impl CommandExecute for TopicListSubCommand {
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 =
TopicService::query_topic_list_by_request_with_credentials(self.request(), credentials, client_runtime)
.await?;
self.print_topics(result);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn topic_list_sub_command_parse() {
let cmd =
TopicListSubCommand::try_parse_from(["topicList", "-c", "DefaultCluster", "-n", "127.0.0.1:9876"]).unwrap();
assert_eq!(cmd.cluster_name, Some("DefaultCluster".to_string()));
assert_eq!(cmd.common_args.namesrv_addr, Some("127.0.0.1:9876".to_string()));
}
}