corge/command/
clone.rs

1use std::path::PathBuf;
2use std::process::Command;
3use crate::cli::{CloneArgs, CloneSource};
4use crate::std_command_ext::ExecuteCommand;
5use crate::tool::dir_copier::deep_copy;
6
7fn clone_git(url: String, branch: String, destination: PathBuf) -> anyhow::Result<()> {
8    if destination.exists() {
9        std::fs::remove_dir_all(&destination)?;
10    }
11
12    let mut command = Command::new("git");
13    command.arg("clone");
14    command.arg(url);
15    command.arg("--single-branch");
16    command.arg("--branch");
17    command.arg(branch);
18    command.arg(destination);
19
20    command.execute(true)?;
21
22    Ok(())
23}
24
25fn clone_file_system(from_path: PathBuf, destination: PathBuf) -> anyhow::Result<()> {
26    deep_copy(from_path, destination)?;
27
28    Ok(())
29}
30
31pub fn clone(clone_args: CloneArgs) -> anyhow::Result<()> {
32    log::info!("Cloning project from {:?} to {:?}", clone_args.source, clone_args.path);
33
34    match clone_args.source {
35        CloneSource::Git { url, branch } => clone_git(url, branch, clone_args.path)?,
36        CloneSource::FileSystem { from } => clone_file_system(from, clone_args.path)?,
37    }
38
39    log::info!("PROJECT CLONED SUCCESSFULLY");
40    Ok(())
41}