use crate::config::global::BGitGlobalConfig;
use crate::config::local::{StepFlags, WorkflowRules};
use dialoguer::{Confirm, Input};
use crate::rules::Rule;
use crate::{
bgit_error::{BGitError, BGitErrorWorkflowType},
events::{AtomicEvent, git_clone::GitClone},
rules::a01_git_install::IsGitInstalledLocally,
step::{PromptStep, Step},
};
pub(crate) struct CloneGitRepo {
name: String,
}
impl PromptStep for CloneGitRepo {
fn new() -> Self
where
Self: Sized,
{
CloneGitRepo {
name: "clone_repo".to_owned(),
}
}
fn get_name(&self) -> &str {
&self.name
}
fn execute(
&self,
_step_config_flags: Option<&StepFlags>,
workflow_rules_config: Option<&WorkflowRules>,
global_config: &BGitGlobalConfig,
) -> Result<Step, Box<BGitError>> {
let clone_link: String = Input::new()
.with_prompt("Enter the link to the repository you want to clone")
.interact()
.map_err(|e| {
Box::new(BGitError::new(
"Input Error",
&e.to_string(),
BGitErrorWorkflowType::ActionStep,
&self.name,
"",
"",
))
})?;
let suggest_shallow = clone_link.contains("large")
|| clone_link.contains("linux")
|| clone_link.contains("chromium");
let mut shallow_clone = false;
if suggest_shallow {
shallow_clone = Confirm::new()
.with_prompt("This repository might be large. Would you like to perform a shallow clone? (faster, but with limited history)")
.default(true)
.interact()
.map_err(|e| Box::new(BGitError::new("Input Error", &e.to_string(), BGitErrorWorkflowType::ActionStep, &self.name, "", "")))?;
}
let mut git_clone = GitClone::new(global_config);
git_clone.add_pre_check_rule(Box::new(IsGitInstalledLocally::new(workflow_rules_config)));
git_clone.set_url(&clone_link);
println!("Cloning repository from {clone_link}...");
println!("Destination: Current directory");
if shallow_clone {
println!("Performing shallow clone (limited history)");
}
match git_clone.execute() {
Ok(_) => {
println!("Git repository cloned successfully!");
Ok(Step::Stop)
}
Err(e) => Err(e),
}
}
}