From 46b7ae9384bcd31b8910eb3bc061eaecad810227 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 28 Dec 2015 20:17:43 +0100 Subject: [PATCH] Add BM::command_add_tags() --- etc/cli.yml | 31 +++++++++++++++++++++++++++++ src/module/bm/mod.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/etc/cli.yml b/etc/cli.yml index 6d36b58b..2e7db1ae 100644 --- a/etc/cli.yml +++ b/etc/cli.yml @@ -118,6 +118,37 @@ subcommands: required: false takes_value: true + - add_tags: + about: Add tags to bookmark(s) + version: 0.1 + author: Matthias Beyer + args: + - with_id: + long: with-id + help: Add tags to bookmark with ID + required: false + takes_value: true + + - with_match: + short: m + long: with-match + help: Add tags to bookmark(s) which match this regex + required: false + takes_value: true + + - with_tags: + long: with-tags + help: Add tags to bookmark(s) which have these tag(s) + required: false + takes_value: true + + - tags: + short: t + long: tags + help: Add these tags + required: true + takes_value: true + - todo: about: Todo module version: 0.1 diff --git a/src/module/bm/mod.rs b/src/module/bm/mod.rs index 0d56665c..ba808229 100644 --- a/src/module/bm/mod.rs +++ b/src/module/bm/mod.rs @@ -152,6 +152,49 @@ impl<'a> BM<'a> { return result; } + fn command_add_tags(&self, matches: &ArgMatches) -> bool { + use self::header::set_tags_in_header; + + let tags = matches.value_of("tags") + .map(|ts| { + ts.split(",") + .map(String::from) + .collect::>() + }) + .unwrap_or(vec![]); + let (filter, files) = self.get_files(matches, "with_id", "with_match", "with_tags"); + + if tags.len() == 0 { + error!("No tags to add, exiting."); + exit(1); + } + + if !filter { + warn!("There were no filter applied when loading the files"); + } + + let result = files + .iter() + .map(|file| { + debug!("Adding tags to file: {:?}", file); + let f = file.deref().borrow(); + let hdr = f.header(); + let mut ts = get_tags_from_header(hdr); + let mut append_tags = tags.clone(); + ts.append(&mut append_tags); + set_tags_in_header(hdr, ts); + true + }) + .all(|x| x); + + if result { + info!("Adding tags to links succeeded"); + } else { + error!("Adding tags to links failed"); + } + + return result; + } fn get_files(&self, matches: &ArgMatches, @@ -248,6 +291,10 @@ impl<'a> Module<'a> for BM<'a> { self.command_remove(matches.subcommand_matches("remove").unwrap()) }, + Some("add_tags") => { + self.command_add_tags(matches.subcommand_matches("add_tags").unwrap()) + }, + Some(_) | None => { info!("No command given, doing nothing"); false