use std::io::{self, BufRead, Write};
use crate::client::CellosClient;
use crate::exit::{CtlError, CtlResult};
pub async fn formation(client: &CellosClient, name: &str, assume_yes: bool) -> CtlResult<()> {
if !assume_yes {
let confirmed = confirm(&format!("Delete formation '{name}'? This is irreversible."))?;
if !confirmed {
return Err(CtlError::usage("delete cancelled by user"));
}
}
client
.delete(&format!("/v1/formations/{}", urlencode(name)))
.await?;
println!("formation/{name} deleted");
Ok(())
}
fn confirm(prompt: &str) -> CtlResult<bool> {
eprint!("{prompt} [y/N] ");
io::stderr().flush().ok();
let mut line = String::new();
io::stdin().lock().read_line(&mut line)?;
let trimmed = line.trim().to_ascii_lowercase();
Ok(matches!(trimmed.as_str(), "y" | "yes"))
}
fn urlencode(s: &str) -> String {
url::form_urlencoded::byte_serialize(s.as_bytes()).collect()
}