Skip to main content

cellos_ctl/cmd/
get.rs

1//! `cellctl get formations` / `cellctl get cells` — list resources.
2//!
3//! Each subcommand corresponds to **exactly one** HTTP GET (Feynman's rule from
4//! CHATROOM Session 16). Filters that the server understands (`?formation=`) are
5//! forwarded as query params; nothing is filtered client-side.
6
7use crate::client::CellosClient;
8use crate::exit::CtlResult;
9use crate::model::{Cell, Formation, List};
10use crate::output::{self, OutputFormat};
11
12pub async fn formations(client: &CellosClient, fmt: OutputFormat) -> CtlResult<()> {
13    let list: List<Formation> = client.get_json("/v1/formations").await?;
14    output::render_formations(&list.items, fmt);
15    Ok(())
16}
17
18pub async fn cells(
19    client: &CellosClient,
20    formation: Option<&str>,
21    fmt: OutputFormat,
22) -> CtlResult<()> {
23    let path = match formation {
24        Some(f) => format!("/v1/cells?formation={}", urlencode(f)),
25        None => "/v1/cells".to_string(),
26    };
27    let list: List<Cell> = client.get_json(&path).await?;
28    output::render_cells(&list.items, fmt);
29    Ok(())
30}
31
32fn urlencode(s: &str) -> String {
33    url::form_urlencoded::byte_serialize(s.as_bytes()).collect()
34}