libimageditcmd/
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 failure;
40extern crate resiter;
41
42extern crate libimagentryedit;
43extern crate libimagerror;
44extern crate libimagrt;
45extern crate libimagstore;
46extern crate libimagutil;
47
48use libimagentryedit::edit::Edit;
49use libimagentryedit::edit::EditHeader;
50use libimagrt::runtime::Runtime;
51use libimagrt::application::ImagApplication;
52use libimagrt::iter::ReportTouchedResultEntry;
53use libimagstore::iter::get::StoreIdGetIteratorExtension;
54
55use failure::Fallible as Result;
56use failure::err_msg;
57use resiter::AndThen;
58use resiter::IterInnerOkOrElse;
59use clap::App;
60
61mod ui;
62
63/// Marker enum for implementing ImagApplication on
64///
65/// This is used by binaries crates to execute business logic
66/// or to build a CLI completion.
67pub enum ImagEdit {}
68impl ImagApplication for ImagEdit {
69    fn run(rt: Runtime) -> Result<()> {
70        let edit_header = rt.cli().is_present("edit-header");
71        let edit_header_only = rt.cli().is_present("edit-header-only");
72
73        rt.ids::<crate::ui::PathProvider>()?
74            .ok_or_else(|| err_msg("No ids supplied"))?
75            .into_iter()
76            .map(Ok)
77            .into_get_iter(rt.store())
78            .map_inner_ok_or_else(|| err_msg("Did not find one entry"))
79            .inspect(|e| debug!("Editing = {:?}", e))
80            .map_report_touched(&rt)
81            .and_then_ok(|mut entry| {
82                if edit_header {
83                    entry.edit_header_and_content(&rt)
84                } else if edit_header_only {
85                    entry.edit_header(&rt)
86                } else {
87                    entry.edit_content(&rt)
88                }
89            })
90            .collect()
91    }
92
93    fn build_cli<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
94        ui::build_ui(app)
95    }
96
97    fn name() -> &'static str {
98        env!("CARGO_PKG_NAME")
99    }
100
101    fn description() -> &'static str {
102        "Edit store entries with $EDITOR"
103    }
104
105    fn version() -> &'static str {
106        env!("CARGO_PKG_VERSION")
107    }
108}