2016-04-16 20:50:42 +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-02-20 14:46:06 +00:00
|
|
|
extern crate clap;
|
2016-02-20 19:51:32 +00:00
|
|
|
extern crate glob;
|
|
|
|
#[macro_use] extern crate log;
|
|
|
|
extern crate semver;
|
|
|
|
extern crate toml;
|
|
|
|
#[macro_use] extern crate version;
|
|
|
|
|
|
|
|
extern crate libimagrt;
|
|
|
|
extern crate libimagstore;
|
|
|
|
extern crate libimagutil;
|
|
|
|
|
2016-03-11 15:16:04 +00:00
|
|
|
use std::result::Result as RResult;
|
2016-02-20 19:51:32 +00:00
|
|
|
use std::process::exit;
|
|
|
|
|
|
|
|
use libimagrt::runtime::Runtime;
|
2016-02-24 12:45:37 +00:00
|
|
|
use libimagstore::store::FileLockEntry;
|
2016-02-20 19:51:32 +00:00
|
|
|
use libimagutil::trace::trace_error;
|
2016-02-20 14:46:06 +00:00
|
|
|
|
2016-03-11 15:16:04 +00:00
|
|
|
mod error;
|
2016-02-20 14:46:06 +00:00
|
|
|
mod ui;
|
2016-02-20 19:51:32 +00:00
|
|
|
mod viewer;
|
|
|
|
|
2016-03-11 15:16:04 +00:00
|
|
|
use error::{ViewError, ViewErrorKind};
|
2016-02-20 19:51:32 +00:00
|
|
|
use ui::build_ui;
|
|
|
|
use viewer::Viewer;
|
|
|
|
use viewer::ViewInformation;
|
2016-02-23 23:51:35 +00:00
|
|
|
use viewer::stdout::StdoutViewer;
|
2016-02-20 14:46:06 +00:00
|
|
|
|
2016-03-11 15:16:04 +00:00
|
|
|
type Result<T> = RResult<T, ViewError>;
|
|
|
|
|
2016-02-18 16:19:56 +00:00
|
|
|
fn main() {
|
2016-02-20 19:51:32 +00:00
|
|
|
let name = "imag-view";
|
|
|
|
let version = &version!()[..];
|
|
|
|
let about = "View entries (readonly)";
|
|
|
|
let ui = build_ui(Runtime::get_default_cli_builder(name, version, about));
|
|
|
|
let rt = {
|
|
|
|
let rt = Runtime::new(ui);
|
|
|
|
if rt.is_ok() {
|
|
|
|
rt.unwrap()
|
|
|
|
} else {
|
|
|
|
println!("Could not set up Runtime");
|
2016-04-17 19:07:57 +00:00
|
|
|
println!("{:?}", rt.unwrap_err());
|
2016-03-11 15:16:35 +00:00
|
|
|
exit(1); // we can afford not-executing destructors here
|
2016-02-20 19:51:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let entry_id = rt.cli().value_of("id").unwrap(); // enforced by clap
|
|
|
|
|
|
|
|
if rt.cli().is_present("versions") {
|
2016-03-11 15:16:35 +00:00
|
|
|
if let Err(e) = view_versions_of(entry_id, &rt) {
|
|
|
|
trace_error(&e);
|
|
|
|
exit(1); // we can afford not-executing destructors here
|
|
|
|
}
|
2016-02-20 19:51:32 +00:00
|
|
|
} else {
|
|
|
|
let entry_version = rt.cli().value_of("version");
|
|
|
|
let view_header = rt.cli().is_present("view-header");
|
|
|
|
let view_content = rt.cli().is_present("view-content");
|
|
|
|
let view_copy = rt.cli().is_present("view-copy");
|
|
|
|
let keep_copy = rt.cli().is_present("keep-copy");
|
|
|
|
|
|
|
|
let scmd = rt.cli().subcommand_matches("view-in");
|
|
|
|
if scmd.is_none() {
|
|
|
|
debug!("No commandline call");
|
2016-03-11 15:16:35 +00:00
|
|
|
exit(1); // we can afford not-executing destructors here
|
2016-02-20 19:51:32 +00:00
|
|
|
}
|
|
|
|
let scmd = scmd.unwrap();
|
|
|
|
|
2016-03-06 11:32:21 +00:00
|
|
|
let viewer = {
|
|
|
|
if scmd.is_present("view-in-stdout") {
|
|
|
|
Box::new(StdoutViewer::new())
|
|
|
|
} else if scmd.is_present("view-in-ui") {
|
|
|
|
warn!("Viewing in UI is currently not supported, switch to stdout");
|
|
|
|
Box::new(StdoutViewer::new())
|
|
|
|
} else if scmd.is_present("view-in-browser") {
|
|
|
|
warn!("Viewing in browser is currently not supported, switch to stdout");
|
|
|
|
Box::new(StdoutViewer::new())
|
|
|
|
} else if scmd.is_present("view-in-texteditor") {
|
|
|
|
warn!("Viewing in texteditor is currently not supported, switch to stdout");
|
|
|
|
Box::new(StdoutViewer::new())
|
|
|
|
} else if scmd.is_present("view-in-custom") {
|
|
|
|
warn!("Viewing in custom is currently not supported, switch to stdout");
|
|
|
|
Box::new(StdoutViewer::new())
|
|
|
|
} else {
|
|
|
|
Box::new(StdoutViewer::new())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-24 12:45:37 +00:00
|
|
|
let entry = load_entry(entry_id, entry_version, &rt);
|
2016-02-20 19:51:32 +00:00
|
|
|
if entry.is_err() {
|
2016-04-17 19:07:57 +00:00
|
|
|
trace_error(&entry.unwrap_err());
|
2016-03-11 15:16:35 +00:00
|
|
|
exit(1); // we can afford not-executing destructors here
|
2016-02-20 19:51:32 +00:00
|
|
|
}
|
|
|
|
let entry = entry.unwrap();
|
|
|
|
|
|
|
|
let view_info = ViewInformation {
|
|
|
|
entry: entry,
|
|
|
|
view_header: view_header,
|
|
|
|
view_content: view_content,
|
|
|
|
view_copy: view_copy,
|
|
|
|
keep_copy: keep_copy,
|
|
|
|
};
|
|
|
|
|
2016-03-06 10:49:04 +00:00
|
|
|
viewer.view(view_info);
|
2016-02-20 19:51:32 +00:00
|
|
|
}
|
2016-02-18 16:19:56 +00:00
|
|
|
}
|
2016-02-20 19:51:32 +00:00
|
|
|
|
2016-02-24 12:45:37 +00:00
|
|
|
// TODO: This is a shameless adaption of imag-store/src/util.rs
|
|
|
|
fn load_entry<'a>(id: &str,
|
|
|
|
version: Option<&str>,
|
|
|
|
rt: &'a Runtime)
|
2016-03-11 15:16:35 +00:00
|
|
|
-> Result<FileLockEntry<'a>>
|
2016-02-24 12:45:37 +00:00
|
|
|
{
|
|
|
|
debug!("Checking path element for version");
|
|
|
|
|
|
|
|
let version = {
|
2016-03-11 15:16:35 +00:00
|
|
|
if version.is_none() {
|
2016-05-02 22:57:41 +00:00
|
|
|
let r = id.split('~').last();
|
2016-03-11 15:16:35 +00:00
|
|
|
if r.is_none() {
|
2016-02-24 12:45:37 +00:00
|
|
|
warn!("No version");
|
2016-03-11 15:16:35 +00:00
|
|
|
return Err(ViewError::new(ViewErrorKind::NoVersion, None));
|
|
|
|
} else {
|
|
|
|
r.unwrap()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
version.unwrap()
|
|
|
|
}
|
2016-02-24 12:45:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
debug!("Building path from {:?} and {:?}", id, version);
|
|
|
|
let mut path = rt.store().path().clone();
|
|
|
|
|
2016-05-02 22:57:41 +00:00
|
|
|
if id.starts_with('/') {
|
2016-02-24 12:45:37 +00:00
|
|
|
path.push(format!("{}~{}", &id[1..id.len()], version));
|
|
|
|
} else {
|
|
|
|
path.push(format!("{}~{}", id, version));
|
|
|
|
}
|
|
|
|
|
|
|
|
// the above is the adaption...
|
|
|
|
|
|
|
|
rt.store().retrieve(path)
|
2016-03-11 15:16:35 +00:00
|
|
|
.map_err(|e| ViewError::new(ViewErrorKind::StoreError, Some(Box::new(e))))
|
2016-02-20 19:51:32 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 15:16:35 +00:00
|
|
|
fn view_versions_of(id: &str, rt: &Runtime) -> Result<()> {
|
2016-02-24 13:10:31 +00:00
|
|
|
use glob::glob;
|
|
|
|
|
|
|
|
let mut path = rt.store().path().clone();
|
|
|
|
|
2016-05-02 22:57:41 +00:00
|
|
|
if id.starts_with('/') {
|
2016-02-24 13:10:31 +00:00
|
|
|
path.push(format!("{}~*", &id[1..id.len()]));
|
|
|
|
} else {
|
|
|
|
path.push(format!("{}~*", id));
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(path) = path.to_str() {
|
|
|
|
match glob(path) {
|
2016-03-11 15:16:35 +00:00
|
|
|
Ok(paths) => {
|
2016-02-24 13:10:31 +00:00
|
|
|
for entry in paths {
|
|
|
|
match entry {
|
|
|
|
Ok(path) => println!("{}", path.file_name().and_then(|s| s.to_str()).unwrap()),
|
|
|
|
Err(e) => trace_error(e.error()),
|
|
|
|
}
|
2016-03-11 15:16:35 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
},
|
2016-02-24 13:10:31 +00:00
|
|
|
Err(e) => {
|
|
|
|
debug!("Error in pattern");
|
2016-03-11 15:16:35 +00:00
|
|
|
Err(ViewError::new(ViewErrorKind::PatternError, Some(Box::new(e))))
|
2016-02-24 13:10:31 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
warn!("Could not build glob() argument!");
|
2016-03-11 15:16:35 +00:00
|
|
|
Err(ViewError::new(ViewErrorKind::GlobBuildError, None))
|
2016-02-24 13:10:31 +00:00
|
|
|
}
|
2016-02-20 19:51:32 +00:00
|
|
|
}
|
|
|
|
|