1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
use clap::{ App, Arg, }; use std::{ fs::{ DirBuilder, File, }, io::{ self, ErrorKind, }, }; pub fn cli() { let m = App::new("uiproject") .author("Nikos Efthias<nikos@mugsoft.io>") .version(clap::crate_version!()) .arg(Arg::with_name("project").required(true)) .get_matches(); let folder_name = m.value_of("project").unwrap(); File::open(folder_name) .and_then(folder_exists) .or_else(|_| create_folder(folder_name)) .and_then(|_| init_git_repo(folder_name)) .and_then(|_| { super::mkdirs(folder_name); Ok(()) }) .expect("cannot create project"); } fn folder_exists(_: File) -> std::io::Result<()> { eprintln!("project exists with given name"); Err(std::io::Error::from(ErrorKind::AlreadyExists)) } fn create_folder(name: &str) -> std::io::Result<()> { let mut builder = DirBuilder::new(); builder.recursive(true); builder.create(name) } fn init_git_repo(name: &str) -> io::Result<()> { git2::Repository::init(name) .and_then(|_| Ok(())) .or_else(|_| Err(io::Error::from(ErrorKind::Other))) }