libimagidscmd/
lib.rs

1//
2// imag - the personal information management suite for the commandline
3// Copyright (C) 2015-2020 Matthias Beyer <mail@beyermatthias.de> and contributors
4//
5// This library is free software; you can redistribute it and/or
6// modify it under the terms of the GNU Lesser General Public
7// License as published by the Free Software Foundation; version
8// 2.1 of the License.
9//
10// This library is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13// Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public
16// License along with this library; if not, write to the Free Software
17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18//
19
20#![forbid(unsafe_code)]
21
22#![deny(
23    non_camel_case_types,
24    non_snake_case,
25    path_statements,
26    trivial_numeric_casts,
27    unstable_features,
28    unused_allocation,
29    unused_import_braces,
30    unused_imports,
31    unused_must_use,
32    unused_mut,
33    unused_qualifications,
34    while_true,
35)]
36
37extern crate clap;
38#[macro_use] extern crate log;
39extern crate toml;
40extern crate toml_query;
41#[macro_use] extern crate failure;
42extern crate resiter;
43
44#[cfg(test)]
45extern crate env_logger;
46
47extern crate libimagerror;
48extern crate libimagstore;
49extern crate libimagrt;
50
51use std::io::Write;
52
53use failure::Fallible as Result;
54use failure::err_msg;
55use resiter::Map;
56use resiter::AndThen;
57use clap::App;
58
59use libimagstore::storeid::StoreId;
60use libimagrt::runtime::Runtime;
61use libimagrt::application::ImagApplication;
62
63mod ui;
64
65/// Marker enum for implementing ImagApplication on
66///
67/// This is used by binaries crates to execute business logic
68/// or to build a CLI completion.
69pub enum ImagIds {}
70impl ImagApplication for ImagIds {
71    fn run(rt: Runtime) -> Result<()> {
72        let print_storepath = rt.cli().is_present("print-storepath");
73
74        let mut stdout = rt.stdout();
75        trace!("Got output: {:?}", stdout);
76
77        let mut process = |iter: &mut dyn Iterator<Item = Result<StoreId>>| -> Result<()> {
78            iter.map_ok(|id| if print_storepath {
79                (Some(rt.store().path()), id)
80            } else {
81                (None, id)
82            }).and_then_ok(|(storepath, id)| {
83                if !rt.output_is_pipe() {
84                    let id = id.to_str()?;
85                    trace!("Writing to {:?}", stdout);
86
87                    if let Some(store) = storepath {
88                        writeln!(stdout, "{}/{}", store.display(), id)?;
89                    } else {
90                        writeln!(stdout, "{}", id)?;
91                    }
92                }
93
94                rt.report_touched(&id)
95            })
96            .collect::<Result<()>>()
97        };
98
99        if rt.ids_from_stdin() {
100            debug!("Fetching IDs from stdin...");
101            let mut iter = rt.ids::<crate::ui::PathProvider>()?
102                .ok_or_else(|| err_msg("No ids supplied"))?
103                .into_iter()
104                .map(Ok);
105
106            process(&mut iter)
107        } else {
108            process(&mut rt.store().entries()?)
109        }
110    }
111
112    fn build_cli<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
113        ui::build_ui(app)
114    }
115
116    fn name() -> &'static str {
117        env!("CARGO_PKG_NAME")
118    }
119
120    fn description() -> &'static str {
121        "print all ids"
122    }
123
124    fn version() -> &'static str {
125        env!("CARGO_PKG_VERSION")
126    }
127}