imag-counter: init

This commit is contained in:
Matthias Beyer 2016-02-13 20:25:11 +01:00
parent 2bc9f2ec25
commit 09ac788e6f
5 changed files with 263 additions and 0 deletions

19
imag-counter/Cargo.toml Normal file
View file

@ -0,0 +1,19 @@
[package]
name = "imag-counter"
version = "0.1.0"
authors = ["Matthias Beyer <mail@beyermatthias.de>"]
[dependencies]
clap = "2.1.1"
log = "0.3.5"
version = "2.0.1"
[dependencies.libimagrt]
path = "../libimagrt"
[dependencies.libimagutil]
path = "../libimagutil"
[dependencies.libimagcounter]
path = "../libimagcounter"

View file

@ -0,0 +1,29 @@
use std::str::FromStr;
use std::process::exit;
use libimagrt::runtime::Runtime;
use libimagutil::trace::trace_error;
use libimagcounter::counter::Counter;
pub fn create(rt: &Runtime) {
rt.cli()
.subcommand_matches("create")
.map(|scmd| {
debug!("Found 'create' subcommand...");
let name = scmd.value_of("name").unwrap(); // safe because clap enforces
let init : i64 = scmd
.value_of("initval")
.and_then(|i| FromStr::from_str(i).ok())
.unwrap_or(0);
match Counter::new(rt.store(), String::from(name.clone()), init) {
Err(e) => {
warn!("Could not create Counter '{}' with initial value '{}'", name, init);
trace_error(&e);
exit(1);
},
Ok(_) => info!("Created Counter '{}' with initial value '{}'", name, init),
}
});
}

View file

@ -0,0 +1,23 @@
use std::process::exit;
use libimagrt::runtime::Runtime;
use libimagutil::trace::trace_error;
use libimagcounter::counter::Counter;
pub fn delete(rt: &Runtime) {
rt.cli()
.subcommand_matches("delete")
.map(|scmd| {
debug!("Found 'delete' subcommand...");
let name = String::from(scmd.value_of("name").unwrap()); // safe because clap enforces
if let Err(e) = Counter::delete(name, rt.store()) {
trace_error(&e);
exit(1);
}
info!("Ok");
});
}

134
imag-counter/src/main.rs Normal file
View file

@ -0,0 +1,134 @@
#[macro_use] extern crate log;
#[macro_use] extern crate version;
extern crate clap;
extern crate libimagcounter;
extern crate libimagrt;
extern crate libimagutil;
use std::process::exit;
use std::str::FromStr;
use libimagrt::runtime::Runtime;
use libimagcounter::counter::Counter;
use libimagutil::trace::trace_error;
use libimagutil::key_value_split::IntoKeyValue;
mod create;
mod delete;
mod ui;
use ui::build_ui;
use create::create;
use delete::delete;
enum Action {
Inc,
Dec,
Reset,
Set,
}
fn main() {
let name = "imag-counter";
let version = &version!()[..];
let about = "Counter tool to count things";
let ui = build_ui(Runtime::get_default_cli_builder(name, version, about));
let rt = {
let rt = Runtime::new(ui);
if rt.is_ok() {
rt.unwrap()
} else {
println!("Could not set up Runtime");
println!("{:?}", rt.err().unwrap());
exit(1);
}
};
rt.init_logger();
debug!("Hello. Logging was just enabled");
debug!("I already set up the Runtime object and build the commandline interface parser.");
debug!("Lets get rollin' ...");
rt.cli()
.subcommand_name()
.map_or_else(|| {
let (action, name) = {
if rt.cli().is_present("increment") {
(Action::Inc, rt.cli().value_of("increment").unwrap())
} else if rt.cli().is_present("decrement") {
(Action::Dec, rt.cli().value_of("decrement").unwrap())
} else if rt.cli().is_present("reset") {
(Action::Reset, rt.cli().value_of("reset").unwrap())
} else /* rt.cli().is_present("set") */ {
(Action::Set, rt.cli().value_of("set").unwrap())
}
};
match action {
Action::Inc => {
Counter::load(String::from(name), rt.store())
.map(|mut counter| {
match counter.inc() {
Err(e) => { trace_error(&e); exit(1); },
Ok(_) => info!("Ok"),
}
})
},
Action::Dec => {
Counter::load(String::from(name), rt.store())
.map(|mut counter| {
match counter.dec() {
Err(e) => { trace_error(&e); exit(1); },
Ok(_) => info!("Ok"),
}
})
},
Action::Reset => {
Counter::load(String::from(name), rt.store())
.map(|mut counter| {
match counter.reset() {
Err(e) => { trace_error(&e); exit(1); },
Ok(_) => info!("Ok"),
}
})
},
Action::Set => {
let kv = String::from(name).into_kv();
if kv.is_none() {
warn!("Not a key-value pair: '{}'", name);
exit(1);
}
let (key, value) = kv.unwrap().into();
let value = FromStr::from_str(&value[..]);
if value.is_err() {
warn!("Not a integer: '{:?}'", value);
exit(1);
}
let value : i64 = value.unwrap();
Counter::load(String::from(key), rt.store())
.map(|mut counter| {
match counter.set(value) {
Err(e) => { trace_error(&e); exit(1); },
Ok(_) => info!("Ok"),
}
})
},
}
.map_err(|e| trace_error(&e));
},
|name| {
debug!("Call: {}", name);
match name {
"create" => create(&rt),
"delete" => delete(&rt),
_ => {
debug!("Unknown command"); // More error handling
},
};
})
}

58
imag-counter/src/ui.rs Normal file
View file

@ -0,0 +1,58 @@
use clap::{Arg, App, SubCommand};
pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
app
.arg(Arg::with_name("increment")
.long("inc")
.short("i")
.takes_value(true)
.required(false)
.help("Increment a counter"))
.arg(Arg::with_name("decrement")
.long("dec")
.short("d")
.takes_value(true)
.required(false)
.help("Decrement a counter"))
.arg(Arg::with_name("reset")
.long("reset")
.takes_value(true)
.required(false)
.help("Reset a counter"))
.arg(Arg::with_name("set")
.long("set")
.takes_value(true)
.required(false)
.help("Set a counter"))
.subcommand(SubCommand::with_name("create")
.about("Create a counter")
.version("0.1")
.arg(Arg::with_name("name")
.long("name")
.short("n")
.takes_value(true)
.required(true)
.help("Create counter with this name"))
.arg(Arg::with_name("initval")
.long("init")
.short("i")
.takes_value(true)
.required(false)
.help("Initial value")))
.subcommand(SubCommand::with_name("delete")
.about("Delete a counter")
.version("0.1")
.arg(Arg::with_name("name")
.long("name")
.short("n")
.takes_value(true)
.required(true)
.help("Create counter with this name")))
}