2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
|
|
|
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; version
|
|
|
|
// 2.1 of the License.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
|
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-05-16 16:59:02 +00:00
|
|
|
extern crate libimagerror;
|
2016-09-05 17:33:39 +00:00
|
|
|
extern crate libimagutil;
|
2016-02-15 13:33:30 +00:00
|
|
|
|
2016-08-26 13:12:06 +00:00
|
|
|
use std::path::PathBuf;
|
2016-02-15 16:16:53 +00:00
|
|
|
|
|
|
|
use libimagrt::runtime::Runtime;
|
2016-05-18 17:06:05 +00:00
|
|
|
use libimagrt::setup::generate_runtime_setup;
|
2016-04-16 19:58:19 +00:00
|
|
|
use libimagentrytag::tagable::Tagable;
|
2016-05-19 14:20:32 +00:00
|
|
|
use libimagentrytag::tag::Tag;
|
2016-07-15 19:56:53 +00:00
|
|
|
use libimagerror::trace::{trace_error, trace_error_exit};
|
2016-05-19 14:20:32 +00:00
|
|
|
use libimagentrytag::ui::{get_add_tags, get_remove_tags};
|
2016-08-26 13:12:06 +00:00
|
|
|
use libimagstore::storeid::StoreId;
|
2016-09-05 17:33:39 +00:00
|
|
|
use libimagutil::warn_exit::warn_exit;
|
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() {
|
2016-05-18 17:06:05 +00:00
|
|
|
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(
|
|
|
|
|| {
|
2016-08-26 13:12:06 +00:00
|
|
|
let id = PathBuf::from(id);
|
2016-05-19 14:20:32 +00:00
|
|
|
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| {
|
2016-08-26 13:12:06 +00:00
|
|
|
let id = PathBuf::from(id);
|
2016-02-15 16:16:53 +00:00
|
|
|
debug!("Call: {}", name);
|
|
|
|
match name {
|
|
|
|
"list" => list(id, &rt),
|
|
|
|
_ => {
|
|
|
|
warn!("Unknown command");
|
|
|
|
// More error handling
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-26 13:12:06 +00:00
|
|
|
fn alter(rt: &Runtime, id: PathBuf, add: Option<Vec<Tag>>, rem: Option<Vec<Tag>>) {
|
2016-03-18 13:38:16 +00:00
|
|
|
let path = {
|
2016-08-26 13:12:06 +00:00
|
|
|
match StoreId::new(Some(rt.store().path().clone()), id) {
|
2016-07-15 19:56:53 +00:00
|
|
|
Err(e) => trace_error_exit(&e, 1),
|
2016-03-18 13:38:16 +00:00
|
|
|
Ok(s) => s,
|
|
|
|
}
|
|
|
|
};
|
2016-02-15 16:16:53 +00:00
|
|
|
debug!("path = {:?}", path);
|
2016-03-18 13:38:16 +00:00
|
|
|
|
2016-05-28 19:19:29 +00:00
|
|
|
match rt.store().get(path) {
|
|
|
|
Ok(Some(mut e)) => {
|
2016-02-15 16:16:53 +00:00
|
|
|
add.map(|tags| {
|
2016-05-14 22:04:13 +00:00
|
|
|
for tag in tags {
|
|
|
|
debug!("Adding tag '{:?}'", tag);
|
|
|
|
if let Err(e) = e.add_tag(tag) {
|
2016-03-06 17:53:10 +00:00
|
|
|
trace_error(&e);
|
|
|
|
}
|
2016-02-15 16:16:53 +00:00
|
|
|
}
|
2016-05-14 22:04:13 +00:00
|
|
|
}); // it is okay to ignore a None here
|
2016-02-15 16:16:53 +00:00
|
|
|
|
|
|
|
rem.map(|tags| {
|
2016-05-14 22:04:13 +00:00
|
|
|
for tag in tags {
|
|
|
|
debug!("Removing tag '{:?}'", tag);
|
|
|
|
if let Err(e) = e.remove_tag(tag) {
|
2016-03-06 17:53:10 +00:00
|
|
|
trace_error(&e);
|
|
|
|
}
|
2016-02-15 16:16:53 +00:00
|
|
|
}
|
2016-05-14 22:04:13 +00:00
|
|
|
}); // it is okay to ignore a None here
|
2016-05-28 19:19:29 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Ok(None) => {
|
|
|
|
info!("No entry found.");
|
|
|
|
},
|
|
|
|
|
|
|
|
Err(e) => {
|
2016-02-15 16:16:53 +00:00
|
|
|
info!("No entry.");
|
2016-03-06 17:53:10 +00:00
|
|
|
trace_error(&e);
|
2016-05-28 19:19:29 +00:00
|
|
|
},
|
|
|
|
}
|
2016-02-15 16:16:53 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 13:12:06 +00:00
|
|
|
fn list(id: PathBuf, rt: &Runtime) {
|
|
|
|
let path = match StoreId::new(Some(rt.store().path().clone()), id) {
|
|
|
|
Err(e) => trace_error_exit(&e, 1),
|
|
|
|
Ok(s) => s,
|
2016-03-18 13:38:16 +00:00
|
|
|
};
|
2016-02-15 16:16:53 +00:00
|
|
|
debug!("path = {:?}", path);
|
|
|
|
|
2016-05-28 19:19:29 +00:00
|
|
|
let entry = match rt.store().get(path.clone()) {
|
|
|
|
Ok(Some(e)) => e,
|
2016-09-05 17:33:39 +00:00
|
|
|
Ok(None) => warn_exit("No entry found.", 1),
|
2016-05-28 19:19:29 +00:00
|
|
|
|
|
|
|
Err(e) => {
|
2016-08-26 13:12:06 +00:00
|
|
|
warn!("Could not get entry '{:?}'", path);
|
2016-07-15 19:56:53 +00:00
|
|
|
trace_error_exit(&e, 1);
|
2016-05-28 19:19:29 +00:00
|
|
|
},
|
|
|
|
};
|
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() {
|
2016-07-15 19:56:53 +00:00
|
|
|
trace_error_exit(&tags.unwrap_err(), 1);
|
2016-02-15 16:16:53 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|