Rewrite create() to use positional arg

This commit is contained in:
Matthias Beyer 2017-09-04 16:33:54 +02:00
parent e6d96c9f83
commit a71732be49
2 changed files with 27 additions and 52 deletions

View File

@ -23,9 +23,6 @@ use std::fs::OpenOptions;
use std::result::Result as RResult;
use std::io::Read;
use std::ops::DerefMut;
use std::io::Write;
use std::io::stderr;
use std::process::exit;
use clap::ArgMatches;
use toml::Value;
@ -44,44 +41,34 @@ use util::build_toml_header;
type Result<T> = RResult<T, StoreError>;
pub fn create(rt: &Runtime) {
rt.cli()
.subcommand_matches("create")
.map(|scmd| {
debug!("Found 'create' subcommand...");
let scmd = rt.cli().subcommand_matches("create").unwrap();
debug!("Found 'create' subcommand...");
// unwrap is safe as value is required
let path = scmd.value_of("path").or_else(|| scmd.value_of("id"));
if path.is_none() {
warn!("No ID / Path provided. Exiting now");
write!(stderr(), "No ID / Path provided. Exiting now").ok();
exit(1);
}
// unwrap is safe as value is required
let path = scmd.value_of("path").unwrap();
let path = PathBuf::from(path);
let store = Some(rt.store().path().clone());
let path = StoreId::new(store, path).unwrap_or_else(|e| trace_error_exit(&e, 1));
let store_path = rt.store().path().clone();
let path = match StoreId::new(Some(store_path), PathBuf::from(path.unwrap())) {
Err(e) => trace_error_exit(&e, 1),
Ok(o) => o,
};
debug!("path = {:?}", path);
debug!("path = {:?}", path);
if scmd.subcommand_matches("entry").is_some() {
debug!("Creating entry from CLI specification");
create_from_cli_spec(rt, scmd, &path)
.or_else(|_| create_from_source(rt, scmd, &path))
.or_else(|_| create_with_content_and_header(rt,
&path,
String::new(),
Entry::default_header()))
} else {
debug!("Creating entry");
create_with_content_and_header(rt, &path, String::new(),
Entry::default_header())
}
.unwrap_or_else(|e| {
error!("Error building Entry");
trace_error_exit(&e, 1);
})
});
if scmd.subcommand_matches("entry").is_some() {
debug!("Creating entry from CLI specification");
create_from_cli_spec(rt, scmd, &path)
.or_else(|_| create_from_source(rt, scmd, &path))
.or_else(|_| create_with_content_and_header(rt,
&path,
String::new(),
Entry::default_header()))
} else {
debug!("Creating entry");
create_with_content_and_header(rt, &path, String::new(),
Entry::default_header())
}
.unwrap_or_else(|e| {
error!("Error building Entry");
trace_error_exit(&e, 1);
})
}
fn create_from_cli_spec(rt: &Runtime, matches: &ArgMatches, path: &StoreId) -> Result<()> {

View File

@ -24,29 +24,17 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.about("Create an entry from the store")
.version("0.1")
.arg(Arg::with_name("path")
.long("path")
.short("p")
.index(1)
.takes_value(true)
.required(false)
.required(true)
.help("Create at this store path")
.value_name("PATH"))
.arg(Arg::with_name("id")
.long("id")
.short("i")
.takes_value(true)
.required(false)
.help("Same as --path, for consistency")
.value_name("PATH"))
.arg(Arg::with_name("from-raw")
.long("from-raw")
.takes_value(true)
.help("Create a new entry by reading this file ('-' for stdin)")
.value_name("FILE"))
.group(ArgGroup::with_name("create-destination-group")
.args(&["path", "id"])
.required(true))
.subcommand(SubCommand::with_name("entry")
.about("Create an entry via commandline")
.version("0.1")