elm_ui_project/cli/
mod.rs

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