From 750f4cd31c210057f9de973359e2a4206a5ea140 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 25 Oct 2015 19:56:04 +0100 Subject: [PATCH] Start adding the BM submodule --- etc/cli.yml | 65 +++++++++++++++++++++++++++++ src/main.rs | 16 ++++++- src/module/bm/mod.rs | 99 ++++++++++++++++++++++++++++++++++++++++++++ src/module/mod.rs | 4 ++ 4 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 src/module/bm/mod.rs diff --git a/etc/cli.yml b/etc/cli.yml index d86660ea..93ab5bf0 100644 --- a/etc/cli.yml +++ b/etc/cli.yml @@ -113,6 +113,71 @@ subcommands: help: Sets the level of debugging information required: false + subcommands: + - add: + about: Add bookmark + version: 0.1 + author: Matthias Beyer + args: + - url: + short: u + long: url + help: Add a new URL as bookmark + required: true + takes_value: true + + - tags: + short: t + long: tags + help: Add these tags to the URL + required: false + takes_value: true + + - list: + about: List bookmarks + version: 0.1 + author: Matthias Beyer + args: + - match: + short: m + long: match + help: Match for regex + required: false + takes_value: true + + - tags: + short: t + long: tags + help: Filter for these tags + required: false + takes_value: true + + - remove: + about: Remove bookmark(s) + version: 0.1 + author: Matthias Beyer + args: + - id: + long: id + help: Delete Bookmark with ID + required: false + takes_value: true + + - match: + short: m + long: match + help: Match for regex + required: false + takes_value: true + + - tags: + short: t + long: tags + help: Filter for these tags + required: false + takes_value: true + + - todo: about: Todo module version: 0.1 diff --git a/src/main.rs b/src/main.rs index f073866d..5d435b48 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,14 +4,16 @@ #[macro_use] extern crate serde_json; #[macro_use] extern crate glob; #[macro_use] extern crate uuid; -#[macro_use] extern crate prettytable; +#[macro_use] extern crate regex; extern crate config; -extern crate regex; use cli::CliConfig; use configuration::Configuration; use runtime::{ImagLogger, Runtime}; use clap::App; +use module::Module; +use module::ModuleError; +use module::bm::BMModule; mod cli; mod configuration; @@ -36,5 +38,15 @@ fn main() { debug!("Runtime : {:?}", &rt); + if let Some(matches) = rt.config.cli_matches.subcommand_matches("bm") { + let module : BMModule = Module::new(&rt); + module.execute(&rt); + module.shutdown(&rt); + } else { + // Err(ModuleError::mk("No commandline call")) + info!("No commandline call...") + } + + info!("Hello, world!"); } diff --git a/src/module/bm/mod.rs b/src/module/bm/mod.rs new file mode 100644 index 00000000..418b6cff --- /dev/null +++ b/src/module/bm/mod.rs @@ -0,0 +1,99 @@ +use runtime::Runtime; +use module::Module; +use module::ModuleResult; +use module::ModuleError; +use std::path::Path; +use std::result::Result; +use clap::ArgMatches; +use regex::Regex; + +pub struct BMModule { + path: Option, +} + +const CALLNAMES : &'static [&'static str] = &[ "bm", "bookmark" ]; + +impl Module for BMModule { + + fn new(rt : &Runtime) -> BMModule { + BMModule { + path: None + } + } + + fn callnames() -> &'static [&'static str] { + CALLNAMES + } + + fn name(&self) -> &'static str{ + "Bookmark" + } + + fn execute(&self, rt : &Runtime) -> ModuleResult { + let cmd = rt.config.cli_matches.subcommand_matches("bm").unwrap(); + match cmd.subcommand_name() { + Some("add") => { add(rt, cmd.subcommand_matches("add").unwrap()) } + Some("list") => { list(rt, cmd.subcommand_matches("list").unwrap()) } + Some("remove") => { list(rt, cmd.subcommand_matches("remove").unwrap()) } + _ => { + info!("Not calling any of add, list, remove"); + Ok(()) + } + } + } + + fn shutdown(&self, rt : &Runtime) -> ModuleResult { + Ok(()) + } +} + +fn add<'a>(rt: &Runtime, sub: &ArgMatches<'a, 'a>) -> ModuleResult { + let url = sub.value_of("url").unwrap(); + let tags = get_tags(rt, sub); + + Ok(()) +} + +fn list<'a>(rt: &Runtime, sub: &ArgMatches<'a, 'a>) -> ModuleResult { + let tags = get_tags(rt, sub); + let matcher = get_matcher(rt, sub); + + Ok(()) +} + +fn remove<'a>(rt: &Runtime, sub: &ArgMatches<'a, 'a>) -> ModuleResult { + let tags = get_tags(rt, sub); + let matcher = get_matcher(rt, sub); + let id = get_id(rt, sub); + + Ok(()) +} + +fn get_tags<'a>(rt: &Runtime, sub: &ArgMatches<'a, 'a>) -> Option> { + sub.value_of("tags").and_then(|tags| + Some(tags.split(",") + .collect::>() + .iter() + .map(|s| s.to_string()) + .collect() + ) + ) + +} + +fn get_matcher<'a>(rt: &Runtime, sub: &ArgMatches<'a, 'a>) -> Option { + if let Some(s) = sub.value_of("match") { + if let Ok(r) = Regex::new(s) { + return Some(r) + } else { + error!("Regex error, continuing without regex"); + } + } + None + +} + +fn get_id<'a>(rt: &Runtime, sub: &ArgMatches<'a, 'a>) -> Option { + sub.value_of("id").and_then(|s| Some(String::from(s))) +} + diff --git a/src/module/mod.rs b/src/module/mod.rs index ceca732f..e846cd57 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -7,7 +7,11 @@ use std::result::Result; use storage::backend::StorageBackend; use self::command::ExecutableCommand; +use module::todo::TodoModule; + mod command; +pub mod todo; +pub mod bm; #[derive(Debug)] pub struct ModuleError {