use anyhow::Context;
use forgejo_api::structs::Repository;
use crate::actions::GeneralArgs;
use crate::render::option::option_display;
use crate::render::spinner::spin_until_ready;
use crate::types::context::BergContext;
use crate::types::git::OwnerRepo;
use super::parse_owner_and_repo;
use clap::Parser;
#[derive(Parser, Debug)]
pub struct RepoInfoArgs {
#[arg(value_name = "OWNER/REPO")]
pub owner_and_repo: Option<String>,
}
impl RepoInfoArgs {
pub async fn run(self, general_args: GeneralArgs) -> anyhow::Result<()> {
let _ = general_args;
let ctx = BergContext::new(self).await?;
let (owner, repo) = ctx
.args
.owner_and_repo
.as_ref()
.and_then(|s| parse_owner_and_repo(s).ok())
.unwrap_or(
ctx.owner_repo()
.map(|OwnerRepo { repo, owner }| (owner, repo))?,
);
let repo_data = spin_until_ready(ctx.client.repo_get(owner.as_str(), repo.as_str()))
.await
.context("Current repo not found on this instance.")?;
present_repo_info(&ctx, repo_data);
Ok(())
}
}
fn present_repo_info(ctx: &BergContext<RepoInfoArgs>, repo_data: Repository) {
let mut table = ctx.make_table();
table
.set_header(vec!["Repository Info"])
.add_row(vec![
String::from("Repository Name"),
option_display(&repo_data.name),
])
.add_row(vec![
String::from("Repository Owner"),
option_display(
&repo_data
.owner
.as_ref()
.and_then(|user| user.login.as_ref()),
),
])
.add_row(vec![
String::from("Stars"),
format!("{}★", option_display(&repo_data.stars_count)),
]);
println!("{table}");
}