Skip to main content

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
18impl RepoInfoArgs {
19    pub async fn run(self, global_args: GlobalArgs) -> miette::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            .into_diagnostic()
26            .context("Current repo not found on this instance.")?;
27
28        match ctx.global_args.output_mode {
29            OutputMode::Pretty => {
30                present_repo_info(&ctx, repo_data);
31            }
32            OutputMode::Json => repo_data.print_json()?,
33        }
34
35        Ok(())
36    }
37}
38
39fn present_repo_info(ctx: &BergContext<RepoInfoArgs>, repo_data: Repository) {
40    let table = ctx
41        .make_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("Visibility"),
58            option_display(&repo_data.private.map(
59                |private| {
60                    if private { "Private" } else { "Public" }
61                },
62            )),
63        ])
64        .add_row(vec![
65            String::from("Stars"),
66            format!("{}★", option_display(&repo_data.stars_count)),
67        ]);
68
69    println!("{table}", table = table.show());
70}