Skip to content

Commit c20075f

Browse files
committed
refactor: move services to commands
1 parent e179962 commit c20075f

7 files changed

Lines changed: 12 additions & 21 deletions

File tree

cli/src/cli.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::Error;
22
use clap::{Parser, Subcommand};
33
use colored::*;
4-
use devmode::{services, CliError};
4+
use devmode::commands;
55

66
#[derive(Parser, Debug)]
77
#[clap(name = "Devmode")]
@@ -27,7 +27,7 @@ pub enum Commands {
2727
impl Cli {
2828
pub fn run(&self) -> Result<(), Error> {
2929
match &self.commands {
30-
Commands::Clone { url } => match services::clone(&url) {
30+
Commands::Clone { url } => match commands::clone::run(&url) {
3131
Ok(_) => {
3232
println!(
3333
"{} {}",
@@ -36,7 +36,7 @@ impl Cli {
3636
);
3737
Ok(())
3838
}
39-
Err(services::Error::Clone(services::CloneError::PathExists(path))) => {
39+
Err(commands::Error::Clone(commands::CloneError::PathExists(path))) => {
4040
if overwrite() {
4141
std::fs::remove_dir_all(&path)?;
4242
println!(
@@ -49,15 +49,15 @@ impl Cli {
4949
"info:".cyan().bold(),
5050
format!("Cloning {}...", url).cyan()
5151
);
52-
services::clone(&url)?;
52+
commands::clone::run(&url)?;
5353
println!(
5454
"{} {}",
5555
"success:".green().bold(),
5656
format!("Repository cloned to {}", path.display()).green()
5757
);
5858
Ok(())
5959
} else {
60-
Err(CliError::RepositoryExists.into())
60+
Err(devmode::CliError::RepositoryExists.into())
6161
}
6262
}
6363
Err(e) => Err(e.into()),
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
// Command module for 'clone'
12
use git2_credentials::CredentialHandler;
23
use git_url_parse::GitUrl;
34

45
use super::{CloneError, Error};
56

6-
pub fn clone(url: &str) -> Result<(), Error> {
7+
pub fn run(url: &str) -> Result<(), Error> {
78
let url = GitUrl::parse(url)?;
8-
99
let path = match (&url.host, &url.owner, &url.name) {
1010
(Some(host), Some(owner), name) if !owner.is_empty() => dirs::home_dir()
1111
.unwrap()
@@ -15,26 +15,19 @@ pub fn clone(url: &str) -> Result<(), Error> {
1515
.join(name),
1616
_ => return Err(CloneError::InvalidUrl.into()),
1717
};
18-
1918
if path.exists() {
2019
return Err(CloneError::PathExists(path).into());
2120
}
22-
2321
let mut cb = git2::RemoteCallbacks::new();
2422
let config = git2::Config::open_default()?;
2523
let mut credential_handler = CredentialHandler::new(config);
26-
2724
cb.credentials(move |url, username, allowed| {
2825
credential_handler.try_next_credential(url, username, allowed)
2926
});
30-
3127
let mut fetch_options = git2::FetchOptions::new();
3228
fetch_options.remote_callbacks(cb);
33-
3429
let mut builder = git2::build::RepoBuilder::new();
3530
builder.fetch_options(fetch_options);
36-
3731
builder.clone(url.to_string().as_str(), &path)?;
38-
3932
Ok(())
4033
}

cli/src/commands/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod clone;
2+
mod error;
3+
pub use error::*;

cli/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use thiserror::Error;
33
#[derive(Error, Debug)]
44
pub enum Error {
55
#[error("{0}")]
6-
Services(#[from] crate::services::Error),
6+
Commands(#[from] crate::commands::Error),
77
#[error("{0}")]
88
Parse(#[from] clap::Error),
99
#[error("{0}")]

cli/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
mod error;
22
pub use error::*;
33

4-
pub mod services;
4+
pub mod commands;

cli/src/services/mod.rs

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)