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;
|
2016-04-16 19:58:19 +00:00
|
|
|
extern crate libimagentrytag;
|
2016-02-15 13:33:30 +00:00
|
|
|
extern crate libimagutil;
|
|
|
|
|
2016-02-15 16:16:53 +00:00
|
|
|
use std::process::exit;
|
|
|
|
|
|
|
|
use libimagrt::runtime::Runtime;
|
2016-04-16 19:58:19 +00:00
|
|
|
use libimagentrytag::tagable::Tagable;
|
2016-03-18 13:38:16 +00:00
|
|
|
use libimagstore::storeid::build_entry_path;
|
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;
|
|
|
|
|
|
|
|
use libimagutil::trace::trace_error;
|
2016-02-15 13:42:20 +00:00
|
|
|
|
2016-02-15 13:33:30 +00:00
|
|
|
fn main() {
|
2016-02-15 16:16:53 +00:00
|
|
|
let name = "imag-store";
|
|
|
|
let version = &version!()[..];
|
|
|
|
let about = "Direct interface to the store. Use with great care!";
|
|
|
|
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");
|
2016-04-17 19:07:47 +00:00
|
|
|
println!("{:?}", rt.unwrap_err());
|
2016-02-15 16:16:53 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let id = rt.cli().value_of("id").unwrap(); // enforced by clap
|
|
|
|
rt.cli()
|
|
|
|
.subcommand_name()
|
|
|
|
.map_or_else(
|
|
|
|
|| {
|
|
|
|
let add = rt.cli().value_of("add");
|
|
|
|
let rem = rt.cli().value_of("remove");
|
|
|
|
let set = rt.cli().value_of("set");
|
|
|
|
|
|
|
|
alter(&rt, id, add, rem, set);
|
|
|
|
},
|
|
|
|
|name| {
|
|
|
|
debug!("Call: {}", name);
|
|
|
|
match name {
|
|
|
|
"list" => list(id, &rt),
|
|
|
|
_ => {
|
|
|
|
warn!("Unknown command");
|
|
|
|
// More error handling
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn alter(rt: &Runtime, id: &str, add: Option<&str>, rem: Option<&str>, set: Option<&str>) {
|
2016-03-18 13:38:16 +00:00
|
|
|
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);
|
2016-03-18 13:38:16 +00:00
|
|
|
|
2016-02-15 16:16:53 +00:00
|
|
|
rt.store()
|
|
|
|
// "id" must be present, enforced via clap spec
|
|
|
|
.retrieve(path)
|
|
|
|
.map(|mut e| {
|
|
|
|
add.map(|tags| {
|
|
|
|
let tags = tags.split(",");
|
|
|
|
for tag in tags {
|
|
|
|
info!("Adding tag '{}'", tag);
|
2016-03-06 17:53:10 +00:00
|
|
|
if let Err(e) = e.add_tag(String::from(tag)) {
|
|
|
|
trace_error(&e);
|
|
|
|
}
|
2016-02-15 16:16:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
rem.map(|tags| {
|
|
|
|
let tags = tags.split(",");
|
|
|
|
for tag in tags {
|
|
|
|
info!("Removing tag '{}'", tag);
|
2016-03-06 17:53:10 +00:00
|
|
|
if let Err(e) = e.remove_tag(String::from(tag)) {
|
|
|
|
trace_error(&e);
|
|
|
|
}
|
2016-02-15 16:16:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
set.map(|tags| {
|
|
|
|
info!("Setting tags '{}'", tags);
|
|
|
|
let tags = tags.split(",").map(String::from).collect();
|
2016-03-06 17:53:10 +00:00
|
|
|
if let Err(e) = e.set_tags(tags) {
|
|
|
|
trace_error(&e);
|
|
|
|
}
|
2016-02-15 16:16:53 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.map_err(|e| {
|
|
|
|
info!("No entry.");
|
2016-03-06 17:53:10 +00:00
|
|
|
trace_error(&e);
|
|
|
|
})
|
|
|
|
.ok();
|
2016-02-15 16:16:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn list(id: &str, rt: &Runtime) {
|
2016-03-18 13:38:16 +00:00
|
|
|
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 = rt.store().retrieve(path.clone());
|
|
|
|
if entry.is_err() {
|
|
|
|
debug!("Could not retrieve '{:?}' => {:?}", id, path);
|
|
|
|
warn!("Could not retrieve entry '{}'", id);
|
2016-04-17 19:07:47 +00:00
|
|
|
trace_error(&entry.unwrap_err());
|
2016-02-15 16:16:53 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
let entry = entry.unwrap();
|
|
|
|
|
|
|
|
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() {
|
2016-04-17 19:07:47 +00:00
|
|
|
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
|
|
|
|