codeberg_cli/actions/repo/branch/
list.rs1use forgejo_api::structs::Branch;
2
3use crate::actions::GlobalArgs;
4use crate::render::json::JsonToStdout;
5use crate::render::option::option_display;
6use crate::render::spinner::spin_until_ready;
7use crate::types::context::BergContext;
8use crate::types::git::OwnerRepo;
9use crate::types::output::OutputMode;
10
11use clap::Parser;
12
13#[derive(Parser, Debug)]
15pub struct RepoBranchesArgs {}
16
17impl RepoBranchesArgs {
18 pub async fn run(self, global_args: GlobalArgs) -> anyhow::Result<()> {
19 let ctx = BergContext::new(self, global_args).await?;
20
21 let OwnerRepo { repo, owner } = ctx.owner_repo()?;
22
23 let (_, branches) = spin_until_ready(
24 ctx.client
25 .repo_list_branches(owner.as_str(), repo.as_str())
26 .send(),
27 )
28 .await?;
29
30 match ctx.global_args.output_mode {
31 OutputMode::Pretty => {
32 present_branches(&ctx, branches);
33 }
34 OutputMode::Json => branches.print_json()?,
35 }
36
37 Ok(())
38 }
39}
40
41fn present_branches(ctx: &BergContext<RepoBranchesArgs>, branches: Vec<Branch>) {
42 let mut table = ctx.make_table();
43
44 table
45 .set_header(vec!["Branch", "Commit"])
46 .add_rows(branches.into_iter().map(|branch| {
47 vec![
48 option_display(&branch.name),
49 option_display(&branch.commit.as_ref().and_then(|c| c.id.as_ref())),
50 ]
51 }));
52
53 println!("{table}", table = table.show());
54}