Rewrite imag-ref

This commit is contained in:
Matthias Beyer 2018-10-23 15:52:13 +02:00
parent ac5a3c0298
commit 49df7f54a9
3 changed files with 165 additions and 60 deletions

View file

@ -47,15 +47,17 @@ extern crate libimagutil;
mod ui; mod ui;
use ui::build_ui; use ui::build_ui;
use std::path::PathBuf;
use std::process::exit; use std::process::exit;
use std::io::Write;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
use libimagerror::exit::ExitUnwrap; use libimagerror::exit::ExitUnwrap;
use libimagrt::setup::generate_runtime_setup; use libimagrt::setup::generate_runtime_setup;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagstore::storeid::IntoStoreId;
use libimagentryref::reference::Ref; use libimagentryref::reference::Ref;
use libimagentryref::reference::MutRef;
use libimagentryref::reference::RefFassade;
use libimagentryref::hasher::default::DefaultHasher;
fn main() { fn main() {
let version = make_imag_version!(); let version = make_imag_version!();
@ -69,6 +71,7 @@ fn main() {
debug!("Call: {}", name); debug!("Call: {}", name);
match name { match name {
"deref" => deref(&rt), "deref" => deref(&rt),
"create" => create(&rt),
"remove" => remove(&rt), "remove" => remove(&rt),
other => { other => {
debug!("Unknown command"); debug!("Unknown command");
@ -83,16 +86,16 @@ fn main() {
fn deref(rt: &Runtime) { fn deref(rt: &Runtime) {
let cmd = rt.cli().subcommand_matches("deref").unwrap(); let cmd = rt.cli().subcommand_matches("deref").unwrap();
let id = cmd.value_of("ID") let ids = rt.ids::<::ui::PathProvider>().map_err_trace_exit_unwrap();
.map(String::from) let out = rt.stdout();
.map(PathBuf::from) let mut outlock = out.lock();
.unwrap() // saved by clap
.into_storeid()
.map_err_trace_exit_unwrap();
ids.into_iter()
.for_each(|id| {
match rt.store().get(id.clone()).map_err_trace_exit_unwrap() { match rt.store().get(id.clone()).map_err_trace_exit_unwrap() {
Some(entry) => { Some(entry) => {
entry entry
.as_ref_with_hasher::<DefaultHasher>()
.get_path() .get_path()
.map_err_trace_exit_unwrap() .map_err_trace_exit_unwrap()
.to_str() .to_str()
@ -100,7 +103,7 @@ fn deref(rt: &Runtime) {
error!("Could not transform path into string!"); error!("Could not transform path into string!");
exit(1) exit(1)
}) })
.map(|s| info!("{}", s)) .map(|s| writeln!(outlock, "{}", s))
.ok(); // safe here because we exited already in the error case .ok(); // safe here because we exited already in the error case
let _ = rt.report_touched(&id).unwrap_or_exit(); let _ = rt.report_touched(&id).unwrap_or_exit();
@ -109,7 +112,8 @@ fn deref(rt: &Runtime) {
error!("No entry for id '{}' found", id); error!("No entry for id '{}' found", id);
exit(1) exit(1)
}, },
}; }
});
} }
fn remove(rt: &Runtime) { fn remove(rt: &Runtime) {
@ -117,12 +121,7 @@ fn remove(rt: &Runtime) {
let cmd = rt.cli().subcommand_matches("remove").unwrap(); let cmd = rt.cli().subcommand_matches("remove").unwrap();
let yes = cmd.is_present("yes"); let yes = cmd.is_present("yes");
let id = cmd.value_of("ID") let ids = rt.ids::<::ui::PathProvider>().map_err_trace_exit_unwrap();
.map(String::from)
.map(PathBuf::from)
.unwrap() // saved by clap
.into_storeid()
.map_err_trace_exit_unwrap();
let mut input = rt.stdin().unwrap_or_else(|| { let mut input = rt.stdin().unwrap_or_else(|| {
error!("No input stream. Cannot ask for permission"); error!("No input stream. Cannot ask for permission");
@ -131,13 +130,17 @@ fn remove(rt: &Runtime) {
let mut output = rt.stdout(); let mut output = rt.stdout();
ids.into_iter()
.for_each(|id| {
match rt.store().get(id.clone()).map_err_trace_exit_unwrap() { match rt.store().get(id.clone()).map_err_trace_exit_unwrap() {
Some(mut entry) => { Some(mut entry) => {
if yes || if yes ||
ask_bool(&format!("Delete ref from entry '{}'", id), None, &mut input, &mut output) ask_bool(&format!("Delete ref from entry '{}'", id), None, &mut input, &mut output)
.map_err_trace_exit_unwrap() .map_err_trace_exit_unwrap()
{ {
let _ = entry.remove_ref().map_err_trace_exit_unwrap(); let _ = entry.as_ref_with_hasher_mut::<DefaultHasher>()
.remove_ref()
.map_err_trace_exit_unwrap();
} else { } else {
info!("Aborted"); info!("Aborted");
} }
@ -146,6 +149,11 @@ fn remove(rt: &Runtime) {
error!("No entry for id '{}' found", id); error!("No entry for id '{}' found", id);
exit(1) exit(1)
}, },
}; }
});
}
fn create(rt: &Runtime) {
unimplemented!()
} }

View file

@ -17,19 +17,34 @@
// 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, App, SubCommand}; use std::path::PathBuf;
use clap::{Arg, App, ArgMatches, 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> { pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
app app
.subcommand(SubCommand::with_name("deref") .subcommand(SubCommand::with_name("deref")
.about("'Dereference' a ref. This prints the Path of the referenced file") .about("'Dereference a ref. This prints the Path(es) of the referenced file(s)")
.version("0.1") .version("0.1")
.arg(Arg::with_name("ID") .arg(Arg::with_name("ID")
.index(1) .index(1)
.takes_value(true) .takes_value(true)
.required(true) .required(false)
.help("The id of the store entry to dereference") .multiple(true)
.help("The id of the store entry to dereference.")
.value_name("ID")) .value_name("ID"))
.arg(Arg::with_name("ignore-noref")
.long("ignore-noref")
.takes_value(false)
.required(false)
.multiple(false)
.help("Ignore store entries which are not refs and do not print error message"))
) )
.subcommand(SubCommand::with_name("remove") .subcommand(SubCommand::with_name("remove")
@ -38,14 +53,89 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.arg(Arg::with_name("ID") .arg(Arg::with_name("ID")
.index(1) .index(1)
.takes_value(true) .takes_value(true)
.required(true) .required(false)
.multiple(true) .multiple(true)
.help("Remove the reference from this store entry") .help("Remove the reference from this store entry")
.value_name("ENTRIES")) .value_name("ENTRIES"))
.arg(Arg::with_name("yes") .arg(Arg::with_name("ignore-noref")
.long("yes") .long("ignore-noref")
.short("y") .takes_value(false)
.help("Don't ask whether this really should be done")) .required(false)
.multiple(false)
.help("Ignore store entries which are not refs and do not print error message"))
)
.subcommand(SubCommand::with_name("create")
.about("Create a reference to a file")
.version("0.1")
.arg(Arg::with_name("ID")
.index(1)
.takes_value(true)
.required(true)
.multiple(false)
.help("Create a reference with that ID in the store. If the store id exists, it will be made into a reference.")
.value_name("ID"))
.arg(Arg::with_name("path")
.index(2)
.takes_value(true)
.required(true)
.multiple(false)
.help("The path to refer to. If there is no basepath configuration in the config file for the path this file is located at, the operation will error.")
.value_name("ID"))
.arg(Arg::with_name("force")
.long("force")
.takes_value(false)
.required(false)
.multiple(false)
.help("Use force to override existing references"))
) )
} }
pub struct PathProvider;
impl IdPathProvider for PathProvider {
fn get_ids(matches: &ArgMatches) -> Vec<StoreId> {
match matches.subcommand() {
("deref", Some(subm)) => {
subm.values_of("ID")
.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()
},
("remove", Some(subm)) => {
subm.values_of("ID")
.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()
},
("create", _) => {
error!("Command does not get IDs as input");
::std::process::exit(1)
},
(other, _) => {
error!("Not a known command: {}", other);
::std::process::exit(1)
}
}
}
}

View file

@ -349,3 +349,10 @@ default = "default"
# if this variable is _true_, imag-git will run git in $IMAG_RTP/store # if this variable is _true_, imag-git will run git in $IMAG_RTP/store
execute_in_store = false execute_in_store = false
[ref]
# configuration for imag-ref and ref using tools.
# The base pathes define the search pathes for libimagentryref
[ref.basepathes]
music = "/home/user/music"