Redesign of the CLI

This commit is contained in:
Matthias Beyer 2018-02-14 12:46:13 +01:00
parent 1153fb180b
commit d3d6b80b8b
3 changed files with 49 additions and 115 deletions

View File

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

View File

@ -35,6 +35,7 @@
#[macro_use] extern crate log; #[macro_use] extern crate log;
extern crate clap; extern crate clap;
extern crate libimagstore;
#[macro_use] extern crate libimagrt; #[macro_use] extern crate libimagrt;
extern crate libimagentryref; extern crate libimagentryref;
extern crate libimagerror; extern crate libimagerror;
@ -48,12 +49,11 @@ use ui::build_ui;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::exit; use std::process::exit;
use libimagentryref::refstore::RefStore;
use libimagentryref::flags::RefFlags;
use libimagerror::trace::trace_error;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
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;
fn main() { fn main() {
let version = make_imag_version!(); let version = make_imag_version!();
@ -66,9 +66,8 @@ fn main() {
.map(|name| { .map(|name| {
debug!("Call: {}", name); debug!("Call: {}", name);
match name { match name {
"add" => add(&rt), "deref" => deref(&rt),
"remove" => remove(&rt), "remove" => remove(&rt),
"list" => list(&rt),
_ => { _ => {
debug!("Unknown command"); // More error handling debug!("Unknown command"); // More error handling
}, },
@ -76,83 +75,57 @@ fn main() {
}); });
} }
fn add(rt: &Runtime) { fn deref(rt: &Runtime) {
let cmd = rt.cli().subcommand_matches("add").unwrap(); let cmd = rt.cli().subcommand_matches("deref").unwrap();
let path = cmd.value_of("path").map(PathBuf::from).unwrap(); // saved by clap 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() match rt.store().get(id.clone()).map_err_trace_exit_unwrap(1) {
.with_content_hashing(cmd.is_present("track-content")) Some(entry) => entry
.with_permission_tracking(cmd.is_present("track-permissions")); .get_path()
.map_err_trace_exit_unwrap(1)
match RefStore::create(rt.store(), path, flags) { .to_str()
Ok(r) => { .ok_or_else(|| {
debug!("Reference created: {:?}", r); 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) { fn remove(rt: &Runtime) {
use libimaginteraction::ask::ask_bool; use libimaginteraction::ask::ask_bool;
let cmd = rt.cli().subcommand_matches("remove").unwrap(); 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 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) { match rt.store().get(id.clone()).map_err_trace_exit_unwrap(1) {
Some(sid) => { Some(mut entry) => {
if yes || ask_bool(&format!("Delete Ref with hash '{}'", hash)[..], None) { if yes || ask_bool(&format!("Delete ref from entry '{}'", id), None) {
debug!("Found for hash '{}' -> {:?}", hash, sid); let _ = entry.remove_ref().map_err_trace_exit_unwrap(1);
rt.store().delete(sid).map_err_trace_exit_unwrap(1)
} else { } else {
info!("Aborted"); info!("Aborted");
} }
}, },
None => { None => {
error!("Not id for hash '{}' found", hash); error!("No entry for id '{}' found", id);
exit(1) 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 clap::{Arg, App, SubCommand};
use libimagutil::cli_validators::is_existing_path;
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("add") .subcommand(SubCommand::with_name("deref")
.about("Add a reference to a file outside of the store") .about("'Dereference' a ref. This prints the Path of the referenced file")
.version("0.1") .version("0.1")
.arg(Arg::with_name("path") .arg(Arg::with_name("ID")
.index(1) .index(1)
.takes_value(true) .takes_value(true)
.required(true) .required(true)
.help("The path of the file") .help("The id of the store entry to dereference")
.validator(is_existing_path) .value_name("ID"))
.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"))
) )
.subcommand(SubCommand::with_name("remove") .subcommand(SubCommand::with_name("remove")
.about("Remove a reference") .about("Remove a reference from an entry")
.version("0.1") .version("0.1")
.arg(Arg::with_name("hash") .arg(Arg::with_name("ID")
.index(1) .index(1)
.takes_value(true) .takes_value(true)
.required(true) .required(true)
.help("Remove the reference with this hash") .multiple(true)
.value_name("HASH")) .help("Remove the reference from this store entry")
.value_name("ENTRIES"))
.arg(Arg::with_name("yes") .arg(Arg::with_name("yes")
.long("yes") .long("yes")
.short("y") .short("y")
.help("Don't ask whether this really should be done")) .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"))
)
} }