Move imag-edit to ID provider infrastructure

This commit is contained in:
Matthias Beyer 2018-09-29 17:44:36 +02:00
parent 30036d5628
commit a805db2a88
2 changed files with 27 additions and 39 deletions

View file

@ -41,15 +41,11 @@ extern crate libimagerror;
extern crate libimagstore; extern crate libimagstore;
extern crate libimagutil; extern crate libimagutil;
use std::path::PathBuf;
use std::io::Read;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
use libimagerror::iter::TraceIterator; use libimagerror::iter::TraceIterator;
use libimagentryedit::edit::Edit; use libimagentryedit::edit::Edit;
use libimagentryedit::edit::EditHeader; use libimagentryedit::edit::EditHeader;
use libimagrt::setup::generate_runtime_setup; use libimagrt::setup::generate_runtime_setup;
use libimagstore::storeid::IntoStoreId;
use libimagstore::storeid::StoreIdIterator; use libimagstore::storeid::StoreIdIterator;
use libimagstore::iter::get::StoreIdGetIteratorExtension; use libimagstore::iter::get::StoreIdGetIteratorExtension;
@ -62,33 +58,11 @@ fn main() {
"Edit store entries with $EDITOR", "Edit store entries with $EDITOR",
ui::build_ui); ui::build_ui);
let sids = match rt.cli().value_of("entry") {
Some(path) => vec![PathBuf::from(path).into_storeid().map_err_trace_exit_unwrap(1)],
None => if rt.cli().is_present("entries-from-stdin") {
let stdin = rt.stdin().unwrap_or_else(|| {
error!("Cannot get handle to stdin");
::std::process::exit(1)
});
let mut buf = String::new();
let _ = stdin.lock().read_to_string(&mut buf).unwrap_or_else(|_| {
error!("Failed to read from stdin");
::std::process::exit(1)
});
buf.lines()
.map(PathBuf::from)
.map(|p| p.into_storeid().map_err_trace_exit_unwrap(1))
.collect()
} else {
error!("Something weird happened. I was not able to find the path of the entries to edit");
::std::process::exit(1)
}
};
let edit_header = rt.cli().is_present("edit-header"); let edit_header = rt.cli().is_present("edit-header");
let edit_header_only = rt.cli().is_present("edit-header-only"); let edit_header_only = rt.cli().is_present("edit-header-only");
let sids = rt.ids::<::ui::PathProvider>().map_err_trace_exit_unwrap(1);
StoreIdIterator::new(Box::new(sids.into_iter().map(Ok))) StoreIdIterator::new(Box::new(sids.into_iter().map(Ok)))
.into_get_iter(rt.store()) .into_get_iter(rt.store())
.trace_unwrap_exit(1) .trace_unwrap_exit(1)

View file

@ -17,7 +17,14 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use clap::{Arg, ArgGroup, App}; use std::path::PathBuf;
use clap::{Arg, ArgMatches, App};
use libimagstore::storeid::IntoStoreId;
use libimagstore::storeid::StoreId;
use libimagrt::runtime::IdPathProvider;
use libimagerror::trace::MapErrTrace;
pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
app app
@ -28,16 +35,6 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.multiple(true) .multiple(true)
.help("The entry/entries to edit") .help("The entry/entries to edit")
.value_name("ENTRY")) .value_name("ENTRY"))
.arg(Arg::with_name("entries-from-stdin")
.long("ids-from-stdin")
.short("I")
.takes_value(false)
.required(false)
.multiple(false)
.help("The entry/entries are piped in via stdin"))
.group(ArgGroup::with_name("input-method")
.args(&["entry", "entries-from-stdin"])
.required(true))
.arg(Arg::with_name("edit-header") .arg(Arg::with_name("edit-header")
.long("header") .long("header")
@ -55,3 +52,20 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.help("Only edit the header")) .help("Only edit the header"))
} }
pub struct PathProvider;
impl IdPathProvider for PathProvider {
fn get_ids(matches: &ArgMatches) -> Vec<StoreId> {
matches
.values_of("entry")
.ok_or_else(|| {
error!("No StoreId found");
::std::process::exit(1)
})
.unwrap()
.into_iter()
.map(PathBuf::from)
.map(|pb| pb.into_storeid())
.collect::<Result<Vec<_>, _>>()
.map_err_trace_exit_unwrap(1)
}
}