imag/imag-view/src/main.rs

183 lines
5.5 KiB
Rust
Raw Normal View History

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;
#[macro_use] extern crate libimagerror;
2016-02-20 19:51:32 +00:00
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;
use libimagrt::setup::generate_runtime_setup;
2016-02-24 12:45:37 +00:00
use libimagstore::store::FileLockEntry;
use libimagerror::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() {
let rt = generate_runtime_setup( "imag-view",
&version!()[..],
"View entries (readonly)",
build_ui);
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") {
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");
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() {
trace_error(&entry.unwrap_err());
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)
-> Result<FileLockEntry<'a>>
2016-02-24 12:45:37 +00:00
{
debug!("Checking path element for version");
let version = {
if version.is_none() {
let r = id.split('~').last();
if r.is_none() {
2016-02-24 12:45:37 +00:00
warn!("No version");
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();
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)
.map_err(|e| ViewError::new(ViewErrorKind::StoreError, Some(Box::new(e))))
2016-02-20 19:51:32 +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();
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) {
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()),
}
}
Ok(())
},
2016-02-24 13:10:31 +00:00
Err(e) => {
debug!("Error in pattern");
Err(ViewError::new(ViewErrorKind::PatternError, Some(Box::new(e))))
2016-02-24 13:10:31 +00:00
},
}
} else {
warn!("Could not build glob() argument!");
Err(ViewError::new(ViewErrorKind::GlobBuildError, None))
2016-02-24 13:10:31 +00:00
}
2016-02-20 19:51:32 +00:00
}