Merge pull request #338 from matthiasbeyer/imag-link/external-linking

Imag link/external linking
This commit is contained in:
Matthias Beyer 2016-04-16 17:56:53 +02:00
commit ff20e79aa1
3 changed files with 148 additions and 8 deletions

View File

@ -9,6 +9,7 @@ clap = "2.1.1"
log = "0.3.5"
version = "2.0.1"
toml = "0.1.25"
url = "0.5.5"
[dependencies.libimagstore]
path = "../libimagstore"

View File

@ -2,6 +2,7 @@
extern crate clap;
#[macro_use] extern crate semver;
extern crate toml;
extern crate url;
#[macro_use] extern crate version;
extern crate libimaglink;
@ -19,6 +20,9 @@ use libimagstore::store::Entry;
use libimagstore::store::FileLockEntry;
use libimagstore::store::Store;
use libimagutil::trace::trace_error;
use libimaglink::external::ExternalLinker;
use clap::ArgMatches;
use url::Url;
mod ui;
@ -49,7 +53,7 @@ fn main() {
.map(|name| {
match name {
"internal" => handle_internal_linking(&rt),
"external" => { unimplemented!() },
"external" => handle_external_linking(&rt),
_ => {
warn!("No commandline call");
exit(1);
@ -177,3 +181,127 @@ fn get_entry_by_name<'a>(rt: &'a Runtime, name: &str) -> Result<FileLockEntry<'a
.and_then(|path| rt.store().retrieve(path))
}
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() {
trace_error(&entry.err().unwrap());
exit(1);
}
let mut entry = entry.unwrap();
if scmd.is_present("add") {
debug!("Adding link to entry!");
add_link_to_entry(rt.store(), scmd, &mut entry);
return;
}
if scmd.is_present("remove") {
debug!("Removing link from entry!");
remove_link_from_entry(rt.store(), scmd, &mut entry);
return;
}
if scmd.is_present("set") {
debug!("Setting links in entry!");
set_links_for_entry(rt.store(), scmd, &mut entry);
return;
}
if scmd.is_present("list") {
debug!("Listing links in entry!");
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() {
debug!("URL parsing error...");
trace_error(&link.err().unwrap());
debug!("Exiting");
exit(1);
}
let link = link.unwrap();
if let Err(e) = entry.add_external_link(store, link) {
debug!("Error while adding external link...");
trace_error(&e);
} else {
debug!("Everything worked well");
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() {
trace_error(&link.err().unwrap());
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");
},
}
}

View File

@ -1,4 +1,4 @@
use clap::{Arg, App, SubCommand};
use clap::{Arg, ArgGroup, App, SubCommand};
pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
app
@ -59,26 +59,37 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.required(true)
.help("Modify external link of this entry"))
.arg(Arg::with_name("set")
.long("set")
.short("s")
.arg(Arg::with_name("add")
.long("add")
.short("a")
.takes_value(true)
.required(false)
.help("Set this URI as external link"))
.help("Add this URI as external link"))
.arg(Arg::with_name("remove")
.long("remove")
.short("r")
.takes_value(false)
.required(false)
.help("Remove external link"))
.help("Remove one external link"))
.arg(Arg::with_name("set")
.long("set")
.short("s")
.takes_value(true)
.required(false)
.help("Set these URIs as external link (seperate by comma)"))
.arg(Arg::with_name("list")
.long("list")
.short("l")
.takes_value(false)
.required(false)
.help("List external link"))
.help("List external links"))
.group(ArgGroup::with_name("external-link-group")
.args(&["add", "remove", "set", "list"])
.required(true))
)
}