codeberg_cli/actions/repo/
info.rs

1use forgejo_api::structs::Repository;
2use miette::{Context, IntoDiagnostic};
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/// Display short summary of the current repository
15#[derive(Parser, Debug)]
16pub struct RepoInfoArgs {
17    /// Repository to be inspected
18    #[arg(value_name = "OWNER/REPO")]
19    owner_and_repo: Option<OwnerRepo>,
20}
21
22impl RepoInfoArgs {
23    pub async fn run(self, global_args: GlobalArgs) -> miette::Result<()> {
24        let ctx = BergContext::new(self, global_args).await?;
25
26        let OwnerRepo { owner, repo } =
27            ctx.args.owner_and_repo.clone().unwrap_or(ctx.owner_repo()?);
28        let repo_data = spin_until_ready(ctx.client.repo_get(owner.as_str(), repo.as_str()).send())
29            .await
30            .into_diagnostic()
31            .context("Current repo not found on this instance.")?;
32
33        match ctx.global_args.output_mode {
34            OutputMode::Pretty => {
35                present_repo_info(&ctx, repo_data);
36            }
37            OutputMode::Json => repo_data.print_json()?,
38        }
39
40        Ok(())
41    }
42}
43
44fn present_repo_info(ctx: &BergContext<RepoInfoArgs>, repo_data: Repository) {
45    let table = ctx
46        .make_table()
47        .set_header(vec!["Repository Info"])
48        .add_row(vec![
49            String::from("Repository Name"),
50            option_display(&repo_data.name),
51        ])
52        .add_row(vec![
53            String::from("Repository Owner"),
54            option_display(
55                &repo_data
56                    .owner
57                    .as_ref()
58                    .and_then(|user| user.login.as_ref()),
59            ),
60        ])
61        .add_row(vec![
62            String::from("Visibility"),
63            option_display(&repo_data.private.map(
64                |private| {
65                    if private { "Private" } else { "Public" }
66                },
67            )),
68        ])
69        .add_row(vec![
70            String::from("Stars"),
71            format!("{}★", option_display(&repo_data.stars_count)),
72        ]);
73
74    println!("{table}", table = table.show());
75}