Convert id-in-collection command to new binary crate layout
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
parent
b6facfff6b
commit
a0c5ea1fda
3 changed files with 95 additions and 29 deletions
|
@ -38,3 +38,10 @@ features = ["color", "suggestions", "wrap_help"]
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
env_logger = "0.7"
|
env_logger = "0.7"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
name = "libimagidincollectioncmd"
|
||||||
|
path = "src/lib.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "imag-id-in-collection"
|
||||||
|
path = "src/bin.rs"
|
||||||
|
|
40
bin/core/imag-id-in-collection/src/bin.rs
Normal file
40
bin/core/imag-id-in-collection/src/bin.rs
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
//
|
||||||
|
// 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!(libimagidincollectioncmd, ImagIdInCollection);
|
||||||
|
|
|
@ -46,7 +46,7 @@ extern crate env_logger;
|
||||||
|
|
||||||
extern crate libimagerror;
|
extern crate libimagerror;
|
||||||
extern crate libimagstore;
|
extern crate libimagstore;
|
||||||
#[macro_use] extern crate libimagrt;
|
extern crate libimagrt;
|
||||||
|
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
|
@ -54,9 +54,11 @@ use filters::filter::Filter;
|
||||||
use failure::Fallible as Result;
|
use failure::Fallible as Result;
|
||||||
use failure::Error;
|
use failure::Error;
|
||||||
use failure::err_msg;
|
use failure::err_msg;
|
||||||
|
use clap::App;
|
||||||
|
|
||||||
use libimagstore::storeid::StoreId;
|
use libimagstore::storeid::StoreId;
|
||||||
use libimagrt::setup::generate_runtime_setup;
|
use libimagrt::application::ImagApplication;
|
||||||
|
use libimagrt::runtime::Runtime;
|
||||||
|
|
||||||
mod ui;
|
mod ui;
|
||||||
|
|
||||||
|
@ -83,35 +85,52 @@ impl<'a, A> Filter<StoreId> for IsInCollectionsFilter<'a, A>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
/// Marker enum for implementing ImagApplication on
|
||||||
let version = make_imag_version!();
|
///
|
||||||
let rt = generate_runtime_setup("imag-id-in-collection",
|
/// This is used by binaries crates to execute business logic
|
||||||
&version,
|
/// or to build a CLI completion.
|
||||||
"filter ids by collection",
|
pub enum ImagIdInCollection {}
|
||||||
crate::ui::build_ui);
|
impl ImagApplication for ImagIdInCollection {
|
||||||
let values = rt
|
fn run(rt: Runtime) -> Result<()> {
|
||||||
.cli()
|
let values = rt
|
||||||
.values_of("in-collection-filter")
|
.cli()
|
||||||
.map(|v| v.collect::<Vec<&str>>());
|
.values_of("in-collection-filter")
|
||||||
|
.map(|v| v.collect::<Vec<&str>>());
|
||||||
|
|
||||||
let collection_filter = IsInCollectionsFilter::new(values);
|
let collection_filter = IsInCollectionsFilter::new(values);
|
||||||
|
|
||||||
let mut stdout = rt.stdout();
|
let mut stdout = rt.stdout();
|
||||||
trace!("Got output: {:?}", stdout);
|
trace!("Got output: {:?}", stdout);
|
||||||
|
|
||||||
rt.ids::<crate::ui::PathProvider>()?
|
rt.ids::<crate::ui::PathProvider>()?
|
||||||
.ok_or_else(|| err_msg("No ids supplied"))?
|
.ok_or_else(|| err_msg("No ids supplied"))?
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|id| collection_filter.filter(id))
|
.filter(|id| collection_filter.filter(id))
|
||||||
.map(|id| {
|
.map(|id| {
|
||||||
if !rt.output_is_pipe() {
|
if !rt.output_is_pipe() {
|
||||||
let id = id.to_str()?;
|
let id = id.to_str()?;
|
||||||
trace!("Writing to {:?}", stdout);
|
trace!("Writing to {:?}", stdout);
|
||||||
writeln!(stdout, "{}", id)?;
|
writeln!(stdout, "{}", id)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
rt.report_touched(&id).map_err(Error::from)
|
rt.report_touched(&id).map_err(Error::from)
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
"print all ids"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn version() -> &'static str {
|
||||||
|
env!("CARGO_PKG_VERSION")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue