Rewrite create() to use positional arg
This commit is contained in:
parent
e6d96c9f83
commit
a71732be49
2 changed files with 27 additions and 52 deletions
|
@ -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,44 +41,34 @@ 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")
|
debug!("Found 'create' subcommand...");
|
||||||
.map(|scmd| {
|
|
||||||
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();
|
debug!("path = {:?}", path);
|
||||||
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);
|
|
||||||
|
|
||||||
if scmd.subcommand_matches("entry").is_some() {
|
if scmd.subcommand_matches("entry").is_some() {
|
||||||
debug!("Creating entry from CLI specification");
|
debug!("Creating entry from CLI specification");
|
||||||
create_from_cli_spec(rt, scmd, &path)
|
create_from_cli_spec(rt, scmd, &path)
|
||||||
.or_else(|_| create_from_source(rt, scmd, &path))
|
.or_else(|_| create_from_source(rt, scmd, &path))
|
||||||
.or_else(|_| create_with_content_and_header(rt,
|
.or_else(|_| create_with_content_and_header(rt,
|
||||||
&path,
|
&path,
|
||||||
String::new(),
|
String::new(),
|
||||||
Entry::default_header()))
|
Entry::default_header()))
|
||||||
} else {
|
} else {
|
||||||
debug!("Creating entry");
|
debug!("Creating entry");
|
||||||
create_with_content_and_header(rt, &path, String::new(),
|
create_with_content_and_header(rt, &path, String::new(),
|
||||||
Entry::default_header())
|
Entry::default_header())
|
||||||
}
|
}
|
||||||
.unwrap_or_else(|e| {
|
.unwrap_or_else(|e| {
|
||||||
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<()> {
|
||||||
|
|
|
@ -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")
|
||||||
|
|
Loading…
Reference in a new issue