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::result::Result as RResult;
use std::io::Read; use std::io::Read;
use std::ops::DerefMut; use std::ops::DerefMut;
use std::io::Write;
use std::io::stderr;
use std::process::exit;
use clap::ArgMatches; use clap::ArgMatches;
use toml::Value; use toml::Value;
@ -44,24 +41,15 @@ use util::build_toml_header;
type Result<T> = RResult<T, StoreError>; type Result<T> = RResult<T, StoreError>;
pub fn create(rt: &Runtime) { pub fn create(rt: &Runtime) {
rt.cli() let scmd = rt.cli().subcommand_matches("create").unwrap();
.subcommand_matches("create")
.map(|scmd| {
debug!("Found 'create' subcommand..."); debug!("Found 'create' subcommand...");
// unwrap is safe as value is required // unwrap is safe as value is required
let path = scmd.value_of("path").or_else(|| scmd.value_of("id")); let path = scmd.value_of("path").unwrap();
if path.is_none() { let path = PathBuf::from(path);
warn!("No ID / Path provided. Exiting now"); let store = Some(rt.store().path().clone());
write!(stderr(), "No ID / Path provided. Exiting now").ok(); let path = StoreId::new(store, path).unwrap_or_else(|e| trace_error_exit(&e, 1));
exit(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() { if scmd.subcommand_matches("entry").is_some() {
@ -81,7 +69,6 @@ pub fn create(rt: &Runtime) {
error!("Error building Entry"); error!("Error building Entry");
trace_error_exit(&e, 1); trace_error_exit(&e, 1);
}) })
});
} }
fn create_from_cli_spec(rt: &Runtime, matches: &ArgMatches, path: &StoreId) -> Result<()> { 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") .about("Create an entry from the store")
.version("0.1") .version("0.1")
.arg(Arg::with_name("path") .arg(Arg::with_name("path")
.long("path") .index(1)
.short("p")
.takes_value(true) .takes_value(true)
.required(false) .required(true)
.help("Create at this store path") .help("Create at this store path")
.value_name("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") .arg(Arg::with_name("from-raw")
.long("from-raw") .long("from-raw")
.takes_value(true) .takes_value(true)
.help("Create a new entry by reading this file ('-' for stdin)") .help("Create a new entry by reading this file ('-' for stdin)")
.value_name("FILE")) .value_name("FILE"))
.group(ArgGroup::with_name("create-destination-group")
.args(&["path", "id"])
.required(true))
.subcommand(SubCommand::with_name("entry") .subcommand(SubCommand::with_name("entry")
.about("Create an entry via commandline") .about("Create an entry via commandline")
.version("0.1") .version("0.1")