summaryrefslogtreecommitdiffstats
path: root/bin/core/imag-grep/src
diff options
context:
space:
mode:
authorLeon Schuermann <leon@is.currently.online>2019-09-14 18:41:21 +0200
committerMatthias Beyer <mail@beyermatthias.de>2019-10-26 14:41:31 +0200
commita7d55930d7303cbced9f9d53b355672b0415d900 (patch)
tree0a8d43e1c8051cf0503f2353c5f02935977b726d /bin/core/imag-grep/src
parent58ac2caf203e66d2b431ca3e0633fd407fd38a1c (diff)
imag-grep: implement ImagApplication
Signed-off-by: Leon Schuermann <leon@is.currently.online>
Diffstat (limited to 'bin/core/imag-grep/src')
-rw-r--r--bin/core/imag-grep/src/bin.rs39
-rw-r--r--bin/core/imag-grep/src/lib.rs (renamed from bin/core/imag-grep/src/main.rs)120
2 files changed, 110 insertions, 49 deletions
diff --git a/bin/core/imag-grep/src/bin.rs b/bin/core/imag-grep/src/bin.rs
new file mode 100644
index 00000000..aacc4628
--- /dev/null
+++ b/bin/core/imag-grep/src/bin.rs
@@ -0,0 +1,39 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; version
+// 2.1 of the License.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+//
+
+#![forbid(unsafe_code)]
+
+#![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,
+)]
+
+#[macro_use] extern crate libimagrt;
+
+simple_imag_application_binary!(libimaggrepcmd, ImagGrep);
diff --git a/bin/core/imag-grep/src/main.rs b/bin/core/imag-grep/src/lib.rs
index a7b1a1f3..f7a4a2d1 100644
--- a/bin/core/imag-grep/src/main.rs
+++ b/bin/core/imag-grep/src/lib.rs
@@ -37,17 +37,20 @@
#[macro_use] extern crate log;
extern crate clap;
extern crate regex;
+extern crate failure;
extern crate libimagstore;
-#[macro_use] extern crate libimagrt;
+extern crate libimagrt;
extern crate libimagerror;
use std::io::Write;
use regex::Regex;
+use failure::Fallible as Result;
+use clap::App;
use libimagrt::runtime::Runtime;
-use libimagrt::setup::generate_runtime_setup;
+use libimagrt::application::ImagApplication;
use libimagstore::store::Entry;
use libimagerror::trace::MapErrTrace;
use libimagerror::exit::ExitUnwrap;
@@ -60,53 +63,72 @@ struct Options {
count: bool,
}
-fn main() {
- let version = make_imag_version!();
- let rt = generate_runtime_setup("imag-grep",
- &version,
- "grep through entries text",
- ui::build_ui);
-
- let opts = Options {
- files_with_matches : rt.cli().is_present("files-with-matches"),
- count : rt.cli().is_present("count"),
- };
-
- let mut count : usize = 0;
-
- let pattern = rt
- .cli()
- .value_of("pattern")
- .map(Regex::new)
- .unwrap() // ensured by clap
- .unwrap_or_else(|e| {
- error!("Regex building error: {:?}", e);
- ::std::process::exit(1)
- });
-
- let overall_count = rt
- .store()
- .entries()
- .map_err_trace_exit_unwrap()
- .into_get_iter()
- .filter_map(|res| res.map_err_trace_exit_unwrap())
- .filter_map(|entry| if pattern.is_match(entry.get_content()) {
- show(&rt, &entry, &pattern, &opts, &mut count);
- Some(())
- } else {
- None
- })
- .count();
-
- if opts.count {
- writeln!(rt.stdout(), "{}", count).to_exit_code().unwrap_or_exit();
- } else if !opts.files_with_matches {
- writeln!(rt.stdout(), "Processed {} files, {} matches, {} nonmatches",
- overall_count,
- count,
- overall_count - count)
- .to_exit_code()
- .unwrap_or_exit();
+/// Marker enum for implementing ImagApplication on
+///
+/// This is used by binaries crates to execute business logic
+/// or to build a CLI completion.
+pub enum ImagGrep {}
+impl ImagApplication for ImagGrep {
+ fn run(rt: Runtime) -> Result<()> {
+ let opts = Options {
+ files_with_matches : rt.cli().is_present("files-with-matches"),
+ count : rt.cli().is_present("count"),
+ };
+
+ let mut count : usize = 0;
+
+ let pattern = rt
+ .cli()
+ .value_of("pattern")
+ .map(Regex::new)
+ .unwrap() // ensured by clap
+ .unwrap_or_else(|e| {
+ error!("Regex building error: {:?}", e);
+ ::std::process::exit(1)
+ });
+
+ let overall_count = rt
+ .store()
+ .entries()
+ .map_err_trace_exit_unwrap()
+ .into_get_iter()
+ .filter_map(|res| res.map_err_trace_exit_unwrap())
+ .filter_map(|entry| if pattern.is_match(entry.get_content()) {
+ show(&rt, &entry, &pattern, &opts, &mut count);
+ Some(())
+ } else {
+ None
+ })
+ .count();
+
+ if opts.count {
+ writeln!(rt.stdout(), "{}", count).to_exit_code().unwrap_or_exit();
+ } else if !opts.files_with_matches {
+ writeln!(rt.stdout(), "Processed {} files, {} matches, {} nonmatches",
+ overall_count,
+ count,
+ overall_count - count)
+ .to_exit_code()
+ .unwrap_or_exit();
+ }
+
+ Ok(())
+ }
+
+ fn build_cli<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
+ ui::build_ui(app)
+ }
+
+ fn name() -> &'static str {
+ env!("CARGO_PKG_NAME")
+ }
+
+ fn description() -> &'static str {
+ "grep through entries text"
+ }
+
+ fn version() -> &'static str {
+ env!("CARGO_PKG_VERSION")
}
}