Move imag-category to ID provider infrastructure

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

View File

@ -47,13 +47,10 @@ use libimagerror::exit::ExitUnwrap;
use libimagerror::io::ToExitCode;
use libimagrt::runtime::Runtime;
use libimagrt::setup::generate_runtime_setup;
use libimagstore::storeid::IntoStoreId;
mod ui;
use std::io::Write;
use std::io::Read;
use std::path::PathBuf;
use libimagentrycategory::store::CategoryStore;
use libimagstore::storeid::StoreIdIterator;
@ -93,29 +90,7 @@ fn main() {
fn set(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("set").unwrap(); // safed by main()
let name = scmd.value_of("set-name").map(String::from).unwrap(); // safed by clap
let sids = match scmd.value_of("set-ids") {
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 sids = rt.ids::<::ui::PathProvider>().map_err_trace_exit_unwrap(1);
StoreIdIterator::new(Box::new(sids.into_iter().map(Ok)))
.into_get_iter(rt.store())
@ -132,31 +107,7 @@ fn set(rt: &Runtime) {
}
fn get(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("get").unwrap(); // safed by main()
let sids = match scmd.value_of("get-ids") {
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 sids = rt.ids::<::ui::PathProvider>().map_err_trace_exit_unwrap(1);
let out = rt.stdout();
let mut outlock = out.lock();

View File

@ -17,7 +17,14 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
use clap::{Arg, ArgGroup, App, SubCommand};
use std::path::PathBuf;
use clap::{Arg, ArgMatches, App, SubCommand};
use libimagstore::storeid::StoreId;
use libimagstore::storeid::IntoStoreId;
use libimagrt::runtime::IdPathProvider;
use libimagerror::trace::MapErrTrace;
pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
app
@ -79,17 +86,6 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.multiple(true)
.help("The entries to set the category for")
.value_name("ID"))
.arg(Arg::with_name("entries-from-stdin")
.long("ids-from-stdin")
.short("I")
.takes_value(false)
.required(false)
.multiple(false)
.help("Read the ids for the entries from stdin"))
.group(ArgGroup::with_name("input-method")
.args(&["set-ids", "entries-from-stdin"])
.required(true))
)
.subcommand(SubCommand::with_name("get")
@ -102,17 +98,65 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.multiple(true)
.help("The id of the Entry to get the category for")
.value_name("ID"))
.arg(Arg::with_name("entries-from-stdin")
.long("ids-from-stdin")
.short("I")
.takes_value(false)
.required(false)
.multiple(false)
.help("Read the ids for the entries from stdin"))
.group(ArgGroup::with_name("input-method")
.args(&["get-ids", "entries-from-stdin"])
.required(true))
)
}
pub struct PathProvider;
impl IdPathProvider for PathProvider {
fn get_ids(matches: &ArgMatches) -> Vec<StoreId> {
match matches.subcommand() {
("create-category", _) => {
error!("Command does not get IDs as input");
::std::process::exit(1)
},
("delete-category", _) => {
error!("Command does not get IDs as input");
::std::process::exit(1)
},
("list-categories", _) => {
error!("Command does not get IDs as input");
::std::process::exit(1)
},
("list-category", _) => {
error!("Command does not get IDs as input");
::std::process::exit(1)
},
("set", Some(subm)) => {
subm.values_of("set-ids")
.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)
},
("get", Some(subm)) => {
subm.values_of("get-ids")
.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)
},
(other, _) => {
error!("Not a known command: {}", other);
::std::process::exit(1)
}
}
}
}