imag-grep: implement ImagApplication
Signed-off-by: Leon Schuermann <leon@is.currently.online>
This commit is contained in:
parent
58ac2caf20
commit
a7d55930d7
3 changed files with 113 additions and 44 deletions
|
@ -22,6 +22,7 @@ maintenance = { status = "actively-developed" }
|
||||||
[dependencies]
|
[dependencies]
|
||||||
log = "0.4.6"
|
log = "0.4.6"
|
||||||
regex = "1.1.7"
|
regex = "1.1.7"
|
||||||
|
failure = "0.1.5"
|
||||||
|
|
||||||
libimagstore = { version = "0.10.0", path = "../../../lib/core/libimagstore" }
|
libimagstore = { version = "0.10.0", path = "../../../lib/core/libimagstore" }
|
||||||
libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" }
|
libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" }
|
||||||
|
@ -32,3 +33,10 @@ version = "2.33.0"
|
||||||
default-features = false
|
default-features = false
|
||||||
features = ["color", "suggestions", "wrap_help"]
|
features = ["color", "suggestions", "wrap_help"]
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
name = "libimaggrepcmd"
|
||||||
|
path = "src/lib.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "imag-grep"
|
||||||
|
path = "src/bin.rs"
|
||||||
|
|
39
bin/core/imag-grep/src/bin.rs
Normal file
39
bin/core/imag-grep/src/bin.rs
Normal 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);
|
|
@ -37,17 +37,20 @@
|
||||||
#[macro_use] extern crate log;
|
#[macro_use] extern crate log;
|
||||||
extern crate clap;
|
extern crate clap;
|
||||||
extern crate regex;
|
extern crate regex;
|
||||||
|
extern crate failure;
|
||||||
|
|
||||||
extern crate libimagstore;
|
extern crate libimagstore;
|
||||||
#[macro_use] extern crate libimagrt;
|
extern crate libimagrt;
|
||||||
extern crate libimagerror;
|
extern crate libimagerror;
|
||||||
|
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
use failure::Fallible as Result;
|
||||||
|
use clap::App;
|
||||||
|
|
||||||
use libimagrt::runtime::Runtime;
|
use libimagrt::runtime::Runtime;
|
||||||
use libimagrt::setup::generate_runtime_setup;
|
use libimagrt::application::ImagApplication;
|
||||||
use libimagstore::store::Entry;
|
use libimagstore::store::Entry;
|
||||||
use libimagerror::trace::MapErrTrace;
|
use libimagerror::trace::MapErrTrace;
|
||||||
use libimagerror::exit::ExitUnwrap;
|
use libimagerror::exit::ExitUnwrap;
|
||||||
|
@ -60,53 +63,72 @@ struct Options {
|
||||||
count: bool,
|
count: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
/// Marker enum for implementing ImagApplication on
|
||||||
let version = make_imag_version!();
|
///
|
||||||
let rt = generate_runtime_setup("imag-grep",
|
/// This is used by binaries crates to execute business logic
|
||||||
&version,
|
/// or to build a CLI completion.
|
||||||
"grep through entries text",
|
pub enum ImagGrep {}
|
||||||
ui::build_ui);
|
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 {
|
let mut count : usize = 0;
|
||||||
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 pattern = rt
|
let overall_count = rt
|
||||||
.cli()
|
.store()
|
||||||
.value_of("pattern")
|
.entries()
|
||||||
.map(Regex::new)
|
.map_err_trace_exit_unwrap()
|
||||||
.unwrap() // ensured by clap
|
.into_get_iter()
|
||||||
.unwrap_or_else(|e| {
|
.filter_map(|res| res.map_err_trace_exit_unwrap())
|
||||||
error!("Regex building error: {:?}", e);
|
.filter_map(|entry| if pattern.is_match(entry.get_content()) {
|
||||||
::std::process::exit(1)
|
show(&rt, &entry, &pattern, &opts, &mut count);
|
||||||
});
|
Some(())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
})
|
||||||
|
.count();
|
||||||
|
|
||||||
let overall_count = rt
|
if opts.count {
|
||||||
.store()
|
writeln!(rt.stdout(), "{}", count).to_exit_code().unwrap_or_exit();
|
||||||
.entries()
|
} else if !opts.files_with_matches {
|
||||||
.map_err_trace_exit_unwrap()
|
writeln!(rt.stdout(), "Processed {} files, {} matches, {} nonmatches",
|
||||||
.into_get_iter()
|
overall_count,
|
||||||
.filter_map(|res| res.map_err_trace_exit_unwrap())
|
count,
|
||||||
.filter_map(|entry| if pattern.is_match(entry.get_content()) {
|
overall_count - count)
|
||||||
show(&rt, &entry, &pattern, &opts, &mut count);
|
.to_exit_code()
|
||||||
Some(())
|
.unwrap_or_exit();
|
||||||
} else {
|
}
|
||||||
None
|
|
||||||
})
|
|
||||||
.count();
|
|
||||||
|
|
||||||
if opts.count {
|
Ok(())
|
||||||
writeln!(rt.stdout(), "{}", count).to_exit_code().unwrap_or_exit();
|
}
|
||||||
} else if !opts.files_with_matches {
|
|
||||||
writeln!(rt.stdout(), "Processed {} files, {} matches, {} nonmatches",
|
fn build_cli<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
|
||||||
overall_count,
|
ui::build_ui(app)
|
||||||
count,
|
}
|
||||||
overall_count - count)
|
|
||||||
.to_exit_code()
|
fn name() -> &'static str {
|
||||||
.unwrap_or_exit();
|
env!("CARGO_PKG_NAME")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn description() -> &'static str {
|
||||||
|
"grep through entries text"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn version() -> &'static str {
|
||||||
|
env!("CARGO_PKG_VERSION")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue