Skip to main content

cellos_ctl/cmd/
delete.rs

1//! `cellctl delete formation <name>` — DELETE /v1/formations/<name>.
2
3use std::io::{self, BufRead, Write};
4
5use crate::client::CellosClient;
6use crate::exit::{CtlError, CtlResult};
7
8pub async fn formation(client: &CellosClient, name: &str, assume_yes: bool) -> CtlResult<()> {
9    if !assume_yes {
10        let confirmed = confirm(&format!("Delete formation '{name}'? This is irreversible."))?;
11        if !confirmed {
12            return Err(CtlError::usage("delete cancelled by user"));
13        }
14    }
15    client
16        .delete(&format!("/v1/formations/{}", urlencode(name)))
17        .await?;
18    println!("formation/{name} deleted");
19    Ok(())
20}
21
22fn confirm(prompt: &str) -> CtlResult<bool> {
23    eprint!("{prompt} [y/N] ");
24    io::stderr().flush().ok();
25    let mut line = String::new();
26    io::stdin().lock().read_line(&mut line)?;
27    let trimmed = line.trim().to_ascii_lowercase();
28    Ok(matches!(trimmed.as_str(), "y" | "yes"))
29}
30
31fn urlencode(s: &str) -> String {
32    url::form_urlencoded::byte_serialize(s.as_bytes()).collect()
33}