imag/imag-tag/src/main.rs

163 lines
4.1 KiB
Rust
Raw Normal View History

2016-02-15 13:33:30 +00:00
extern crate clap;
#[macro_use] extern crate log;
extern crate semver;
extern crate toml;
#[macro_use] extern crate version;
extern crate libimagstore;
extern crate libimagrt;
extern crate libimagentrytag;
extern crate libimagerror;
2016-02-15 13:33:30 +00:00
2016-02-15 16:16:53 +00:00
use std::process::exit;
use libimagrt::runtime::Runtime;
use libimagrt::setup::generate_runtime_setup;
use libimagentrytag::tagable::Tagable;
use libimagentrytag::tag::Tag;
use libimagstore::storeid::build_entry_path;
use libimagerror::trace::trace_error;
use libimagentrytag::ui::{get_add_tags, get_remove_tags};
2016-02-15 16:16:53 +00:00
2016-02-15 13:42:20 +00:00
mod ui;
2016-02-15 16:16:53 +00:00
use ui::build_ui;
2016-02-15 13:33:30 +00:00
fn main() {
let rt = generate_runtime_setup("imag-store",
&version!()[..],
"Direct interface to the store. Use with great care!",
build_ui);
2016-02-15 16:16:53 +00:00
let id = rt.cli().value_of("id").unwrap(); // enforced by clap
rt.cli()
.subcommand_name()
.map_or_else(
|| {
let add = get_add_tags(rt.cli());
let rem = get_remove_tags(rt.cli());
2016-05-14 22:03:51 +00:00
alter(&rt, id, add, rem);
2016-02-15 16:16:53 +00:00
},
|name| {
debug!("Call: {}", name);
match name {
"list" => list(id, &rt),
_ => {
warn!("Unknown command");
// More error handling
},
};
});
}
fn alter(rt: &Runtime, id: &str, add: Option<Vec<Tag>>, rem: Option<Vec<Tag>>) {
let path = {
match build_entry_path(rt.store(), id) {
Err(e) => {
trace_error(&e);
exit(1);
},
Ok(s) => s,
}
};
2016-02-15 16:16:53 +00:00
debug!("path = {:?}", path);
match rt.store().get(path) {
Ok(Some(mut e)) => {
2016-02-15 16:16:53 +00:00
add.map(|tags| {
for tag in tags {
debug!("Adding tag '{:?}'", tag);
if let Err(e) = e.add_tag(tag) {
trace_error(&e);
}
2016-02-15 16:16:53 +00:00
}
}); // it is okay to ignore a None here
2016-02-15 16:16:53 +00:00
rem.map(|tags| {
for tag in tags {
debug!("Removing tag '{:?}'", tag);
if let Err(e) = e.remove_tag(tag) {
trace_error(&e);
}
2016-02-15 16:16:53 +00:00
}
}); // it is okay to ignore a None here
},
Ok(None) => {
info!("No entry found.");
},
Err(e) => {
2016-02-15 16:16:53 +00:00
info!("No entry.");
trace_error(&e);
},
}
2016-02-15 16:16:53 +00:00
}
fn list(id: &str, rt: &Runtime) {
let path = {
match build_entry_path(rt.store(), id) {
Err(e) => {
trace_error(&e);
exit(1);
},
Ok(s) => s,
}
};
2016-02-15 16:16:53 +00:00
debug!("path = {:?}", path);
let entry = match rt.store().get(path.clone()) {
Ok(Some(e)) => e,
Ok(None) => {
info!("No entry found.");
exit(1);
},
Err(e) => {
debug!("Could not get '{:?}' => {:?}", id, path);
warn!("Could not get entry '{}'", id);
trace_error(&e);
exit(1);
},
};
2016-02-15 16:16:53 +00:00
let scmd = rt.cli().subcommand_matches("list").unwrap(); // safe, we checked in main()
let json_out = scmd.is_present("json");
let line_out = scmd.is_present("linewise");
let sepp_out = scmd.is_present("sep");
let mut comm_out = scmd.is_present("commasep");
2016-03-06 17:52:01 +00:00
if !vec![json_out, line_out, comm_out, sepp_out].iter().any(|v| *v) {
2016-02-15 16:16:53 +00:00
// None of the flags passed, go to default
comm_out = true;
}
let tags = entry.get_tags();
if tags.is_err() {
trace_error(&tags.unwrap_err());
2016-02-15 16:16:53 +00:00
exit(1);
}
let tags = tags.unwrap();
if json_out {
unimplemented!()
}
if line_out {
for tag in &tags {
println!("{}", tag);
}
}
if sepp_out {
let sepp = scmd.value_of("sep").unwrap(); // we checked before
println!("{}", tags.join(sepp));
}
if comm_out {
println!("{}", tags.join(", "));
}
2016-02-15 13:33:30 +00:00
}
2016-02-15 16:16:53 +00:00