Skip to content

Commit

Permalink
perf resuse client && bump
Browse files Browse the repository at this point in the history
  • Loading branch information
Its-Just-Nans committed Jan 8, 2025
1 parent 13f9841 commit 981bb38
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 71 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "git-mover"
version = "1.0.4"
version = "1.0.6"
edition = "2021"
keywords = ["utility", "git", "cli"]
categories = ["command-line-utilities"]
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Move git repositories to a new location

[![asciicast](https://asciinema.org/a/Lfge8LlwsR9A2dKYMNT3v9Nh4.svg)](https://asciinema.org/a/Lfge8LlwsR9A2dKYMNT3v9Nh4)

## Usage

```sh
Expand Down
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ pub struct GitMoverCli {
#[arg(short, long = "no-forks")]
pub no_forks: bool,

/// Resync all repositories
#[arg(short, long)]
pub resync: bool,

/// Custom configuration file
#[arg(short, long)]
pub config: Option<String>,
Expand Down
31 changes: 19 additions & 12 deletions src/codeberg/platform.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Codeberg platform implementation
use reqwest::header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE};
use serde::{Deserialize, Serialize};
use std::pin::Pin;
use urlencoding::encode;

Expand All @@ -12,18 +11,25 @@ use crate::{
};

/// Codeberg platform
#[derive(Deserialize, Serialize, Default, Debug, Clone)]
#[derive(Default, Debug, Clone)]
pub struct CodebergPlatform {
/// Codeberg username
pub username: String,
username: String,
/// Codeberg token
pub token: String,
token: String,

/// Reqwest client
client: reqwest::Client,
}

impl CodebergPlatform {
/// Create a new codeberg platform
pub fn new(username: String, token: String) -> Self {
Self { username, token }
Self {
username,
token,
client: reqwest::Client::new(),
}
}
}

Expand All @@ -44,8 +50,8 @@ impl Platform for CodebergPlatform {
let repo_name = repo.name.to_string();
let description = repo.description.to_string();
let private = repo.private;
let client = self.client.clone();
Box::pin(async move {
let client = reqwest::Client::new();
let url = format!("https://{}/api/v1/user/repos", CODEBERG_URL);
let json_body = CodebergRepo {
name: repo_name.to_string(),
Expand All @@ -66,10 +72,11 @@ impl Platform for CodebergPlatform {
let text = response.text().await?;
let get_repo = match self.get_repo(repo_name.as_str()).await {
Ok(repo) => repo,
Err(_e) => {
Err(e) => {
let text_error = format!("{} - {:?}", &text, e);
return Err(GitMoverError::new(GitMoverErrorKind::RepoCreation)
.with_platform(PlatformType::Codeberg)
.with_text(&text));
.with_text(&text_error));
}
};
let json_body_as_repo = json_body.clone().into();
Expand All @@ -94,8 +101,8 @@ impl Platform for CodebergPlatform {
) -> Pin<Box<dyn std::future::Future<Output = Result<Repo, GitMoverError>> + Send + '_>> {
let token = self.token.clone();
let repo_name = repo_name.to_string();
let client = self.client.clone();
Box::pin(async move {
let client = reqwest::Client::new();
let url = format!(
"https://{}/api/v1/repos/{}/{}",
CODEBERG_URL,
Expand Down Expand Up @@ -127,8 +134,8 @@ impl Platform for CodebergPlatform {
) -> Pin<Box<dyn std::future::Future<Output = Result<(), GitMoverError>> + Send + '_>> {
let repo = repo.clone();
let token = self.token.clone();
let client = self.client.clone();
Box::pin(async move {
let client = reqwest::Client::new();
let url = format!(
"https://{}/api/v1/repos/{}/{}",
CODEBERG_URL,
Expand Down Expand Up @@ -164,8 +171,8 @@ impl Platform for CodebergPlatform {
&self,
) -> Pin<Box<dyn std::future::Future<Output = Result<Vec<Repo>, GitMoverError>> + Send>> {
let token = self.token.clone();
let client = self.client.clone();
Box::pin(async move {
let client = reqwest::Client::new();
let url = format!("https://{}/api/v1/user/repos", CODEBERG_URL);
let mut page: usize = 1;
let limit = 100;
Expand Down Expand Up @@ -205,8 +212,8 @@ impl Platform for CodebergPlatform {
) -> Pin<Box<dyn std::future::Future<Output = Result<(), GitMoverError>> + Send + '_>> {
let token = self.token.clone();
let name = name.to_string();
let client = self.client.clone();
Box::pin(async move {
let client = reqwest::Client::new();
let url = format!(
"https://{}/api/v1/repos/{}/{}",
CODEBERG_URL,
Expand Down
6 changes: 3 additions & 3 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ struct Inner {
/// Error kind.
kind: GitMoverErrorKind,

/// Source error.
source: Option<BoxError>,

/// Platform error
platform: Option<PlatformType>,

/// Source error.
source: Option<BoxError>,
}

#[derive(Debug)]
Expand Down
38 changes: 21 additions & 17 deletions src/github/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,31 @@ use crate::{
platform::{Platform, PlatformType},
utils::Repo,
};
use reqwest::{
header::{ACCEPT, AUTHORIZATION, USER_AGENT},
Client,
};
use serde::{Deserialize, Serialize};
use reqwest::header::{ACCEPT, AUTHORIZATION, USER_AGENT};
use std::pin::Pin;
use urlencoding::encode;

/// Github Platform
#[derive(Deserialize, Serialize, Default, Debug, Clone)]
#[derive(Default, Debug, Clone)]
pub struct GithubPlatform {
/// Github username
pub username: String,
username: String,

/// Github token
pub token: String,
token: String,

/// Reqwest client
client: reqwest::Client,
}

impl GithubPlatform {
/// Create a new GithubPlatform
pub fn new(username: String, token: String) -> Self {
Self { username, token }
pub(crate) fn new(username: String, token: String) -> Self {
Self {
username,
token,
client: reqwest::Client::new(),
}
}
}

Expand All @@ -46,8 +49,8 @@ impl Platform for GithubPlatform {
) -> Pin<Box<dyn std::future::Future<Output = Result<(), GitMoverError>> + Send + '_>> {
let token = self.token.clone();
let repo = repo.clone();
let client = self.client.clone();
Box::pin(async move {
let client = reqwest::Client::new();
let url = format!("https://{}/user/repos", GITHUB_API_URL);
let request = client
.post(&url)
Expand All @@ -63,10 +66,11 @@ impl Platform for GithubPlatform {
let text = response.text().await?;
let get_repo = match self.get_repo(repo.name.as_str()).await {
Ok(repo) => repo,
Err(_) => {
Err(e) => {
let text_error = format!("{} - {:?}", &text, e);
return Err(GitMoverError::new(GitMoverErrorKind::RepoCreation)
.with_platform(PlatformType::Github)
.with_text(&text))
.with_text(&text_error));
}
};
if get_repo != repo {
Expand All @@ -82,8 +86,8 @@ impl Platform for GithubPlatform {
repo: Repo,
) -> Pin<Box<dyn std::future::Future<Output = Result<(), GitMoverError>> + Send + '_>> {
let token = self.token.clone();
let client = self.client.clone();
Box::pin(async move {
let client = reqwest::Client::new();
let url = format!(
"https://{}/repos/{}/{}",
GITHUB_API_URL,
Expand Down Expand Up @@ -116,8 +120,8 @@ impl Platform for GithubPlatform {
let token = self.token.clone();
let username = self.username.clone();
let repo_name = repo_name.to_string();
let client = self.client.clone();
Box::pin(async move {
let client = Client::new();
let url = format!(
"https://{}/repos/{}/{}",
GITHUB_API_URL,
Expand Down Expand Up @@ -148,8 +152,8 @@ impl Platform for GithubPlatform {
&self,
) -> Pin<Box<dyn std::future::Future<Output = Result<Vec<Repo>, GitMoverError>> + Send>> {
let token = self.token.clone();
let client = self.client.clone();
Box::pin(async move {
let client = Client::new();
let url = &format!("https://{}/user/repos", GITHUB_API_URL);
let mut need_request = true;
let mut page: usize = 1;
Expand Down Expand Up @@ -194,8 +198,8 @@ impl Platform for GithubPlatform {
) -> Pin<Box<dyn std::future::Future<Output = Result<(), GitMoverError>> + Send + '_>> {
let token = self.token.clone();
let name = repo_name.to_string();
let client = self.client.clone();
Box::pin(async move {
let client = reqwest::Client::new();
let url = format!(
"https://{}/repos/{}/{}",
GITHUB_API_URL,
Expand Down
29 changes: 17 additions & 12 deletions src/gitlab/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::platform::PlatformType;
use crate::utils::Repo;
use reqwest::header::ACCEPT;
use reqwest::header::CONTENT_TYPE;
use serde::{Deserialize, Serialize};
use std::pin::Pin;
use urlencoding::encode;

Expand All @@ -15,19 +14,26 @@ use super::repo::GitlabRepoEdition;
use super::GITLAB_URL;

/// Gitlab platform
#[derive(Deserialize, Serialize, Default, Debug, Clone)]
#[derive(Default, Debug, Clone)]
pub struct GitlabPlatform {
/// Gitlab username
username: String,

/// Gitlab token
token: String,

/// Reqwest client
client: reqwest::Client,
}

impl GitlabPlatform {
/// Create a new Gitlab platform
pub fn new(username: String, token: String) -> Self {
Self { username, token }
Self {
username,
token,
client: reqwest::Client::new(),
}
}
}

Expand All @@ -46,8 +52,8 @@ impl Platform for GitlabPlatform {
) -> Pin<Box<dyn std::future::Future<Output = Result<(), GitMoverError>> + Send + '_>> {
let token = self.token.clone();
let repo = repo.clone();
let client = self.client.clone();
Box::pin(async move {
let client = reqwest::Client::new();
let url = format!("https://{}/api/v4/projects", GITLAB_URL);
let visibility = if repo.private { "private" } else { "public" };
let json_body = GitlabRepo {
Expand All @@ -69,10 +75,11 @@ impl Platform for GitlabPlatform {
let text = response.text().await?;
let get_repo = match self.get_repo(repo.name.as_str()).await {
Ok(repo) => repo,
Err(_e) => {
Err(e) => {
let text_error = format!("{} - {:?}", &text, e);
return Err(GitMoverError::new(GitMoverErrorKind::RepoCreation)
.with_platform(PlatformType::Gitlab)
.with_text(&text))
.with_text(&text_error));
}
};
let json_body_as_repo = json_body.into();
Expand All @@ -90,8 +97,8 @@ impl Platform for GitlabPlatform {
) -> Pin<Box<dyn std::future::Future<Output = Result<(), GitMoverError>> + Send + '_>> {
let token = self.token.clone();
let repo = repo.clone();
let client = self.client.clone();
Box::pin(async move {
let client = reqwest::Client::new();
let repo_url = format!("{}/{}", self.get_username(), repo.name);
let url = format!(
"https://{}/api/v4/projects/{}",
Expand Down Expand Up @@ -127,17 +134,15 @@ impl Platform for GitlabPlatform {
) -> Pin<Box<dyn std::future::Future<Output = Result<Repo, GitMoverError>> + Send>> {
let token = self.token.clone();
let name = name.to_string();
let client = self.client.clone();
Box::pin(async move {
let client = reqwest::Client::new();
let url = format!("https://{}/api/v4/projects", GITLAB_URL);
let request = client
.get(&url)
.header("PRIVATE-TOKEN", &token)
.query(&[("owned", "true"), ("search", name.as_str())])
.send();

let response = request.await?;

if !response.status().is_success() {
let text = response.text().await?;
return Err(GitMoverError::new(GitMoverErrorKind::GetRepo)
Expand All @@ -159,8 +164,8 @@ impl Platform for GitlabPlatform {
&self,
) -> Pin<Box<dyn std::future::Future<Output = Result<Vec<Repo>, GitMoverError>> + Send>> {
let token = self.token.clone();
let client = self.client.clone();
Box::pin(async move {
let client = reqwest::Client::new();
let url = format!("https://{}/api/v4/projects", GITLAB_URL);
let mut need_request = true;
let mut page: usize = 1;
Expand Down Expand Up @@ -207,8 +212,8 @@ impl Platform for GitlabPlatform {
) -> Pin<Box<dyn std::future::Future<Output = Result<(), GitMoverError>> + Send + '_>> {
let token = self.token.clone();
let name = name.to_string();
let client = self.client.clone();
Box::pin(async move {
let client = reqwest::Client::new();
let repo_url = format!("{}/{}", self.get_username(), name);
let url = format!(
"https://{}/api/v4/projects/{}",
Expand Down
Loading

0 comments on commit 981bb38

Please sign in to comment.