Merge pull request #1073 from matthiasbeyer/imag-bookmark/configurable

Make default bookmark collection configurable via imagrc.toml
This commit is contained in:
Matthias Beyer 2017-09-16 09:52:08 +02:00 committed by GitHub
commit 26a0038ac2
4 changed files with 56 additions and 15 deletions

View file

@ -17,6 +17,8 @@ homepage = "http://imag-pim.org"
clap = ">=2.17" clap = ">=2.17"
log = "0.3" log = "0.3"
version = "2.0.1" version = "2.0.1"
toml = "0.4"
toml-query = "0.3.1"
libimagrt = { version = "0.4.0", path = "../../../lib/core/libimagrt" } libimagrt = { version = "0.4.0", path = "../../../lib/core/libimagrt" }
libimagerror = { version = "0.4.0", path = "../../../lib/core/libimagerror" } libimagerror = { version = "0.4.0", path = "../../../lib/core/libimagerror" }

View file

@ -35,6 +35,8 @@
extern crate clap; extern crate clap;
#[macro_use] extern crate log; #[macro_use] extern crate log;
#[macro_use] extern crate version; #[macro_use] extern crate version;
extern crate toml;
extern crate toml_query;
extern crate libimagbookmark; extern crate libimagbookmark;
extern crate libimagrt; extern crate libimagrt;
@ -43,6 +45,9 @@ extern crate libimagutil;
use std::process::exit; use std::process::exit;
use toml::Value;
use toml_query::read::TomlValueReadExt;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagrt::setup::generate_runtime_setup; use libimagrt::setup::generate_runtime_setup;
use libimagbookmark::collection::BookmarkCollection; use libimagbookmark::collection::BookmarkCollection;
@ -79,9 +84,9 @@ fn main() {
fn add(rt: &Runtime) { fn add(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("add").unwrap(); let scmd = rt.cli().subcommand_matches("add").unwrap();
let coll = scmd.value_of("collection").unwrap(); // enforced by clap let coll = get_collection_name(rt, "add", "collection");
BookmarkCollection::get(rt.store(), coll) BookmarkCollection::get(rt.store(), &coll)
.and_then(|mut collection| { .and_then(|mut collection| {
scmd.values_of("urls") scmd.values_of("urls")
.unwrap() // enforced by clap .unwrap() // enforced by clap
@ -97,7 +102,7 @@ fn collection(rt: &Runtime) {
if scmd.is_present("add") { // adding a new collection if scmd.is_present("add") { // adding a new collection
let name = scmd.value_of("add").unwrap(); let name = scmd.value_of("add").unwrap();
if let Ok(_) = BookmarkCollection::new(rt.store(), name) { if let Ok(_) = BookmarkCollection::new(rt.store(), &name) {
info!("Created: {}", name); info!("Created: {}", name);
} else { } else {
warn!("Creating collection {} failed", name); warn!("Creating collection {} failed", name);
@ -107,7 +112,7 @@ fn collection(rt: &Runtime) {
if scmd.is_present("remove") { // remove a collection if scmd.is_present("remove") { // remove a collection
let name = scmd.value_of("remove").unwrap(); let name = scmd.value_of("remove").unwrap();
if let Ok(_) = BookmarkCollection::delete(rt.store(), name) { if let Ok(_) = BookmarkCollection::delete(rt.store(), &name) {
info!("Deleted: {}", name); info!("Deleted: {}", name);
} else { } else {
warn!("Deleting collection {} failed", name); warn!("Deleting collection {} failed", name);
@ -117,10 +122,9 @@ fn collection(rt: &Runtime) {
} }
fn list(rt: &Runtime) { fn list(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("list").unwrap(); let coll = get_collection_name(rt, "list", "collection");
let coll = scmd.value_of("collection").unwrap(); // enforced by clap
BookmarkCollection::get(rt.store(), coll) BookmarkCollection::get(rt.store(), &coll)
.map(|collection| { .map(|collection| {
match collection.links() { match collection.links() {
Ok(links) => { Ok(links) => {
@ -142,9 +146,9 @@ fn list(rt: &Runtime) {
fn remove(rt: &Runtime) { fn remove(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("remove").unwrap(); let scmd = rt.cli().subcommand_matches("remove").unwrap();
let coll = scmd.value_of("collection").unwrap(); // enforced by clap let coll = get_collection_name(rt, "list", "collection");
BookmarkCollection::get(rt.store(), coll) BookmarkCollection::get(rt.store(), &coll)
.map(|mut collection| { .map(|mut collection| {
for url in scmd.values_of("urls").unwrap() { // enforced by clap for url in scmd.values_of("urls").unwrap() { // enforced by clap
collection.remove_link(BookmarkLink::from(url)).map_err(|e| trace_error(&e)).ok(); collection.remove_link(BookmarkLink::from(url)).map_err(|e| trace_error(&e)).ok();
@ -154,3 +158,35 @@ fn remove(rt: &Runtime) {
info!("Ready"); info!("Ready");
} }
fn get_collection_name(rt: &Runtime,
subcommand_name: &str,
collection_argument_name: &str)
-> String
{
rt.cli()
.subcommand_matches(subcommand_name)
.and_then(|scmd| scmd.value_of(collection_argument_name).map(String::from))
.unwrap_or_else(|| {
rt.config()
.map(|cfg| match cfg.config().read("bookmark.default_collection") {
Err(e) => trace_error_exit(&e, 1),
Ok(Some(&Value::String(ref name))) => name.clone(),
Ok(None) => {
error!("Missing config: 'bookmark.default_collection'. Set or use commandline to specify.");
exit(1)
},
Ok(Some(_)) => {
error!("Type error in configuration: 'bookmark.default_collection' should be string");
exit(1)
}
})
.unwrap_or_else(|| {
error!("Failed to read configuration");
exit(1)
})
})
}

View file

@ -30,10 +30,10 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.long("collection") .long("collection")
.short("c") .short("c")
.takes_value(true) .takes_value(true)
.required(true) .required(false)
.multiple(false) .multiple(false)
.value_name("COLLECTION") .value_name("COLLECTION")
.help("Add to this collection")) .help("Add to this collection, if not specified default from config will be used"))
.arg(Arg::with_name("urls") .arg(Arg::with_name("urls")
.long("urls") .long("urls")
.short("u") .short("u")
@ -52,10 +52,10 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.long("collection") .long("collection")
.short("c") .short("c")
.takes_value(true) .takes_value(true)
.required(true) .required(false)
.multiple(false) .multiple(false)
.value_name("COLLECTION") .value_name("COLLECTION")
.help("Remove from this collection")) .help("Remove from this collection, if not specified default from config will be used"))
.arg(Arg::with_name("urls") .arg(Arg::with_name("urls")
.long("urls") .long("urls")
.short("u") .short("u")
@ -87,10 +87,10 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.long("collection") .long("collection")
.short("c") .short("c")
.takes_value(true) .takes_value(true)
.required(true) .required(false)
.multiple(false) .multiple(false)
.value_name("COLLECTION") .value_name("COLLECTION")
.help("Select from this collection")) .help("Select from this collection, if not specified default from config will be used"))
.arg(Arg::with_name("tags") .arg(Arg::with_name("tags")
.long("tags") .long("tags")
.short("t") .short("t")

View file

@ -82,3 +82,6 @@ default_diary = "default"
[diary.diaries.default] [diary.diaries.default]
timed = "minutely" timed = "minutely"
[bookmark]
default_collection = "default"