Skip to content

Commit

Permalink
Merge branch 'main' into feat/open-application
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/cmd/clone.rs
#	src/config.rs
  • Loading branch information
siketyan committed Nov 22, 2022
2 parents 2b7e229 + 9df2358 commit a02b262
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/cmd/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use tracing::info;

use crate::config::Config;
use crate::console::create_spinner;
use crate::git::CloneRepository;
use crate::path::Path;
use crate::root::Root;
use crate::url::Url;
Expand Down Expand Up @@ -51,7 +52,9 @@ impl Cmd {
.resolve(&url)
.and_then(|r| config.profiles.resolve(&r.profile));

let repo = Repository::clone(&url.to_string(), &path)?;
config.git.strategy.clone.clone_repository(url, &path)?;

let repo = Repository::open(&path)?;

tx.send(())?;
progress.await?;
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ use anyhow::Result;
use serde::Deserialize;

use crate::application::Applications;
use crate::git::Config as GitConfig;
use crate::profile::Profiles;
use crate::root::Root;
use crate::rule::Rules;

#[derive(Debug, Default, Deserialize)]
pub struct Config {
#[serde(default)]
pub git: GitConfig,
#[serde(default)]
pub profiles: Profiles,
#[serde(default)]
Expand Down
15 changes: 15 additions & 0 deletions src/git/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use serde::Deserialize;

use crate::git::strategy::Strategy;

#[derive(Debug, Default, Deserialize)]
pub struct StrategyConfig {
#[serde(default)]
pub clone: Strategy,
}

#[derive(Debug, Default, Deserialize)]
pub struct Config {
#[serde(default)]
pub strategy: StrategyConfig,
}
15 changes: 15 additions & 0 deletions src/git/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
mod config;
mod strategy;

pub use config::Config;

use std::path::Path;

use anyhow::Result;

pub trait CloneRepository {
fn clone_repository<U, P>(&self, url: U, path: P) -> Result<()>
where
U: ToString,
P: AsRef<Path>;
}
24 changes: 24 additions & 0 deletions src/git/strategy/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::path::Path;
use std::process::Command;

use tracing::debug;

use crate::git::CloneRepository;

pub struct Cli;

impl CloneRepository for Cli {
fn clone_repository<U, P>(&self, url: U, path: P) -> anyhow::Result<()>
where
U: ToString,
P: AsRef<Path>,
{
debug!("Cloning the repository using CLI strategy");

let _ = Command::new("git")
.args(["clone", &url.to_string(), path.as_ref().to_str().unwrap()])
.output()?;

Ok(())
}
}
22 changes: 22 additions & 0 deletions src/git/strategy/git2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::path::Path;

use git2::Repository;
use tracing::debug;

use crate::git::CloneRepository;

pub struct Git2;

impl CloneRepository for Git2 {
fn clone_repository<U, P>(&self, url: U, path: P) -> anyhow::Result<()>
where
U: ToString,
P: AsRef<Path>,
{
debug!("Cloning the repository using Git2 strategy");

let _ = Repository::clone(&url.to_string(), path)?;

Ok(())
}
}
30 changes: 30 additions & 0 deletions src/git/strategy/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
mod cli;
mod git2;

pub use {self::git2::Git2, cli::Cli};

use std::path::Path;

use serde::Deserialize;

use crate::git::CloneRepository;

#[derive(Debug, Default, Deserialize)]
pub enum Strategy {
#[default]
Cli,
Git2,
}

impl CloneRepository for Strategy {
fn clone_repository<U, P>(&self, url: U, path: P) -> anyhow::Result<()>
where
U: ToString,
P: AsRef<Path>,
{
match self {
Self::Cli => Cli.clone_repository(url, path),
Self::Git2 => Git2.clone_repository(url, path),
}
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod application;
mod cmd;
mod config;
mod console;
mod git;
mod path;
mod profile;
mod root;
Expand Down

0 comments on commit a02b262

Please sign in to comment.