use std::future::Future; use serde::Deserialize; use topcoat::context::Cx; use topcoat::router::{content::Form, href, page}; use topcoat::view::{View, ViewExt, view}; use topcoat::Result; use crate::deploy::{compose_hub, compose_satellite, ComposeAction}; use crate::ui::app; #[derive(Debug, Deserialize)] pub struct ComposeForm { pub target: String, #[serde(default)] pub name: String, pub action: String, } fn parse_action(s: &str) -> anyhow::Result { match s { "start" => Ok(ComposeAction::Start), "stop" => Ok(ComposeAction::Stop), "restart" => Ok(ComposeAction::Restart), other => anyhow::bail!("unknown compose action: {other}"), } } #[page(POST)] pub async fn action(cx: &Cx, Form(form): Form) -> Result> { let cfg = app(cx).config_snapshot(); let target = form.target.clone(); let name = form.name.clone(); let action = form.action.clone(); let result = tokio::task::spawn_blocking(move || -> anyhow::Result { let act = parse_action(&action)?; match target.as_str() { "hub" => { compose_hub(&cfg, act)?; Ok(format!("Hub: docker compose {}", act.as_str())) } "satellite" => { if name.is_empty() { anyhow::bail!("name required for target=satellite"); } compose_satellite(&cfg, &name, act)?; Ok(format!( "Satellite {name}: docker compose {}", act.as_str() )) } other => anyhow::bail!("unknown target: {other}"), } }) .await .map_err(|e| topcoat::Error::msg(e.to_string()))?; match result { Ok(msg) => Ok(view! {

"Overview" "/" "Compose"

"Compose"

(msg.clone())
} .boxed()), Err(e) => Ok(view! {

"Compose failed"

(format!("{e:#}"))
} .boxed()), } }