libimagstorecmd/
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 resiter;
41#[cfg(test)] extern crate toml_query;
42#[macro_use] extern crate failure;
43
44extern crate libimagrt;
45extern crate libimagstore;
46extern crate libimagerror;
47
48#[cfg(test)]
49#[macro_use]
50extern crate libimagutil;
51
52#[cfg(not(test))]
53extern crate libimagutil;
54
55use libimagrt::application::ImagApplication;
56use libimagrt::runtime::Runtime;
57
58use failure::Fallible as Result;
59use failure::err_msg;
60
61mod create;
62mod delete;
63mod get;
64mod retrieve;
65mod ui;
66mod update;
67mod verify;
68mod util;
69
70use std::ops::Deref;
71
72use clap::App;
73
74use crate::create::create;
75use crate::delete::delete;
76use crate::get::get;
77use crate::retrieve::retrieve;
78use crate::update::update;
79use crate::verify::verify;
80
81/// Marker enum for implementing ImagApplication on
82///
83/// This is used by binaries crates to execute business logic
84/// or to build a CLI completion.
85pub enum ImagStore {}
86impl ImagApplication for ImagStore {
87    fn run(rt: Runtime) -> Result<()> {
88        if let Some(command) = rt.cli().subcommand_name() {
89            debug!("Call: {}", command);
90            match command.deref() {
91                "create"   => create(&rt),
92                "delete"   => delete(&rt),
93                "get"      => get(&rt),
94                "retrieve" => retrieve(&rt),
95                "update"   => update(&rt),
96                "verify"   => verify(&rt),
97                other      => {
98                    debug!("Unknown command");
99                    if rt.handle_unknown_subcommand("imag-store", other, rt.cli())?.success() {
100                        Ok(())
101                    } else {
102                        Err(format_err!("Subcommand failed"))
103                    }
104                },
105            }
106        } else {
107            Err(err_msg("No command"))
108        }
109    }
110
111    fn build_cli<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
112        ui::build_ui(app)
113    }
114
115    fn name() -> &'static str {
116        env!("CARGO_PKG_NAME")
117    }
118
119    fn description() -> &'static str {
120        "Direct interface to the store. Use with great care!"
121    }
122
123    fn version() -> &'static str {
124        env!("CARGO_PKG_VERSION")
125    }
126}