use crate::command::{CommandExecutor, ComposeCommand, ComposeConfig, DockerCommand};
use crate::error::Result;
use async_trait::async_trait;
use serde::Deserialize;
#[derive(Debug, Clone, Default)]
pub struct ComposeLsCommand {
pub executor: CommandExecutor,
pub config: ComposeConfig,
pub all: bool,
pub filter: Option<String>,
pub format: Option<LsFormat>,
pub quiet: bool,
}
#[derive(Debug, Clone, Copy)]
pub enum LsFormat {
Table,
Json,
}
impl LsFormat {
#[must_use]
pub fn as_arg(&self) -> &str {
match self {
Self::Table => "table",
Self::Json => "json",
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ComposeProject {
pub name: String,
pub status: String,
#[serde(default)]
pub config_files: String,
#[serde(default)]
pub created: String,
}
#[derive(Debug, Clone)]
pub struct LsResult {
pub projects: Vec<ComposeProject>,
pub raw_output: String,
}
impl ComposeLsCommand {
#[must_use]
pub fn new() -> Self {
Self {
executor: CommandExecutor::new(),
config: ComposeConfig::new(),
..Default::default()
}
}
#[must_use]
pub fn all(mut self) -> Self {
self.all = true;
self
}
#[must_use]
pub fn filter(mut self, filter: impl Into<String>) -> Self {
self.filter = Some(filter.into());
self
}
#[must_use]
pub fn format(mut self, format: LsFormat) -> Self {
self.format = Some(format);
self
}
#[must_use]
pub fn format_json(mut self) -> Self {
self.format = Some(LsFormat::Json);
self
}
#[must_use]
pub fn quiet(mut self) -> Self {
self.quiet = true;
self
}
}
#[async_trait]
impl DockerCommand for ComposeLsCommand {
type Output = LsResult;
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 projects = if matches!(self.format, Some(LsFormat::Json)) {
serde_json::from_str(&output.stdout).unwrap_or_default()
} else {
Vec::new()
};
Ok(LsResult {
projects,
raw_output: output.stdout,
})
}
}
impl ComposeCommand for ComposeLsCommand {
fn get_config(&self) -> &ComposeConfig {
&self.config
}
fn get_config_mut(&mut self) -> &mut ComposeConfig {
&mut self.config
}
fn subcommand(&self) -> &'static str {
"ls"
}
fn build_subcommand_args(&self) -> Vec<String> {
let mut args = Vec::new();
if self.all {
args.push("--all".to_string());
}
if self.quiet {
args.push("--quiet".to_string());
}
if let Some(filter) = &self.filter {
args.push("--filter".to_string());
args.push(filter.clone());
}
if let Some(format) = &self.format {
args.push("--format".to_string());
args.push(format.as_arg().to_string());
}
args
}
}
impl LsResult {
#[must_use]
pub fn project_names(&self) -> Vec<String> {
self.projects.iter().map(|p| p.name.clone()).collect()
}
#[must_use]
pub fn has_project(&self, name: &str) -> bool {
self.projects.iter().any(|p| p.name == name)
}
#[must_use]
pub fn running_projects(&self) -> Vec<&ComposeProject> {
self.projects
.iter()
.filter(|p| p.status.contains("running"))
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ls_command_basic() {
let cmd = ComposeLsCommand::new();
let _args = cmd.build_subcommand_args();
let full_args = ComposeCommand::build_command_args(&cmd);
assert_eq!(full_args[0], "compose");
assert!(full_args.contains(&"ls".to_string()));
}
#[test]
fn test_ls_command_with_all() {
let cmd = ComposeLsCommand::new().all();
let args = cmd.build_subcommand_args();
assert!(args.contains(&"--all".to_string()));
}
#[test]
fn test_ls_command_with_format() {
let cmd = ComposeLsCommand::new().format_json();
let args = cmd.build_subcommand_args();
assert!(args.contains(&"--format".to_string()));
assert!(args.contains(&"json".to_string()));
}
#[test]
fn test_ls_command_with_filter() {
let cmd = ComposeLsCommand::new().filter("status=running").quiet();
let args = cmd.build_subcommand_args();
assert!(args.contains(&"--filter".to_string()));
assert!(args.contains(&"status=running".to_string()));
assert!(args.contains(&"--quiet".to_string()));
}
#[test]
fn test_compose_config_integration() {
let cmd = ComposeLsCommand::new()
.file("docker-compose.yml")
.project_name("my-project");
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()));
}
#[test]
fn test_ls_result_helpers() {
let result = LsResult {
projects: vec![
ComposeProject {
name: "web".to_string(),
status: "running(3)".to_string(),
config_files: "docker-compose.yml".to_string(),
created: "2025-08-23".to_string(),
},
ComposeProject {
name: "db".to_string(),
status: "exited(0)".to_string(),
config_files: "docker-compose.yml".to_string(),
created: "2025-08-23".to_string(),
},
],
raw_output: String::new(),
};
assert_eq!(result.project_names(), vec!["web", "db"]);
assert!(result.has_project("web"));
assert!(!result.has_project("cache"));
assert_eq!(result.running_projects().len(), 1);
}
}