From c040f3d3e53d6d127bdfafca48a680a5fc3d4351 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 10 Mar 2016 20:27:21 +0100 Subject: [PATCH 1/5] Add ui for "list" subcommand --- imag-counter/src/ui.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/imag-counter/src/ui.rs b/imag-counter/src/ui.rs index 994bb65e..2e6d18ef 100644 --- a/imag-counter/src/ui.rs +++ b/imag-counter/src/ui.rs @@ -54,6 +54,38 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .required(true) .help("Create counter with this name"))) + .subcommand(SubCommand::with_name("list") + .about("List counters") + .version("0.1") + .arg(Arg::with_name("name") + .long("name") + .short("n") + .takes_value(true) + .required(false) + .help("List counters with this name (foo/bar and baz/bar would match 'bar')")) + + .arg(Arg::with_name("greater-than") + .long("greater") + .short("g") + .takes_value(true) + .required(false) + .help("List counters which are greater than VALUE")) + + .arg(Arg::with_name("lower-than") + .long("lower") + .short("l") + .takes_value(true) + .required(false) + .help("List counters which are lower than VALUE")) + + .arg(Arg::with_name("equals") + .long("equal") + .short("e") + .takes_value(true) + .required(false) + .help("List counters which equal VALUE")) + ) + .subcommand(SubCommand::with_name("interactive") .about("Interactively count things") .version("0.1") From 44287d21621559820e3a5868ab8558de7d2c92cc Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 10 Mar 2016 20:28:45 +0100 Subject: [PATCH 2/5] Add list() --- imag-counter/src/list.rs | 5 +++++ imag-counter/src/main.rs | 3 +++ 2 files changed, 8 insertions(+) create mode 100644 imag-counter/src/list.rs diff --git a/imag-counter/src/list.rs b/imag-counter/src/list.rs new file mode 100644 index 00000000..9a9721df --- /dev/null +++ b/imag-counter/src/list.rs @@ -0,0 +1,5 @@ +use libimagrt::runtime::Runtime; + +pub fn list(rt: &Runtime) { + unimplemented!(); +} diff --git a/imag-counter/src/main.rs b/imag-counter/src/main.rs index 961db48c..8e62be23 100644 --- a/imag-counter/src/main.rs +++ b/imag-counter/src/main.rs @@ -17,12 +17,14 @@ use libimagutil::key_value_split::IntoKeyValue; mod create; mod delete; mod interactive; +mod list; mod ui; use ui::build_ui; use create::create; use delete::delete; use interactive::interactive; +use list::list; enum Action { Inc, @@ -126,6 +128,7 @@ fn main() { "create" => create(&rt), "delete" => delete(&rt), "interactive" => interactive(&rt), + "list" => list(&rt), _ => { debug!("Unknown command"); // More error handling }, From 8c380aa3aecd9a2ccafa791c0b744576ce9148c0 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 11 Mar 2016 09:45:20 +0100 Subject: [PATCH 3/5] Start implementing "list" --- imag-counter/src/list.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/imag-counter/src/list.rs b/imag-counter/src/list.rs index 9a9721df..2a4759fb 100644 --- a/imag-counter/src/list.rs +++ b/imag-counter/src/list.rs @@ -1,5 +1,10 @@ use libimagrt::runtime::Runtime; pub fn list(rt: &Runtime) { - unimplemented!(); + rt.cli() + .subcommand_matches("list") + .map(|scmd| { + debug!("Found 'list' subcommand..."); + + }); } From 78feb53aa8734de44deba792faf7782cbc25920f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 13 Mar 2016 16:33:30 +0100 Subject: [PATCH 4/5] Add Counter::all_counters() to get all counters --- libimagcounter/src/counter.rs | 51 +++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/libimagcounter/src/counter.rs b/libimagcounter/src/counter.rs index d233e80d..18d5ca30 100644 --- a/libimagcounter/src/counter.rs +++ b/libimagcounter/src/counter.rs @@ -8,6 +8,7 @@ use toml::Value; use std::collections::BTreeMap; use libimagstore::store::Store; +use libimagstore::storeid::StoreIdIterator; use libimagstore::store::FileLockEntry; use libimagstore::storeid::StoreId; use libimagstore::error::StoreError; @@ -140,5 +141,55 @@ impl<'a> Counter<'a> { store.delete(ModuleEntryPath::new(name).into_storeid()) .map_err(|e| CE::new(CEK::StoreWriteError, Some(Box::new(e)))) } + + pub fn all_counters(store: &Store) -> Result { + store.retrieve_for_module("counter") + .map(|iter| CounterIterator::new(store, iter)) + .map_err(|e| CE::new(CEK::StoreReadError, Some(Box::new(e)))) + } + +} + +trait FromStoreId { + fn from_storeid<'a>(&'a Store, StoreId) -> Result>; +} + +impl<'a> FromStoreId for Counter<'a> { + + fn from_storeid<'b>(store: &'b Store, id: StoreId) -> Result> { + debug!("Loading counter from storeid: '{:?}'", id); + match store.retrieve(id) { + Err(e) => Err(CE::new(CEK::StoreReadError, Some(Box::new(e)))), + Ok(c) => Ok(Counter { fle: c }), + } + } + +} + +pub struct CounterIterator<'a> { + store: &'a Store, + iditer: StoreIdIterator, +} + +impl<'a> CounterIterator<'a> { + + pub fn new(store: &'a Store, iditer: StoreIdIterator) -> CounterIterator<'a> { + CounterIterator { + store: store, + iditer: iditer, + } + } + +} + +impl<'a> Iterator for CounterIterator<'a> { + type Item = Result>; + + fn next(&mut self) -> Option>> { + self.iditer + .next() + .map(|id| Counter::from_storeid(self.store, id)) + } + } From 2e340acbb78282d3effd01d3af328dcf275d9e82 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 13 Mar 2016 16:50:10 +0100 Subject: [PATCH 5/5] Implement list() for imag-counter --- imag-counter/src/list.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/imag-counter/src/list.rs b/imag-counter/src/list.rs index 2a4759fb..abb14fda 100644 --- a/imag-counter/src/list.rs +++ b/imag-counter/src/list.rs @@ -1,4 +1,7 @@ use libimagrt::runtime::Runtime; +use libimagutil::trace::trace_error; +use libimagcounter::counter::Counter; +use libimagcounter::result::Result; pub fn list(rt: &Runtime) { rt.cli() @@ -6,5 +9,27 @@ pub fn list(rt: &Runtime) { .map(|scmd| { debug!("Found 'list' subcommand..."); + Counter::all_counters(rt.store()).map(|iterator| { + for counter in iterator { + counter.map(|c| { + let name = c.name(); + let value = c.value(); + + if name.is_err() { + trace_error(&name.err().unwrap()); + } else { + + if value.is_err() { + trace_error(&value.err().unwrap()); + } else { + println!("{} - {}", name.unwrap(), value.unwrap()); + } + } + }) + .map_err(|e| trace_error(&e)); + } + }) + .map_err(|e| trace_error(&e)) + }); }