use serde::Deserialize; use anyhow::{Context, Result}; use crate::config::Config; use crate::ssh; #[derive(Debug, Clone, Deserialize)] pub struct ComposeService { #[serde(default, alias = "Service")] pub service: String, #[serde(default, alias = "Name")] pub name: String, #[serde(default, alias = "State")] pub state: String, #[serde(default, alias = "Status")] pub status: String, #[serde(default, alias = "Health")] pub health: String, } impl ComposeService { pub fn display_name(&self) -> &str { if !self.service.is_empty() { &self.service } else { &self.name } } pub fn is_running(&self) -> bool { self.state.eq_ignore_ascii_case("running") } pub fn health_label(&self) -> &str { if self.health.is_empty() { "—" } else { &self.health } } } pub fn parse_compose_ps(raw: &str) -> Result> { let trimmed = raw.trim(); if trimmed.is_empty() { return Ok(Vec::new()); } if trimmed.starts_with('[') { return serde_json::from_str(trimmed).context("parse docker compose ps (array)"); } let mut services = Vec::new(); for line in trimmed.lines() { let line = line.trim(); if line.is_empty() { continue; } let svc: ComposeService = serde_json::from_str(line).with_context(|| format!("parse compose ps line: {line}"))?; services.push(svc); } Ok(services) } fn compose_ps_cmd(path: &str) -> String { format!("cd {path} && docker compose ps --format json") } pub fn status_hub_data(cfg: &Config) -> Result> { let out = ssh::ssh_capture( &cfg.hub.user, &cfg.hub.host, &compose_ps_cmd(&cfg.hub.path), )?; parse_compose_ps(&out) } pub fn status_satellite_data(cfg: &Config, name: &str) -> Result> { let sat = cfg.satellite(name)?; let out = ssh::ssh_capture(&sat.user, &sat.host, &compose_ps_cmd(&sat.path))?; parse_compose_ps(&out) } pub fn format_services_table(services: &[ComposeService]) -> String { if services.is_empty() { return "(no services)".to_string(); } let mut lines = Vec::with_capacity(services.len() + 1); lines.push(format!( "{:<20} {:<12} {:<10} {}", "SERVICE", "STATE", "HEALTH", "STATUS" )); for s in services { lines.push(format!( "{:<20} {:<12} {:<10} {}", s.display_name(), s.state, s.health_label(), s.status )); } lines.join("\n") }