codeberg_cli/actions/repo/
info.rs1use anyhow::Context;
2use forgejo_api::structs::Repository;
3
4use crate::actions::GlobalArgs;
5use crate::render::json::JsonToStdout;
6use crate::render::option::option_display;
7use crate::render::spinner::spin_until_ready;
8use crate::types::context::BergContext;
9use crate::types::git::OwnerRepo;
10use crate::types::output::OutputMode;
11
12use clap::Parser;
13
14#[derive(Parser, Debug)]
16pub struct RepoInfoArgs {}
17
18impl RepoInfoArgs {
19 pub async fn run(self, global_args: GlobalArgs) -> anyhow::Result<()> {
20 let ctx = BergContext::new(self, global_args).await?;
21
22 let OwnerRepo { owner, repo } = ctx.owner_repo()?;
23 let repo_data = spin_until_ready(ctx.client.repo_get(owner.as_str(), repo.as_str()).send())
24 .await
25 .context("Current repo not found on this instance.")?;
26
27 match ctx.global_args.output_mode {
28 OutputMode::Pretty => {
29 present_repo_info(&ctx, repo_data);
30 }
31 OutputMode::Json => repo_data.print_json()?,
32 }
33
34 Ok(())
35 }
36}
37
38fn present_repo_info(ctx: &BergContext<RepoInfoArgs>, repo_data: Repository) {
39 let mut table = ctx.make_table();
40
41 table
42 .set_header(vec!["Repository Info"])
43 .add_row(vec![
44 String::from("Repository Name"),
45 option_display(&repo_data.name),
46 ])
47 .add_row(vec![
48 String::from("Repository Owner"),
49 option_display(
50 &repo_data
51 .owner
52 .as_ref()
53 .and_then(|user| user.login.as_ref()),
54 ),
55 ])
56 .add_row(vec![
57 String::from("Stars"),
58 format!("{}★", option_display(&repo_data.stars_count)),
59 ]);
60
61 println!("{table}", table = table.show());
62}