imag-grep: implement ImagApplication

Signed-off-by: Leon Schuermann <leon@is.currently.online>
This commit is contained in:
Leon Schuermann 2019-09-14 18:41:21 +02:00 committed by Matthias Beyer
parent 58ac2caf20
commit a7d55930d7
3 changed files with 113 additions and 44 deletions

View File

@ -22,6 +22,7 @@ maintenance = { status = "actively-developed" }
[dependencies]
log = "0.4.6"
regex = "1.1.7"
failure = "0.1.5"
libimagstore = { version = "0.10.0", path = "../../../lib/core/libimagstore" }
libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" }
@ -32,3 +33,10 @@ version = "2.33.0"
default-features = false
features = ["color", "suggestions", "wrap_help"]
[lib]
name = "libimaggrepcmd"
path = "src/lib.rs"
[[bin]]
name = "imag-grep"
path = "src/bin.rs"

View File

@ -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);

View File

@ -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);
/// 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 opts = Options {
files_with_matches : rt.cli().is_present("files-with-matches"),
count : rt.cli().is_present("count"),
};
let mut count : usize = 0;
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 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();
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();
}
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")
}
}