Skip to main content

ytcli/cli/
component.rs

1//! Components: the parts a queue splits its work by.
2//!
3//! Read-only. Components are configured once per queue by whoever owns it, and
4//! the question a command line has is what they are called — `--set
5//! components=…` takes the name, and without a listing that is a guess.
6
7use clap::Subcommand;
8
9use crate::cli::{Session, emit, report};
10use crate::exit::ExitCode;
11use crate::render::{Format, machine, queue as render};
12
13#[derive(Debug, Subcommand)]
14pub enum ComponentCommand {
15    /// List components, in one queue or in the whole organisation.
16    #[command(long_about = crate::cli::help::md(crate::cli::help::COMPONENT_LIST))]
17    List {
18        /// Only this queue's components, e.g. PROJ.
19        #[arg(long, short = 'q')]
20        queue: Option<String>,
21    },
22}
23
24pub async fn run(command: &ComponentCommand, session: &Session) -> ExitCode {
25    let client = match session.client() {
26        Ok(client) => client,
27        Err(code) => return code,
28    };
29
30    let ComponentCommand::List { queue } = command;
31    match client.components(queue.as_deref()).await {
32        Ok(components) => {
33            let rendered = match session.render.format {
34                Format::Text => Ok(render::components(
35                    &components,
36                    queue.as_deref(),
37                    &session.render,
38                )),
39                Format::JsonRaw => machine(&components, Format::Json),
40                other => machine(&components, other),
41            };
42            match rendered {
43                Ok(text) => {
44                    emit(&text);
45                    ExitCode::Success
46                }
47                Err(error) => report(&error, ExitCode::Failure),
48            }
49        }
50        Err(error) => {
51            let code = error.exit_code();
52            report(&error, code)
53        }
54    }
55}