use crate::command::{CommandExecutor, ComposeCommand, ComposeConfig, DockerCommand};
use crate::error::Result;
use async_trait::async_trait;
#[derive(Debug, Clone)]
#[allow(clippy::struct_excessive_bools)] pub struct ComposeLogsCommand {
pub executor: CommandExecutor,
pub config: ComposeConfig,
pub services: Vec<String>,
pub follow: bool,
pub timestamps: bool,
pub tail: Option<String>,
pub since: Option<String>,
pub until: Option<String>,
pub no_log_prefix: bool,
pub no_color: bool,
}
#[derive(Debug, Clone)]
pub struct ComposeLogsResult {
pub stdout: String,
pub stderr: String,
pub success: bool,
pub services: Vec<String>,
}
impl ComposeLogsCommand {
#[must_use]
pub fn new() -> Self {
Self {
executor: CommandExecutor::new(),
config: ComposeConfig::new(),
services: Vec::new(),
follow: false,
timestamps: false,
tail: None,
since: None,
until: None,
no_log_prefix: false,
no_color: false,
}
}
#[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
}
#[must_use]
pub fn follow(mut self) -> Self {
self.follow = true;
self
}
#[must_use]
pub fn timestamps(mut self) -> Self {
self.timestamps = true;
self
}
#[must_use]
pub fn tail(mut self, lines: impl Into<String>) -> Self {
self.tail = Some(lines.into());
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 no_log_prefix(mut self) -> Self {
self.no_log_prefix = true;
self
}
#[must_use]
pub fn no_color(mut self) -> Self {
self.no_color = true;
self
}
}
impl Default for ComposeLogsCommand {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl DockerCommand for ComposeLogsCommand {
type Output = ComposeLogsResult;
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?;
Ok(ComposeLogsResult {
stdout: output.stdout,
stderr: output.stderr,
success: output.success,
services: self.services.clone(),
})
}
}
impl ComposeCommand for ComposeLogsCommand {
fn get_config(&self) -> &ComposeConfig {
&self.config
}
fn get_config_mut(&mut self) -> &mut ComposeConfig {
&mut self.config
}
fn subcommand(&self) -> &'static str {
"logs"
}
fn build_subcommand_args(&self) -> Vec<String> {
let mut args = Vec::new();
if self.follow {
args.push("--follow".to_string());
}
if self.timestamps {
args.push("--timestamps".to_string());
}
if let Some(ref tail) = self.tail {
args.push("--tail".to_string());
args.push(tail.clone());
}
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());
}
if self.no_log_prefix {
args.push("--no-log-prefix".to_string());
}
if self.no_color {
args.push("--no-color".to_string());
}
args.extend(self.services.clone());
args
}
}
impl ComposeLogsResult {
#[must_use]
pub fn success(&self) -> bool {
self.success
}
#[must_use]
pub fn services(&self) -> &[String] {
&self.services
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_compose_logs_basic() {
let cmd = ComposeLogsCommand::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(&"logs".to_string()));
}
#[test]
fn test_compose_logs_follow() {
let cmd = ComposeLogsCommand::new().follow().timestamps();
let args = cmd.build_subcommand_args();
assert_eq!(args, vec!["--follow", "--timestamps"]);
}
#[test]
fn test_compose_logs_with_tail() {
let cmd = ComposeLogsCommand::new().tail("100").service("web");
let args = cmd.build_subcommand_args();
assert_eq!(args, vec!["--tail", "100", "web"]);
}
#[test]
fn test_compose_logs_with_services() {
let cmd = ComposeLogsCommand::new()
.services(vec!["web", "db"])
.follow();
let args = cmd.build_subcommand_args();
assert!(args.contains(&"--follow".to_string()));
assert!(args.contains(&"web".to_string()));
assert!(args.contains(&"db".to_string()));
}
#[test]
fn test_compose_logs_all_options() {
let cmd = ComposeLogsCommand::new()
.follow()
.timestamps()
.tail("50")
.since("2024-01-01T00:00:00")
.until("2024-01-02T00:00:00")
.no_color()
.no_log_prefix()
.service("web")
.service("db");
let args = cmd.build_subcommand_args();
assert!(args.contains(&"--follow".to_string()));
assert!(args.contains(&"--timestamps".to_string()));
assert!(args.contains(&"--tail".to_string()));
assert!(args.contains(&"50".to_string()));
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()));
assert!(args.contains(&"--no-color".to_string()));
assert!(args.contains(&"--no-log-prefix".to_string()));
assert!(args.contains(&"web".to_string()));
assert!(args.contains(&"db".to_string()));
}
#[test]
fn test_compose_config_integration() {
let cmd = ComposeLogsCommand::new()
.file("docker-compose.yml")
.project_name("my-project")
.follow()
.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(&"my-project".to_string()));
assert!(args.contains(&"--follow".to_string()));
assert!(args.contains(&"api".to_string()));
}
}