use std::{env, error::Error, fs, path::Path, process};
pub mod create;
pub struct Config {
pub file_path: String,
}
impl Config {
pub fn build(args: &[String]) -> Result<Config, Box<dyn Error>> {
let file_path;
if args.len() < 3 {
if let Ok(path) = env::current_dir() {
if path.join(".gor").exists() {
Err(Box::new(std::io::Error::new(
std::io::ErrorKind::AlreadyExists,
format!(
"{} is already a gor repository",
path.to_str().unwrap()
),
)))
} else {
file_path = path.to_str().unwrap().to_string();
Ok(Config { file_path })
}
} else {
Err(Box::new(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Error getting current directory",
)))
}
} else {
let target_path = Path::new(&args[2]);
if !target_path.exists() {
println!("1");
fs::create_dir_all(&args[2])?;
file_path = env::current_dir()
.unwrap()
.join(target_path)
.to_str()
.unwrap()
.to_string();
Ok(Config { file_path })
} else if !target_path.is_dir() {
Err(Box::new(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!("{} is not a directory", target_path.to_str().unwrap()),
)))
} else if target_path.join(".gor").exists() {
Err(Box::new(std::io::Error::new(
std::io::ErrorKind::AlreadyExists,
format!(
"{} is already a gor repository",
target_path.to_str().unwrap()
),
)))
} else {
println!("2");
file_path = env::current_dir()
.unwrap()
.join(target_path)
.to_str()
.unwrap()
.to_string();
Ok(Config { file_path })
}
}
}
}
pub fn run(args: &Vec<String>){
let config = Config::build(&args).unwrap_or_else(|err| {
println!("{err}");
process::exit(1)
});
if let Err(e) = create::repo(&config) {
println!("Application error: {}", e);
process::exit(1);
}
}