use crate::utils;
use anyhow::{anyhow, Result};
use clap::Parser;
use colored::Colorize;
use std::{
fs,
path::{Path, PathBuf},
process,
};
const TEMPLATE_REPO: &str = "https://github.com/gear-foundation/dapp-template-generate.git";
#[derive(Parser, Clone)]
pub enum Command {
New {
path: PathBuf,
},
}
impl Command {
pub fn run(self) -> Result<()> {
match self {
Command::New { path } => git_clone(TEMPLATE_REPO, path),
}
}
}
fn git_clone(repo: &str, target: PathBuf) -> Result<()> {
let path = target
.as_os_str()
.to_str()
.ok_or(anyhow!("Failed to convert target path to string"))?;
utils::info("Cloning", repo);
let result = process::Command::new("git")
.args(["clone", repo, path, "--depth=1"])
.output()
.map_err(|e| anyhow!("Failed to download template: {}", e))?;
if !result.status.success() {
utils::error(&result.stderr);
}
fs::remove_dir_all(target.join(".git"))?;
process::Command::new("git")
.args(["init", path])
.output()
.map_err(|e| anyhow!("Failed to init git: {}", e))?;
if !result.status.success() {
utils::error(&result.stderr);
}
utils::info("Created", path);
Ok(())
}