Skip to content

Commit

Permalink
Implement display for provider
Browse files Browse the repository at this point in the history
  • Loading branch information
orf committed Oct 27, 2019
1 parent 1b00289 commit feca91d
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 28 deletions.
7 changes: 7 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use failure::{Error, ResultExt};
use serde::{Deserialize, Serialize};
use std::fmt;
use std::fs;
use std::path::PathBuf;
use structopt::StructOpt;
Expand Down Expand Up @@ -69,3 +70,9 @@ impl ProviderSource {
Ok(self.provider().fetch_repositories()?)
}
}

impl fmt::Display for ProviderSource {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.provider())
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ fn add_provider_to_config(
if sources.iter().any(|s| s == &provider_source) {
println!("Entry already exists, skipping");
} else {
println!("Adding {} to workspace.toml", provider_source);
// Push the provider into the source and write it to the configuration file
sources.push(provider_source);
config.write(sources).context("Error writing config file")?;
println!("Added entry to workspace.toml");
}
Ok(())
}
Expand Down
34 changes: 23 additions & 11 deletions src/providers/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,9 @@ use failure::{Error, ResultExt};
use graphql_client::{GraphQLQuery, Response};
use serde::{Deserialize, Serialize};
use std::env;
use std::fmt;
use structopt::StructOpt;

#[derive(Deserialize, Serialize, Debug, Eq, Ord, PartialEq, PartialOrd)]
#[serde(rename_all = "lowercase")]
#[derive(StructOpt)]
#[structopt(about = "Add a Github user or organization by name")]
pub struct GithubProvider {
pub name: String,
#[structopt(long = "path", default_value = "github")]
#[structopt(about = "Clone repositories to a specific base path")]
path: String,
}

// See https://github.com/graphql-rust/graphql-client/blob/master/graphql_client/tests/custom_scalars.rs#L6
type GitSSHRemote = String;

Expand All @@ -29,6 +19,28 @@ type GitSSHRemote = String;
)]
pub struct Repositories;

#[derive(Deserialize, Serialize, Debug, Eq, Ord, PartialEq, PartialOrd)]
#[serde(rename_all = "lowercase")]
#[derive(StructOpt)]
#[structopt(about = "Add a Github user or organization by name")]
pub struct GithubProvider {
pub name: String,
#[structopt(long = "path", default_value = "github/")]
#[structopt(about = "Clone repositories to a specific base path")]
path: String,
}

impl fmt::Display for GithubProvider {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"Github user/org {} in path {}",
style(&self.name).green(),
style(&self.path).green()
)
}
}

impl GithubProvider {
fn parse_repo(
&self,
Expand Down
59 changes: 44 additions & 15 deletions src/providers/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,31 @@ use failure::{Error, ResultExt};
use graphql_client::{GraphQLQuery, Response};
use serde::{Deserialize, Serialize};
use std::env;
use std::fmt;
use structopt::StructOpt;

// GraphQL queries we use to fetch user and group repositories.
// Right now, annoyingly, Gitlab has a bug around GraphQL pagination:
// https://gitlab.com/gitlab-org/gitlab/issues/33419
// So, we don't paginate at all in these queries. I'll fix this once
// the issue is closed.

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "src/providers/graphql/gitlab/schema.json",
query_path = "src/providers/graphql/gitlab/projects.graphql",
response_derives = "Debug"
)]
pub struct UserRepositories;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "src/providers/graphql/gitlab/schema.json",
query_path = "src/providers/graphql/gitlab/projects.graphql",
response_derives = "Debug"
)]
pub struct GroupRepositories;

static DEFAULT_GITLAB_URL: &str = "https://gitlab.com";

fn public_gitlab_url() -> String {
Expand Down Expand Up @@ -34,27 +57,33 @@ pub enum GitlabProvider {
#[serde(default = "public_gitlab_url")]
#[structopt(long = "url", default_value = DEFAULT_GITLAB_URL)]
url: String,
#[structopt(long = "path", default_value = "gitlab")]
#[structopt(long = "path", default_value = "gitlab/")]
#[structopt(about = "Clone repositories to a specific base path")]
path: String,
},
}

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "src/providers/graphql/gitlab/schema.json",
query_path = "src/providers/graphql/gitlab/projects.graphql",
response_derives = "Debug"
)]
pub struct UserRepositories;
impl fmt::Display for GitlabProvider {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self {
GitlabProvider::User { user, url, path } => write!(
f,
"Gitlab user {} at {} in path {}",
style(user).green(),
style(url).green(),
style(path).green()
),
GitlabProvider::Group { group, url, path } => write!(
f,
"Gitlab group {} at {} in path {}",
style(group).green(),
style(url).green(),
style(path).green()
),
}
}
}

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "src/providers/graphql/gitlab/schema.json",
query_path = "src/providers/graphql/gitlab/projects.graphql",
response_derives = "Debug"
)]
pub struct GroupRepositories;
impl GitlabProvider {
/*
Duplicating these two methods is madness, but I don't know enough about rust to make them generic.
Expand Down
4 changes: 3 additions & 1 deletion src/providers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
mod github;
mod gitlab;

use crate::repository::Repository;
use failure::Error;
pub use github::GithubProvider;
pub use gitlab::GitlabProvider;
use std::fmt;

pub trait Provider {
pub trait Provider: fmt::Display {
/// Returns true if the provider should work, otherwise prints an error and return false
fn correctly_configured(&self) -> bool;
fn fetch_repositories(&self) -> Result<Vec<Repository>, Error>;
Expand Down

0 comments on commit feca91d

Please sign in to comment.