use crate::{
args::Args,
config::user::UserConfig,
git::{clone_into_folder, parse_to_git_url},
};
use anyhow::Result;
use std::{env, fs, path::PathBuf, process::exit};
#[allow(clippy::or_fun_call)]
pub fn run(args: &Args, user_config: &UserConfig) -> Result<PathBuf> {
let repository_url = args
.profile
.as_ref()
.map(|profile_name| {
if let Some(profiles) = &user_config.profiles {
profiles.get(profile_name.as_str())
} else {
None
}
})
.flatten()
.or(args.git.as_deref().as_ref())
.map(|&url| parse_to_git_url(url));
if let Some(repository_url) = repository_url {
let repository_dir_path = env::current_dir()?.join(&args.name.as_ref().unwrap());
if repository_dir_path.exists() {
if args.force {
fs::remove_dir_all(&repository_dir_path)?;
} else {
eprintln!(
"A folder named {:?} already exists! Use `--force` or another project name.",
&args.name.as_ref().unwrap()
);
exit(1);
}
}
clone_into_folder(&repository_url, &args.branch.as_ref(), &repository_dir_path)?;
Ok(repository_dir_path)
} else {
unreachable!();
}
}