use std::{
fs::{self, File},
path::PathBuf,
};
use rust_terminal::Terminal;
use clap::Parser;
use crate::{SolarError, Install};
#[derive(Parser, Clone)]
pub struct Init {
#[arg(short, long, default_value = ".")]
destination: PathBuf,
#[arg(short, long, default_value = "false")]
force_init: bool,
#[arg(short, long, default_value = "false")]
install_existing: bool,
}
impl Init {
pub fn run(&self) -> Result<(), SolarError> {
let no_existing_git_repo =
fs::exists(self.destination.join(PathBuf::from(".git"))).is_err();
if self.force_init || no_existing_git_repo {
Terminal::command().piped().run("git", vec!["init"])?;
}
File::create(&self.destination.join(PathBuf::from("README.md")))?;
if self.install_existing || no_existing_git_repo {
let installer: Install = Install::parse_from(vec![
"",
self.destination
.to_str()
.ok_or("Failed to convert destination path to string.")?,
]);
installer.run()?;
}
Ok(())
}
}