use super::{CommandExecutor, CommandOutput, DockerCommand};
use crate::error::Result;
use crate::stream::{OutputLine, StreamResult, StreamableCommand};
use async_trait::async_trait;
use tokio::process::Command as TokioCommand;
use tokio::sync::mpsc;
#[derive(Debug, Clone)]
pub struct LogsCommand {
container: String,
follow: bool,
timestamps: bool,
tail: Option<String>,
since: Option<String>,
until: Option<String>,
details: bool,
pub executor: CommandExecutor,
}
impl LogsCommand {
#[must_use]
pub fn new(container: impl Into<String>) -> Self {
Self {
container: container.into(),
follow: false,
timestamps: false,
tail: None,
since: None,
until: None,
details: false,
executor: CommandExecutor::new(),
}
}
#[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 all(mut self) -> Self {
self.tail = Some("all".to_string());
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 details(mut self) -> Self {
self.details = true;
self
}
pub async fn run(&self) -> Result<CommandOutput> {
self.execute().await
}
}
#[async_trait]
impl DockerCommand for LogsCommand {
type Output = CommandOutput;
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> {
let mut args = vec!["logs".to_string()];
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.details {
args.push("--details".to_string());
}
args.push(self.container.clone());
args.extend(self.executor.raw_args.clone());
args
}
async fn execute(&self) -> Result<Self::Output> {
let args = self.build_command_args();
self.execute_command(args).await
}
}
#[async_trait]
impl StreamableCommand for LogsCommand {
async fn stream<F>(&self, handler: F) -> Result<StreamResult>
where
F: FnMut(OutputLine) + Send + 'static,
{
let mut cmd = TokioCommand::new("docker");
cmd.arg("logs");
for arg in self.build_command_args() {
cmd.arg(arg);
}
crate::stream::stream_command(cmd, handler).await
}
async fn stream_channel(&self) -> Result<(mpsc::Receiver<OutputLine>, StreamResult)> {
let mut cmd = TokioCommand::new("docker");
cmd.arg("logs");
for arg in self.build_command_args() {
cmd.arg(arg);
}
crate::stream::stream_command_channel(cmd).await
}
}
impl LogsCommand {
pub async fn stream<F>(&self, handler: F) -> Result<StreamResult>
where
F: FnMut(OutputLine) + Send + 'static,
{
<Self as StreamableCommand>::stream(self, handler).await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_logs_basic() {
let cmd = LogsCommand::new("test-container");
let args = cmd.build_command_args();
assert_eq!(args, vec!["logs", "test-container"]);
}
#[test]
fn test_logs_follow() {
let cmd = LogsCommand::new("test-container").follow();
let args = cmd.build_command_args();
assert_eq!(args, vec!["logs", "--follow", "test-container"]);
}
#[test]
fn test_logs_with_tail() {
let cmd = LogsCommand::new("test-container").tail("100");
let args = cmd.build_command_args();
assert_eq!(args, vec!["logs", "--tail", "100", "test-container"]);
}
#[test]
fn test_logs_with_timestamps() {
let cmd = LogsCommand::new("test-container").timestamps();
let args = cmd.build_command_args();
assert_eq!(args, vec!["logs", "--timestamps", "test-container"]);
}
#[test]
fn test_logs_with_since() {
let cmd = LogsCommand::new("test-container").since("10m");
let args = cmd.build_command_args();
assert_eq!(args, vec!["logs", "--since", "10m", "test-container"]);
}
#[test]
fn test_logs_all_options() {
let cmd = LogsCommand::new("test-container")
.follow()
.timestamps()
.tail("50")
.since("1h")
.details();
let args = cmd.build_command_args();
assert_eq!(
args,
vec![
"logs",
"--follow",
"--timestamps",
"--tail",
"50",
"--since",
"1h",
"--details",
"test-container"
]
);
}
}