Merge branch 'imag-ref/rewrite-libimagref-api-usage' into libimagentryref/refactor

This commit is contained in:
Matthias Beyer 2018-02-19 12:17:58 +01:00
commit 2dc23c8101
3 changed files with 49 additions and 115 deletions

View file

@ -24,6 +24,7 @@ maintenance = { status = "actively-developed" }
[dependencies]
log = "0.4.0"
libimagstore = { version = "0.7.0", path = "../../../lib/core/libimagstore" }
libimagrt = { version = "0.7.0", path = "../../../lib/core/libimagrt" }
libimagerror = { version = "0.7.0", path = "../../../lib/core/libimagerror" }
libimagentryref = { version = "0.7.0", path = "../../../lib/entry/libimagentryref" }

View file

@ -35,6 +35,7 @@
#[macro_use] extern crate log;
extern crate clap;
extern crate libimagstore;
#[macro_use] extern crate libimagrt;
extern crate libimagentryref;
extern crate libimagerror;
@ -48,12 +49,11 @@ use ui::build_ui;
use std::path::PathBuf;
use std::process::exit;
use libimagentryref::refstore::RefStore;
use libimagentryref::flags::RefFlags;
use libimagerror::trace::trace_error;
use libimagerror::trace::MapErrTrace;
use libimagrt::setup::generate_runtime_setup;
use libimagrt::runtime::Runtime;
use libimagstore::storeid::IntoStoreId;
use libimagentryref::reference::Ref;
fn main() {
let version = make_imag_version!();
@ -66,9 +66,8 @@ fn main() {
.map(|name| {
debug!("Call: {}", name);
match name {
"add" => add(&rt),
"deref" => deref(&rt),
"remove" => remove(&rt),
"list" => list(&rt),
_ => {
debug!("Unknown command"); // More error handling
},
@ -76,83 +75,57 @@ fn main() {
});
}
fn add(rt: &Runtime) {
let cmd = rt.cli().subcommand_matches("add").unwrap();
let path = cmd.value_of("path").map(PathBuf::from).unwrap(); // saved by clap
fn deref(rt: &Runtime) {
let cmd = rt.cli().subcommand_matches("deref").unwrap();
let id = cmd.value_of("ID")
.map(String::from)
.map(PathBuf::from)
.unwrap() // saved by clap
.into_storeid()
.map_err_trace_exit_unwrap(1);
let flags = RefFlags::default()
.with_content_hashing(cmd.is_present("track-content"))
.with_permission_tracking(cmd.is_present("track-permissions"));
match RefStore::create(rt.store(), path, flags) {
Ok(r) => {
debug!("Reference created: {:?}", r);
match rt.store().get(id.clone()).map_err_trace_exit_unwrap(1) {
Some(entry) => entry
.get_path()
.map_err_trace_exit_unwrap(1)
.to_str()
.ok_or_else(|| {
error!("Could not transform path into string!");
exit(1)
})
.map(|s| info!("{}", s))
.ok(), // safe here because we exited already in the error case
None => {
error!("No entry for id '{}' found", id);
exit(1)
},
Err(e) => {
trace_error(&e);
warn!("Failed to create reference");
},
}
};
}
fn remove(rt: &Runtime) {
use libimaginteraction::ask::ask_bool;
let cmd = rt.cli().subcommand_matches("remove").unwrap();
let hash = cmd.value_of("hash").map(String::from).unwrap(); // saved by clap
let yes = cmd.is_present("yes");
let id = cmd.value_of("ID")
.map(String::from)
.map(PathBuf::from)
.unwrap() // saved by clap
.into_storeid()
.map_err_trace_exit_unwrap(1);
match rt.store().find_storeid_by_partial_hash(&hash).map_err_trace_exit_unwrap(1) {
Some(sid) => {
if yes || ask_bool(&format!("Delete Ref with hash '{}'", hash)[..], None) {
debug!("Found for hash '{}' -> {:?}", hash, sid);
rt.store().delete(sid).map_err_trace_exit_unwrap(1)
match rt.store().get(id.clone()).map_err_trace_exit_unwrap(1) {
Some(mut entry) => {
if yes || ask_bool(&format!("Delete ref from entry '{}'", id), None) {
let _ = entry.remove_ref().map_err_trace_exit_unwrap(1);
} else {
info!("Aborted");
}
},
None => {
error!("Not id for hash '{}' found", hash);
error!("No entry for id '{}' found", id);
exit(1)
},
};
}
fn list(rt: &Runtime) {
use std::process::exit;
use libimagentrylist::lister::Lister;
use libimagentryref::lister::RefLister;
let cmd = rt.cli().subcommand_matches("list").unwrap();
let do_check_dead = cmd.is_present("check-dead");
let do_check_changed = cmd.is_present("check-changed");
let do_check_changed_content = cmd.is_present("check-changed-content");
let do_check_changed_permiss = cmd.is_present("check-changed-permissions");
let iter = match rt.store().retrieve_for_module("ref") {
Ok(iter) => iter.filter_map(|id| {
match rt.store().get(id) {
Ok(r) => Some(r),
Err(e) => {
trace_error(&e);
None
},
}
}),
Err(e) => {
trace_error(&e);
exit(1);
}
};
RefLister::new()
.check_dead(do_check_dead)
.check_changed(do_check_changed)
.check_changed_content(do_check_changed_content)
.check_changed_permiss(do_check_changed_permiss)
.list(iter.filter_map(Into::into))
.ok();
}

View file

@ -19,73 +19,33 @@
use clap::{Arg, App, SubCommand};
use libimagutil::cli_validators::is_existing_path;
pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
app
.subcommand(SubCommand::with_name("add")
.about("Add a reference to a file outside of the store")
.subcommand(SubCommand::with_name("deref")
.about("'Dereference' a ref. This prints the Path of the referenced file")
.version("0.1")
.arg(Arg::with_name("path")
.arg(Arg::with_name("ID")
.index(1)
.takes_value(true)
.required(true)
.help("The path of the file")
.validator(is_existing_path)
.value_name("PATH"))
.arg(Arg::with_name("track-content")
.long("content-hash")
.short("C")
.takes_value(false)
.required(false)
.help("Hash the content for the reference"))
.arg(Arg::with_name("track-permissions")
.long("permission-tracking")
.short("P")
.takes_value(false)
.required(false)
.help("Rememeber the permissions of the referenced file"))
.help("The id of the store entry to dereference")
.value_name("ID"))
)
.subcommand(SubCommand::with_name("remove")
.about("Remove a reference")
.about("Remove a reference from an entry")
.version("0.1")
.arg(Arg::with_name("hash")
.arg(Arg::with_name("ID")
.index(1)
.takes_value(true)
.required(true)
.help("Remove the reference with this hash")
.value_name("HASH"))
.multiple(true)
.help("Remove the reference from this store entry")
.value_name("ENTRIES"))
.arg(Arg::with_name("yes")
.long("yes")
.short("y")
.help("Don't ask whether this really should be done"))
)
.subcommand(SubCommand::with_name("list")
.about("List references in the store")
.version("0.1")
.arg(Arg::with_name("check-dead")
.long("check-dead")
.short("d")
.help("Check each reference whether it is dead"))
.arg(Arg::with_name("check-changed")
.long("check-changed")
.short("c")
.help("Check whether a reference had changed (content or permissions)"))
.arg(Arg::with_name("check-changed-content")
.long("check-changed-content")
.short("C")
.help("Check whether the content of the referenced file changed"))
.arg(Arg::with_name("check-changed-permissions")
.long("check-changed-perms")
.short("P")
.help("Check whether the permissions of the referenced file changed"))
)
}