use clap::{SubCommand, Arg, ArgMatches};
use std::path::Path;
use error::Error;
use commands::StaticSubcommand;
use libpijul::Repository;
use libpijul::fs_representation::{find_repo_root, pristine_dir, create};
use rand;
use super::{get_wd};
pub fn invocation() -> StaticSubcommand {
return SubCommand::with_name("init")
.about("Create a new repository")
.arg(Arg::with_name("directory")
.index(1)
.help("Where to create the repository, defaults to the current repository.")
.required(false));
}
pub struct Params<'a> {
pub location: Option<&'a Path>,
pub allow_nested: bool,
}
pub fn parse_args<'a>(args: &'a ArgMatches) -> Params<'a> {
Params {
location: args.value_of("repository").map(|x| Path::new(x)),
allow_nested: false,
}
}
pub fn run(p: &Params) -> Result<(), Error> {
let wd = try!(get_wd(p.location));
match find_repo_root(&wd) {
Some(_) => {
if p.allow_nested {
try!(create(&wd));
} else {
return Err(Error::InARepository)
}
}
None => {
try!(create(&wd));
}
}
let repo_dir = pristine_dir(wd);
let repo = try!(Repository::open(&repo_dir, None));
let txn = try!(repo.mut_txn_begin(rand::thread_rng()));
try!(txn.commit());
Ok(())
}