use clap::Parser;
use rocketmq_admin_core::client_adapter::services::message::DumpCompactionLogRequest;
use rocketmq_admin_core::client_adapter::services::message::MessageService;
use rocketmq_error::Result as CanonicalResult;
use crate::commands::CommandExecute;
#[derive(Debug, Clone, Parser)]
pub struct DumpCompactionLogSubCommand {
#[arg(short = 'f', long = "file", required = false, help = "to dump file name")]
file: Option<String>,
}
impl DumpCompactionLogSubCommand {
fn request(&self) -> DumpCompactionLogRequest {
DumpCompactionLogRequest::try_new(self.file.clone())
}
}
impl CommandExecute for DumpCompactionLogSubCommand {
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 = MessageService::dump_compaction_log_by_request(&self.request())?;
if result.missing_file_name {
println!("miss dump log file name");
return Ok(());
}
for message in result.messages {
println!("{}", message);
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn dump_compaction_log_sub_command_builds_core_request() {
let command =
DumpCompactionLogSubCommand::try_parse_from(["dumpCompactionLog", "-f", " ./compact.log "]).unwrap();
let request = command.request();
assert_eq!(request.file().unwrap().to_string_lossy(), "./compact.log");
}
}