use anyhow::{Result, bail};
use ryra_core::Lifecycle;
use super::apply;
pub async fn run(service: Option<&str>, all: bool, action: Lifecycle, dry_run: bool) -> Result<()> {
let targets: Vec<String> = if all {
let installed = ryra_core::list_installed()?;
if installed.is_empty() {
println!("No services installed.");
return Ok(());
}
installed.into_iter().map(|s| s.name).collect()
} else {
match service {
Some(s) => vec![s.to_string()],
None => bail!("specify a service name or --all"),
}
};
let mut steps = Vec::new();
for name in &targets {
steps.extend(ryra_core::ops::plan_lifecycle(
&ryra_core::ops::LifecycleRequest {
service: name.to_string(),
action,
},
)?);
}
if dry_run {
super::print_dry_run(&steps);
return Ok(());
}
let verb = match action {
Lifecycle::Start => "Starting",
Lifecycle::Stop => "Stopping",
};
println!("{verb} {}...", targets.join(", "));
apply::execute_all(&steps).await?;
Ok(())
}