codeberg_cli/actions/label/
list.rs1use forgejo_api::structs::{IssueListLabelsQuery, Label};
2use miette::IntoDiagnostic;
3
4use crate::actions::GlobalArgs;
5use crate::render::json::JsonToStdout;
6use crate::render::option::option_display;
7use crate::render::spinner::spin_until_ready;
8
9use crate::types::context::BergContext;
10use crate::types::git::OwnerRepo;
11use crate::types::output::OutputMode;
12
13use clap::Parser;
14
15#[derive(Parser, Debug)]
17pub struct ListLabelsArgs {
18 #[arg(short, long, value_name = "N", default_value_t = 5)]
20 pub count: usize,
21}
22
23impl ListLabelsArgs {
24 pub async fn run(self, global_args: GlobalArgs) -> miette::Result<()> {
25 let ctx = BergContext::new(self, global_args).await?;
26
27 let OwnerRepo { repo, owner } = ctx.owner_repo()?;
28 let (_, labels_list) = spin_until_ready(
29 ctx.client
30 .issue_list_labels(
31 owner.as_str(),
32 repo.as_str(),
33 IssueListLabelsQuery::default(),
34 )
35 .send(),
36 )
37 .await
38 .into_diagnostic()?;
39
40 match ctx.global_args.output_mode {
41 OutputMode::Pretty => {
42 present_labels_list(&ctx, labels_list);
43 }
44 OutputMode::Json => labels_list.print_json()?,
45 }
46
47 Ok(())
48 }
49}
50
51fn present_labels_list(ctx: &BergContext<ListLabelsArgs>, labels: Vec<Label>) {
52 let labels_empty = labels.is_empty();
53
54 let table = ctx
55 .make_table()
56 .set_header(vec![format!(
57 "Labels{}",
58 if labels_empty {
59 " (empty)"
60 } else {
61 Default::default()
62 }
63 )])
64 .add_rows(
65 labels
66 .into_iter()
67 .map(|label| vec![option_display(&label.name)]),
68 );
69
70 println!("{table}", table = table.show());
71}