Skip to content

Commit

Permalink
feat(info): Update info display output (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
fioncat authored Feb 29, 2024
1 parent f98f0a4 commit 9e0f59e
Show file tree
Hide file tree
Showing 7 changed files with 386 additions and 175 deletions.
4 changes: 4 additions & 0 deletions src/api/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ pub struct Alias {
}

impl Provider for Alias {
fn info(&self) -> Result<ProviderInfo> {
self.upstream.info()
}

fn list_repos(&self, owner: &str) -> Result<Vec<String>> {
let owner = self.alias_owner(owner);
let names = self.upstream.list_repos(owner)?;
Expand Down
4 changes: 4 additions & 0 deletions src/api/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ pub struct Cache {
}

impl Provider for Cache {
fn info(&self) -> Result<ProviderInfo> {
self.upstream.info()
}

fn list_repos(&self, owner: &str) -> Result<Vec<String>> {
let path = self.list_repos_path(owner);
if !self.force {
Expand Down
11 changes: 11 additions & 0 deletions src/api/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,17 @@ pub struct Github {
}

impl Provider for Github {
fn info(&self) -> Result<ProviderInfo> {
let auth = self.token.is_some();
let ping = self.execute_get_resp("").is_ok();

Ok(ProviderInfo {
name: format!("GitHub {}", Self::API_VERSION),
auth,
ping,
})
}

fn list_repos(&self, owner: &str) -> Result<Vec<String>> {
let path = format!("users/{owner}/repos?per_page={}", self.per_page);
let github_repos = self.execute_get::<Vec<Repo>>(&path)?;
Expand Down
11 changes: 11 additions & 0 deletions src/api/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ struct GitlabError {
}

impl Provider for Gitlab {
fn info(&self) -> Result<ProviderInfo> {
let auth = self.token.is_some();
let ping = self.execute_get_resp("projects").is_ok();

Ok(ProviderInfo {
name: format!("GitLab v{}", Gitlab::API_VERSION),
auth,
ping,
})
}

fn list_repos(&self, owner: &str) -> Result<Vec<String>> {
let owner_encode = urlencoding::encode(owner);
let path = format!("groups/{owner_encode}/projects?per_page={}", self.per_page);
Expand Down
25 changes: 25 additions & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ use crate::api::github::Github;
use crate::api::gitlab::Gitlab;
use crate::config::{Config, ProviderType, RemoteConfig};

#[derive(Debug, Serialize)]
pub struct ProviderInfo {
pub name: String,
pub auth: bool,
pub ping: bool,
}

impl Display for ProviderInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let auth = if self.auth { "with auth" } else { "no auth" };
let ping = if self.ping {
format!("ping {}", style("ok").green())
} else {
format!("ping {}", style("failed").red())
};
write!(f, "{}, {auth}, {ping}", self.name)
}
}

/// Represents repository information obtained from a [`Provider`].
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct ApiRepo {
Expand Down Expand Up @@ -208,6 +227,8 @@ impl ActionJobStatus {
/// cache layer. External components do not need to be concerned with the internal
/// implementation of the provider.
pub trait Provider {
fn info(&self) -> Result<ProviderInfo>;

/// Retrieve all repositories under a given owner.
fn list_repos(&self, owner: &str) -> Result<Vec<String>>;

Expand Down Expand Up @@ -336,6 +357,10 @@ pub mod api_tests {
}

impl Provider for StaticProvider {
fn info(&self) -> Result<ProviderInfo> {
todo!()
}

fn list_repos(&self, owner: &str) -> Result<Vec<String>> {
match self.repos.get(owner) {
Some(repos) => Ok(repos.clone()),
Expand Down
Loading

0 comments on commit 9e0f59e

Please sign in to comment.