imag/bin/core/imag-ref/src/main.rs

135 lines
4 KiB
Rust
Raw Normal View History

//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2018 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
//
2016-10-07 22:51:47 +00:00
#![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,
)]
2016-06-26 09:29:47 +00:00
#[macro_use] extern crate log;
extern crate clap;
2018-02-14 11:46:13 +00:00
extern crate libimagstore;
#[macro_use] extern crate libimagrt;
extern crate libimagentryref;
2016-06-26 09:29:47 +00:00
extern crate libimagerror;
2016-06-28 21:10:49 +00:00
extern crate libimaginteraction;
2016-10-07 15:35:43 +00:00
extern crate libimagutil;
2016-06-26 09:29:47 +00:00
2016-06-26 09:30:04 +00:00
mod ui;
use ui::build_ui;
2016-06-28 20:45:37 +00:00
use std::path::PathBuf;
2017-10-14 11:46:17 +00:00
use std::process::exit;
2016-06-28 20:45:37 +00:00
2017-10-14 11:46:17 +00:00
use libimagerror::trace::MapErrTrace;
2016-06-28 20:45:37 +00:00
use libimagrt::setup::generate_runtime_setup;
use libimagrt::runtime::Runtime;
2018-02-14 11:46:13 +00:00
use libimagstore::storeid::IntoStoreId;
use libimagentryref::reference::Ref;
2016-06-28 20:45:37 +00:00
2016-06-26 09:17:30 +00:00
fn main() {
let version = make_imag_version!();
2016-06-26 09:31:46 +00:00
let rt = generate_runtime_setup("imag-ref",
&version,
2016-06-26 09:31:46 +00:00
"Reference files outside of the store",
build_ui);
rt.cli()
.subcommand_name()
.map(|name| {
debug!("Call: {}", name);
match name {
2018-02-14 11:46:13 +00:00
"deref" => deref(&rt),
2016-06-26 09:31:46 +00:00
"remove" => remove(&rt),
other => {
debug!("Unknown command");
let _ = rt.handle_unknown_subcommand("imag-ref", other, rt.cli())
.map_err_trace_exit_unwrap(1)
.code()
.map(::std::process::exit);
2016-06-26 09:31:46 +00:00
},
};
});
2016-06-26 09:17:30 +00:00
}
2016-06-26 09:31:46 +00:00
2018-02-14 11:46:13 +00:00
fn deref(rt: &Runtime) {
let cmd = rt.cli().subcommand_matches("deref").unwrap();
let id = cmd.value_of("ID")
.map(String::from)
.map(PathBuf::from)
.unwrap() // saved by clap
.into_storeid()
.map_err_trace_exit_unwrap(1);
match rt.store().get(id.clone()).map_err_trace_exit_unwrap(1) {
Some(entry) => entry
.get_path()
.map_err_trace_exit_unwrap(1)
.to_str()
.ok_or_else(|| {
error!("Could not transform path into string!");
exit(1)
})
.map(|s| info!("{}", s))
.ok(), // safe here because we exited already in the error case
None => {
error!("No entry for id '{}' found", id);
exit(1)
2016-06-26 09:39:27 +00:00
},
2018-02-14 11:46:13 +00:00
};
2016-06-26 09:31:46 +00:00
}
fn remove(rt: &Runtime) {
2016-06-28 21:10:49 +00:00
use libimaginteraction::ask::ask_bool;
let cmd = rt.cli().subcommand_matches("remove").unwrap();
let yes = cmd.is_present("yes");
2018-02-14 11:46:13 +00:00
let id = cmd.value_of("ID")
.map(String::from)
.map(PathBuf::from)
.unwrap() // saved by clap
.into_storeid()
.map_err_trace_exit_unwrap(1);
match rt.store().get(id.clone()).map_err_trace_exit_unwrap(1) {
Some(mut entry) => {
if yes || ask_bool(&format!("Delete ref from entry '{}'", id), None) {
let _ = entry.remove_ref().map_err_trace_exit_unwrap(1);
2017-10-14 11:46:17 +00:00
} else {
info!("Aborted");
}
},
None => {
2018-02-14 11:46:13 +00:00
error!("No entry for id '{}' found", id);
2017-10-14 11:46:17 +00:00
exit(1)
},
};
2016-06-26 09:31:46 +00:00
}