From d3d6b80b8be7d9b3de95043d2c3dffd23b5c77f3 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 14 Feb 2018 12:46:13 +0100 Subject: [PATCH] Redesign of the CLI --- bin/core/imag-ref/Cargo.toml | 1 + bin/core/imag-ref/src/main.rs | 103 +++++++++++++--------------------- bin/core/imag-ref/src/ui.rs | 60 ++++---------------- 3 files changed, 49 insertions(+), 115 deletions(-) diff --git a/bin/core/imag-ref/Cargo.toml b/bin/core/imag-ref/Cargo.toml index b0fb814a..9a0b2dae 100644 --- a/bin/core/imag-ref/Cargo.toml +++ b/bin/core/imag-ref/Cargo.toml @@ -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" } diff --git a/bin/core/imag-ref/src/main.rs b/bin/core/imag-ref/src/main.rs index 3d1339e7..ce24b2f3 100644 --- a/bin/core/imag-ref/src/main.rs +++ b/bin/core/imag-ref/src/main.rs @@ -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(); } diff --git a/bin/core/imag-ref/src/ui.rs b/bin/core/imag-ref/src/ui.rs index 295b6a26..14d79917 100644 --- a/bin/core/imag-ref/src/ui.rs +++ b/bin/core/imag-ref/src/ui.rs @@ -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")) - - ) }