2016-04-16 20:46:46 +00:00
|
|
|
#![deny(
|
|
|
|
non_camel_case_types,
|
|
|
|
non_snake_case,
|
|
|
|
path_statements,
|
|
|
|
trivial_numeric_casts,
|
|
|
|
unstable_features,
|
|
|
|
unused_allocation,
|
|
|
|
unused_import_braces,
|
|
|
|
unused_imports,
|
|
|
|
unused_must_use,
|
|
|
|
unused_mut,
|
|
|
|
unused_qualifications,
|
|
|
|
while_true,
|
|
|
|
)]
|
|
|
|
|
2016-02-23 11:06:28 +00:00
|
|
|
#[macro_use] extern crate log;
|
|
|
|
extern crate clap;
|
|
|
|
#[macro_use] extern crate semver;
|
|
|
|
extern crate toml;
|
2016-04-12 15:49:23 +00:00
|
|
|
extern crate url;
|
2016-02-23 11:06:28 +00:00
|
|
|
#[macro_use] extern crate version;
|
|
|
|
|
2016-04-16 19:56:16 +00:00
|
|
|
extern crate libimagentrylink;
|
2016-02-23 11:06:28 +00:00
|
|
|
extern crate libimagrt;
|
|
|
|
extern crate libimagstore;
|
|
|
|
extern crate libimagutil;
|
|
|
|
|
2016-02-23 11:36:32 +00:00
|
|
|
use std::process::exit;
|
|
|
|
use std::ops::Deref;
|
|
|
|
|
|
|
|
use libimagrt::runtime::Runtime;
|
|
|
|
use libimagstore::error::StoreError;
|
|
|
|
use libimagstore::store::Entry;
|
|
|
|
use libimagstore::store::FileLockEntry;
|
2016-02-23 11:06:28 +00:00
|
|
|
use libimagstore::store::Store;
|
2016-02-23 11:36:32 +00:00
|
|
|
use libimagutil::trace::trace_error;
|
2016-04-16 19:56:16 +00:00
|
|
|
use libimagentrylink::external::ExternalLinker;
|
2016-04-12 15:49:23 +00:00
|
|
|
use clap::ArgMatches;
|
|
|
|
use url::Url;
|
2016-02-23 11:06:28 +00:00
|
|
|
|
2016-02-23 11:15:36 +00:00
|
|
|
mod ui;
|
|
|
|
|
|
|
|
use ui::build_ui;
|
|
|
|
|
2016-02-23 11:04:41 +00:00
|
|
|
fn main() {
|
2016-02-23 11:36:32 +00:00
|
|
|
let name = "imag-link";
|
|
|
|
let version = &version!()[..];
|
|
|
|
let about = "Link entries";
|
|
|
|
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:05:34 +00:00
|
|
|
println!("{:?}", rt.unwrap_err());
|
2016-02-23 11:36:32 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
rt.cli()
|
|
|
|
.subcommand_name()
|
|
|
|
.map(|name| {
|
|
|
|
match name {
|
|
|
|
"internal" => handle_internal_linking(&rt),
|
2016-04-12 15:49:23 +00:00
|
|
|
"external" => handle_external_linking(&rt),
|
2016-02-23 11:36:32 +00:00
|
|
|
_ => {
|
|
|
|
warn!("No commandline call");
|
|
|
|
exit(1);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_internal_linking(rt: &Runtime) {
|
2016-04-16 19:56:16 +00:00
|
|
|
use libimagentrylink::internal::InternalLinker;
|
2016-02-23 11:36:32 +00:00
|
|
|
use libimagutil::trace::trace_error;
|
|
|
|
|
2016-04-12 19:12:38 +00:00
|
|
|
debug!("Handle internal linking call");
|
|
|
|
let cmd = rt.cli().subcommand_matches("internal").unwrap();
|
2016-02-23 11:36:32 +00:00
|
|
|
|
2016-04-12 19:12:38 +00:00
|
|
|
if cmd.is_present("list") {
|
|
|
|
debug!("List...");
|
|
|
|
for entry in cmd.value_of("list").unwrap().split(",") {
|
|
|
|
debug!("Listing for '{}'", entry);
|
|
|
|
match get_entry_by_name(rt, entry) {
|
|
|
|
Ok(e) => {
|
|
|
|
e.get_internal_links()
|
|
|
|
.map(|links| {
|
|
|
|
let mut i = 0;
|
|
|
|
for link in links.iter().map(|l| l.to_str()).filter_map(|x| x) {
|
|
|
|
println!("{: <3}: {}", i, link);
|
|
|
|
i += 1;
|
|
|
|
}
|
2016-04-16 20:46:29 +00:00
|
|
|
})
|
|
|
|
.map_err(|e| trace_error(&e))
|
|
|
|
.ok();
|
2016-04-12 19:12:38 +00:00
|
|
|
},
|
2016-02-23 11:36:32 +00:00
|
|
|
|
2016-04-12 19:12:38 +00:00
|
|
|
Err(e) => {
|
2016-02-23 11:36:32 +00:00
|
|
|
trace_error(&e);
|
2016-04-12 19:12:38 +00:00
|
|
|
break;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug!("Listing ready!");
|
|
|
|
} else {
|
|
|
|
let mut from = {
|
2016-04-16 20:46:39 +00:00
|
|
|
let from = get_from_entry(&rt);
|
2016-04-12 19:12:38 +00:00
|
|
|
if from.is_none() {
|
|
|
|
warn!("No 'from' entry");
|
|
|
|
exit(1);
|
2016-02-23 11:36:32 +00:00
|
|
|
}
|
2016-04-12 19:12:38 +00:00
|
|
|
from.unwrap()
|
|
|
|
};
|
|
|
|
debug!("Link from = {:?}", from.deref());
|
2016-02-23 11:36:32 +00:00
|
|
|
|
2016-04-16 20:46:39 +00:00
|
|
|
let to = {
|
|
|
|
let to = get_to_entries(&rt);
|
2016-04-12 19:12:38 +00:00
|
|
|
if to.is_none() {
|
|
|
|
warn!("No 'to' entry");
|
|
|
|
exit(1);
|
2016-02-23 11:36:32 +00:00
|
|
|
}
|
2016-04-12 19:12:38 +00:00
|
|
|
to.unwrap()
|
|
|
|
};
|
|
|
|
debug!("Link to = {:?}", to.iter().map(|f| f.deref()).collect::<Vec<&Entry>>());
|
|
|
|
|
|
|
|
match cmd.subcommand_name() {
|
|
|
|
Some("add") => {
|
|
|
|
for mut to_entry in to {
|
|
|
|
if let Err(e) = to_entry.add_internal_link(&mut from) {
|
|
|
|
trace_error(&e);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
Some("remove") => {
|
|
|
|
for mut to_entry in to {
|
|
|
|
if let Err(e) = to_entry.remove_internal_link(&mut from) {
|
|
|
|
trace_error(&e);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2016-02-23 11:36:32 +00:00
|
|
|
|
2016-04-12 19:12:38 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2016-02-23 11:36:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_from_entry<'a>(rt: &'a Runtime) -> Option<FileLockEntry<'a>> {
|
|
|
|
rt.cli()
|
|
|
|
.subcommand_matches("internal")
|
|
|
|
.unwrap() // safe, we know there is an "internal" subcommand"
|
2016-04-12 19:56:01 +00:00
|
|
|
.subcommand_matches("add")
|
|
|
|
.unwrap() // safe, we know there is an "add" subcommand
|
2016-02-23 11:36:32 +00:00
|
|
|
.value_of("from")
|
|
|
|
.and_then(|from_name| {
|
|
|
|
match get_entry_by_name(rt, from_name) {
|
2016-04-12 19:56:01 +00:00
|
|
|
Err(e) => {
|
|
|
|
debug!("We couldn't get the entry from name: '{:?}'", from_name);
|
|
|
|
trace_error(&e); None
|
|
|
|
},
|
2016-02-23 11:36:32 +00:00
|
|
|
Ok(e) => Some(e),
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_to_entries<'a>(rt: &'a Runtime) -> Option<Vec<FileLockEntry<'a>>> {
|
|
|
|
rt.cli()
|
|
|
|
.subcommand_matches("internal")
|
|
|
|
.unwrap() // safe, we know there is an "internal" subcommand"
|
2016-04-12 19:56:01 +00:00
|
|
|
.subcommand_matches("add")
|
|
|
|
.unwrap() // safe, we know there is an "add" subcommand
|
2016-02-23 11:36:32 +00:00
|
|
|
.values_of("to")
|
|
|
|
.map(|values| {
|
|
|
|
let mut v = vec![];
|
|
|
|
for entry in values.map(|v| get_entry_by_name(rt, v)) {
|
|
|
|
match entry {
|
|
|
|
Err(e) => trace_error(&e),
|
|
|
|
Ok(e) => v.push(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_entry_by_name<'a>(rt: &'a Runtime, name: &str) -> Result<FileLockEntry<'a>, StoreError> {
|
|
|
|
use libimagstore::storeid::build_entry_path;
|
|
|
|
build_entry_path(rt.store(), name)
|
|
|
|
.and_then(|path| rt.store().retrieve(path))
|
2016-02-23 11:04:41 +00:00
|
|
|
}
|
2016-02-23 11:06:28 +00:00
|
|
|
|
2016-04-12 15:49:23 +00:00
|
|
|
fn handle_external_linking(rt: &Runtime) {
|
|
|
|
use libimagutil::trace::trace_error;
|
|
|
|
|
|
|
|
let scmd = rt.cli().subcommand_matches("external").unwrap();
|
|
|
|
let entry_name = scmd.value_of("id").unwrap(); // enforced by clap
|
|
|
|
let entry = get_entry_by_name(rt, entry_name);
|
|
|
|
if entry.is_err() {
|
2016-04-17 19:05:34 +00:00
|
|
|
trace_error(&entry.unwrap_err());
|
2016-04-12 15:49:23 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
let mut entry = entry.unwrap();
|
|
|
|
|
|
|
|
if scmd.is_present("add") {
|
2016-04-12 20:27:57 +00:00
|
|
|
debug!("Adding link to entry!");
|
2016-04-12 15:49:23 +00:00
|
|
|
add_link_to_entry(rt.store(), scmd, &mut entry);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if scmd.is_present("remove") {
|
2016-04-12 20:27:57 +00:00
|
|
|
debug!("Removing link from entry!");
|
2016-04-12 15:49:23 +00:00
|
|
|
remove_link_from_entry(rt.store(), scmd, &mut entry);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if scmd.is_present("set") {
|
2016-04-12 20:27:57 +00:00
|
|
|
debug!("Setting links in entry!");
|
2016-04-12 15:49:23 +00:00
|
|
|
set_links_for_entry(rt.store(), scmd, &mut entry);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if scmd.is_present("list") {
|
2016-04-12 20:27:57 +00:00
|
|
|
debug!("Listing links in entry!");
|
2016-04-12 15:49:23 +00:00
|
|
|
list_links_for_entry(rt.store(), &mut entry);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
panic!("Clap failed to enforce one of 'add', 'remove', 'set' or 'list'");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_link_to_entry(store: &Store, matches: &ArgMatches, entry: &mut FileLockEntry) {
|
|
|
|
let link = matches.value_of("add").unwrap();
|
|
|
|
|
|
|
|
let link = Url::parse(link);
|
|
|
|
if link.is_err() {
|
2016-04-12 20:27:57 +00:00
|
|
|
debug!("URL parsing error...");
|
2016-04-17 19:05:34 +00:00
|
|
|
trace_error(&link.unwrap_err());
|
2016-04-12 20:27:57 +00:00
|
|
|
debug!("Exiting");
|
2016-04-12 15:49:23 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
let link = link.unwrap();
|
|
|
|
|
|
|
|
if let Err(e) = entry.add_external_link(store, link) {
|
2016-04-12 20:27:57 +00:00
|
|
|
debug!("Error while adding external link...");
|
2016-04-12 15:49:23 +00:00
|
|
|
trace_error(&e);
|
|
|
|
} else {
|
2016-04-12 20:27:57 +00:00
|
|
|
debug!("Everything worked well");
|
2016-04-12 15:49:23 +00:00
|
|
|
info!("Ok");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn remove_link_from_entry(store: &Store, matches: &ArgMatches, entry: &mut FileLockEntry) {
|
|
|
|
let link = matches.value_of("remove").unwrap();
|
|
|
|
|
|
|
|
let link = Url::parse(link);
|
|
|
|
if link.is_err() {
|
2016-04-17 19:05:34 +00:00
|
|
|
trace_error(&link.unwrap_err());
|
2016-04-12 15:49:23 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
let link = link.unwrap();
|
|
|
|
|
|
|
|
if let Err(e) = entry.remove_external_link(store, link) {
|
|
|
|
trace_error(&e);
|
|
|
|
} else {
|
|
|
|
info!("Ok");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_links_for_entry(store: &Store, matches: &ArgMatches, entry: &mut FileLockEntry) {
|
|
|
|
let links = matches
|
|
|
|
.value_of("links")
|
|
|
|
.map(String::from)
|
|
|
|
.unwrap()
|
|
|
|
.split(",")
|
|
|
|
.map(|uri| {
|
|
|
|
match Url::parse(uri) {
|
|
|
|
Err(e) => {
|
|
|
|
warn!("Could not parse '{}' as URL, ignoring", uri);
|
|
|
|
trace_error(&e);
|
|
|
|
None
|
|
|
|
},
|
|
|
|
Ok(u) => Some(u),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter_map(|x| x)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
if let Err(e) = entry.set_external_links(store, links) {
|
|
|
|
trace_error(&e);
|
|
|
|
} else {
|
|
|
|
info!("Ok");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn list_links_for_entry(store: &Store, entry: &mut FileLockEntry) {
|
|
|
|
let res = entry.get_external_links(store)
|
|
|
|
.and_then(|links| {
|
|
|
|
let mut i = 0;
|
|
|
|
for link in links {
|
|
|
|
println!("{: <3}: {}", i, link);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
|
|
|
|
match res {
|
|
|
|
Err(e) => {
|
|
|
|
trace_error(&e);
|
|
|
|
},
|
|
|
|
Ok(_) => {
|
|
|
|
info!("Ok");
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|