use crate::command::{CommandExecutor, ComposeCommand, ComposeConfig, DockerCommand};
use crate::error::Result;
use async_trait::async_trait;
use serde::Deserialize;
#[derive(Debug, Clone)]
pub struct ComposeEventsCommand {
pub executor: CommandExecutor,
pub config: ComposeConfig,
pub json: bool,
pub since: Option<String>,
pub until: Option<String>,
pub services: Vec<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct ComposeEvent {
pub time: String,
#[serde(rename = "type")]
pub event_type: String,
pub action: String,
pub service: Option<String>,
pub container: Option<String>,
pub attributes: Option<serde_json::Value>,
}
#[derive(Debug, Clone)]
pub struct ComposeEventsResult {
pub stdout: String,
pub stderr: String,
pub success: bool,
pub events: Vec<ComposeEvent>,
pub services: Vec<String>,
}
impl ComposeEventsCommand {
#[must_use]
pub fn new() -> Self {
Self {
executor: CommandExecutor::new(),
config: ComposeConfig::new(),
json: false,
since: None,
until: None,
services: Vec::new(),
}
}
#[must_use]
pub fn json(mut self) -> Self {
self.json = true;
self
}
#[must_use]
pub fn since(mut self, timestamp: impl Into<String>) -> Self {
self.since = Some(timestamp.into());
self
}
#[must_use]
pub fn until(mut self, timestamp: impl Into<String>) -> Self {
self.until = Some(timestamp.into());
self
}
#[must_use]
pub fn service(mut self, service: impl Into<String>) -> Self {
self.services.push(service.into());
self
}
#[must_use]
pub fn services<I, S>(mut self, services: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.services.extend(services.into_iter().map(Into::into));
self
}
}
impl Default for ComposeEventsCommand {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl DockerCommand for ComposeEventsCommand {
type Output = ComposeEventsResult;
fn get_executor(&self) -> &CommandExecutor {
&self.executor
}
fn get_executor_mut(&mut self) -> &mut CommandExecutor {
&mut self.executor
}
fn build_command_args(&self) -> Vec<String> {
<Self as ComposeCommand>::build_command_args(self)
}
async fn execute(&self) -> Result<Self::Output> {
let args = <Self as ComposeCommand>::build_command_args(self);
let output = self.execute_command(args).await?;
let events = if self.json {
output
.stdout
.lines()
.filter_map(|line| {
if line.trim().is_empty() {
None
} else {
serde_json::from_str(line).ok()
}
})
.collect()
} else {
Vec::new()
};
Ok(ComposeEventsResult {
stdout: output.stdout,
stderr: output.stderr,
success: output.success,
events,
services: self.services.clone(),
})
}
}
impl ComposeCommand for ComposeEventsCommand {
fn get_config(&self) -> &ComposeConfig {
&self.config
}
fn get_config_mut(&mut self) -> &mut ComposeConfig {
&mut self.config
}
fn subcommand(&self) -> &'static str {
"events"
}
fn build_subcommand_args(&self) -> Vec<String> {
let mut args = Vec::new();
if self.json {
args.push("--json".to_string());
}
if let Some(ref since) = self.since {
args.push("--since".to_string());
args.push(since.clone());
}
if let Some(ref until) = self.until {
args.push("--until".to_string());
args.push(until.clone());
}
args.extend(self.services.clone());
args
}
}
impl ComposeEventsResult {
#[must_use]
pub fn success(&self) -> bool {
self.success
}
#[must_use]
pub fn events(&self) -> &[ComposeEvent] {
&self.events
}
#[must_use]
pub fn services(&self) -> &[String] {
&self.services
}
#[must_use]
pub fn events_for_service(&self, service: &str) -> Vec<&ComposeEvent> {
self.events
.iter()
.filter(|event| event.service.as_ref().is_some_and(|s| s == service))
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_compose_events_basic() {
let cmd = ComposeEventsCommand::new();
let args = cmd.build_subcommand_args();
assert!(args.is_empty());
let full_args = ComposeCommand::build_command_args(&cmd);
assert_eq!(full_args[0], "compose");
assert!(full_args.contains(&"events".to_string()));
}
#[test]
fn test_compose_events_with_json() {
let cmd = ComposeEventsCommand::new().json();
let args = cmd.build_subcommand_args();
assert!(args.contains(&"--json".to_string()));
}
#[test]
fn test_compose_events_with_timestamps() {
let cmd = ComposeEventsCommand::new()
.since("2024-01-01T00:00:00")
.until("2024-01-02T00:00:00");
let args = cmd.build_subcommand_args();
assert!(args.contains(&"--since".to_string()));
assert!(args.contains(&"2024-01-01T00:00:00".to_string()));
assert!(args.contains(&"--until".to_string()));
assert!(args.contains(&"2024-01-02T00:00:00".to_string()));
}
#[test]
fn test_compose_events_with_services() {
let cmd = ComposeEventsCommand::new().service("web").service("db");
let args = cmd.build_subcommand_args();
assert!(args.contains(&"web".to_string()));
assert!(args.contains(&"db".to_string()));
}
#[test]
fn test_compose_events_with_services_method() {
let cmd = ComposeEventsCommand::new().services(vec!["cache", "queue"]);
let args = cmd.build_subcommand_args();
assert!(args.contains(&"cache".to_string()));
assert!(args.contains(&"queue".to_string()));
}
#[test]
fn test_compose_events_all_options() {
let cmd = ComposeEventsCommand::new()
.json()
.since("2024-01-01")
.until("2024-01-02")
.services(vec!["web", "worker"]);
let args = cmd.build_subcommand_args();
assert!(args.contains(&"--json".to_string()));
assert!(args.contains(&"--since".to_string()));
assert!(args.contains(&"2024-01-01".to_string()));
assert!(args.contains(&"--until".to_string()));
assert!(args.contains(&"2024-01-02".to_string()));
assert!(args.contains(&"web".to_string()));
assert!(args.contains(&"worker".to_string()));
}
#[test]
fn test_compose_config_integration() {
let cmd = ComposeEventsCommand::new()
.file("docker-compose.yml")
.project_name("myapp")
.json()
.service("api");
let args = ComposeCommand::build_command_args(&cmd);
assert!(args.contains(&"--file".to_string()));
assert!(args.contains(&"docker-compose.yml".to_string()));
assert!(args.contains(&"--project-name".to_string()));
assert!(args.contains(&"myapp".to_string()));
assert!(args.contains(&"--json".to_string()));
assert!(args.contains(&"api".to_string()));
}
}