Skip to content

Commit

Permalink
refactor(remote): move clone to top-level trait
Browse files Browse the repository at this point in the history
  • Loading branch information
benpueschel committed Jul 6, 2024
1 parent 90fa380 commit a92eae9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 52 deletions.
28 changes: 2 additions & 26 deletions src/remote/gitea.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,32 +82,8 @@ impl Remote for GiteaRemote {
.map_err(map_error)
}

async fn clone_repo(&self, name: &str, path: &str) -> Result<(), Error> {
let username = &self.config.username;
let clean_url = self
.config
.url
.replace("https://", "")
.replace("http://", "");
let url = match self.config.clone_protocol {
CloneProtocol::SSH => format!("git@{}:{}/{}.git", clean_url, username, name),
CloneProtocol::HTTPS => format!("{}/{}/{}.git", self.config.url, username, name),
};

let status = std::process::Command::new("git")
.arg("clone")
.arg(url)
.arg(path)
.status()?;

if !status.success() {
return Err(Error::new(
ErrorKind::Other,
format!("Failed to clone repository '{}'", name),
));
}

Ok(())
fn get_config(&self) -> &RemoteConfig {
&self.config
}
}

Expand Down
29 changes: 5 additions & 24 deletions src/remote/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use octocrab::{
};
use serde::{Deserialize, Serialize};

use super::{map_error, CloneProtocol, Commit, Remote, RemoteConfig, RepoCreateInfo, Repository};
use super::{map_error, Commit, Remote, RemoteConfig, RepoCreateInfo, Repository};

pub struct GitHubRemote {
config: RemoteConfig,
Expand All @@ -37,6 +37,10 @@ impl Remote for GitHubRemote {
}
}

fn get_config(&self) -> &RemoteConfig {
&self.config
}

async fn create_repo(&self, create_info: RepoCreateInfo) -> Result<String, Error> {
#[derive(Serialize, Deserialize)]
struct Request {
Expand Down Expand Up @@ -103,29 +107,6 @@ impl Remote for GitHubRemote {

Ok(())
}

async fn clone_repo(&self, name: &str, path: &str) -> Result<(), Error> {
let username = &self.config.username;
let url = match self.config.clone_protocol {
CloneProtocol::SSH => format!("git@github.com:{}/{}.git", &username, name),
CloneProtocol::HTTPS => format!("https://github.com/{}/{}.git", &username, name),
};

let status = std::process::Command::new("git")
.arg("clone")
.arg(url)
.arg(path)
.status()?;

if !status.success() {
return Err(Error::new(
ErrorKind::Other,
format!("Failed to clone repository '{}'", name),
));
}

Ok(())
}
}

impl GitHubRemote {
Expand Down
29 changes: 27 additions & 2 deletions src/remote/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub struct RepoCreateInfo {
}

#[async_trait]
pub trait Remote {
pub trait Remote: Sync {
/// Create a new remote with the given configuration.
async fn new(config: &RemoteConfig) -> Self
where
Expand All @@ -96,8 +96,33 @@ pub trait Remote {
/// Delete a repository.
/// Warning: Operation does not prompt for confirmation and is irreversible.
async fn delete_repo(&self, name: &str) -> Result<(), Error>;
/// Get the configuration of the remote.
fn get_config(&self) -> &RemoteConfig;
/// Clone a repository to the given path.
async fn clone_repo(&self, name: &str, path: &str) -> Result<(), Error>;
async fn clone_repo(&self, name: &str, path: &str) -> Result<(), Error> {
let config = self.get_config();
let username = &config.username;
let clean_url = config.url.replace("https://", "").replace("http://", "");
let url = match config.clone_protocol {
CloneProtocol::SSH => format!("git@{}:{}/{}.git", clean_url, username, name),
CloneProtocol::HTTPS => format!("{}/{}/{}.git", config.url, username, name),
};

let status = std::process::Command::new("git")
.arg("clone")
.arg(url)
.arg(path)
.status()?;

if !status.success() {
return Err(Error::new(
ErrorKind::Other,
format!("Failed to clone repository '{}'", name),
));
}

Ok(())
}
}

pub async fn create_remote(config: &RemoteConfig, provider: Provider) -> Box<dyn Remote> {
Expand Down

0 comments on commit a92eae9

Please sign in to comment.