Merge pull request #559 from matthiasbeyer/imag-view/refactor
Imag view/refactor
This commit is contained in:
commit
887ad1c7ed
12 changed files with 145 additions and 225 deletions
|
@ -5,7 +5,6 @@ authors = ["Matthias Beyer <mail@beyermatthias.de>"]
|
|||
|
||||
[dependencies]
|
||||
clap = "2.1.1"
|
||||
glob = "0.2.11"
|
||||
log = "0.3"
|
||||
semver = "0.2.1"
|
||||
toml = "0.1.25"
|
||||
|
@ -20,3 +19,6 @@ path = "../libimagrt"
|
|||
[dependencies.libimagerror]
|
||||
path = "../libimagerror"
|
||||
|
||||
[dependencies.libimagentryview]
|
||||
path = "../libimagentryview"
|
||||
|
||||
|
|
|
@ -1,12 +0,0 @@
|
|||
generate_error_module!(
|
||||
generate_error_types!(ViewError, ViewErrorKind,
|
||||
StoreError => "Store error",
|
||||
NoVersion => "No version specified",
|
||||
PatternError => "Error in Pattern",
|
||||
GlobBuildError => "Could not build glob() Argument"
|
||||
);
|
||||
);
|
||||
|
||||
pub use self::error::ViewError;
|
||||
pub use self::error::ViewErrorKind;
|
||||
|
|
@ -14,7 +14,6 @@
|
|||
)]
|
||||
|
||||
extern crate clap;
|
||||
extern crate glob;
|
||||
#[macro_use] extern crate log;
|
||||
extern crate semver;
|
||||
extern crate toml;
|
||||
|
@ -22,27 +21,21 @@ extern crate toml;
|
|||
|
||||
extern crate libimagrt;
|
||||
extern crate libimagstore;
|
||||
extern crate libimagentryview;
|
||||
#[macro_use] extern crate libimagerror;
|
||||
|
||||
use std::result::Result as RResult;
|
||||
use std::process::exit;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use libimagrt::runtime::Runtime;
|
||||
use libimagrt::setup::generate_runtime_setup;
|
||||
use libimagstore::store::FileLockEntry;
|
||||
use libimagerror::trace::{trace_error, trace_error_exit};
|
||||
use libimagerror::trace::trace_error;
|
||||
use libimagentryview::builtin::stdout::StdoutViewer;
|
||||
use libimagentryview::builtin::versions::VersionsViewer;
|
||||
use libimagentryview::viewer::Viewer;
|
||||
|
||||
mod error;
|
||||
mod ui;
|
||||
mod viewer;
|
||||
|
||||
use error::{ViewError, ViewErrorKind};
|
||||
use ui::build_ui;
|
||||
use viewer::Viewer;
|
||||
use viewer::ViewInformation;
|
||||
use viewer::stdout::StdoutViewer;
|
||||
|
||||
type Result<T> = RResult<T, ViewError>;
|
||||
|
||||
fn main() {
|
||||
let rt = generate_runtime_setup( "imag-view",
|
||||
|
@ -50,136 +43,46 @@ fn main() {
|
|||
"View entries (readonly)",
|
||||
build_ui);
|
||||
|
||||
let entry_id = rt.cli().value_of("id").unwrap(); // enforced by clap
|
||||
let entry_id = rt.cli().value_of("id").unwrap(); // enforced by clap
|
||||
let view_header = rt.cli().is_present("view-header");
|
||||
let view_content = rt.cli().is_present("view-content");
|
||||
|
||||
if entry_id.contains("~") {
|
||||
error!("The --id argument does not need the version part");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if rt.cli().is_present("versions") {
|
||||
if let Err(e) = view_versions_of(entry_id, &rt) {
|
||||
trace_error_exit(&e, 1);
|
||||
}
|
||||
} 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() {
|
||||
let scmd = match rt.cli().subcommand_matches("view-in") {
|
||||
None => {
|
||||
debug!("No commandline call");
|
||||
exit(1); // we can afford not-executing destructors here
|
||||
}
|
||||
let scmd = scmd.unwrap();
|
||||
Some(s) => s,
|
||||
};
|
||||
|
||||
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())
|
||||
}
|
||||
};
|
||||
|
||||
let entry = load_entry(entry_id, entry_version, &rt);
|
||||
if entry.is_err() {
|
||||
trace_error_exit(&entry.unwrap_err(), 1);
|
||||
}
|
||||
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,
|
||||
};
|
||||
|
||||
viewer.view(view_info);
|
||||
}
|
||||
}
|
||||
|
||||
// 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>>
|
||||
{
|
||||
debug!("Checking path element for version");
|
||||
|
||||
let version = {
|
||||
if version.is_none() {
|
||||
let r = id.split('~').last();
|
||||
if r.is_none() {
|
||||
warn!("No version");
|
||||
return Err(ViewError::new(ViewErrorKind::NoVersion, None));
|
||||
} else {
|
||||
r.unwrap()
|
||||
}
|
||||
} else {
|
||||
version.unwrap()
|
||||
let entry = match rt.store().retrieve(PathBuf::from(entry_id)) {
|
||||
Ok(fle) => fle,
|
||||
Err(e) => {
|
||||
trace_error(&e);
|
||||
exit(1); // we can afford not-executing destructors here
|
||||
}
|
||||
};
|
||||
|
||||
debug!("Building path from {:?} and {:?}", id, version);
|
||||
let mut path = rt.store().path().clone();
|
||||
|
||||
if id.starts_with('/') {
|
||||
path.push(format!("{}~{}", &id[1..id.len()], version));
|
||||
let res = if rt.cli().is_present("versions") {
|
||||
VersionsViewer::new(rt.store()).view_entry(&entry)
|
||||
} 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))))
|
||||
}
|
||||
|
||||
fn view_versions_of(id: &str, rt: &Runtime) -> Result<()> {
|
||||
use glob::glob;
|
||||
|
||||
let mut path = rt.store().path().clone();
|
||||
|
||||
if id.starts_with('/') {
|
||||
path.push(format!("{}~*", &id[1..id.len()]));
|
||||
} else {
|
||||
path.push(format!("{}~*", id));
|
||||
}
|
||||
|
||||
if let Some(path) = path.to_str() {
|
||||
match glob(path) {
|
||||
Ok(paths) => {
|
||||
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(())
|
||||
},
|
||||
Err(e) => {
|
||||
debug!("Error in pattern");
|
||||
Err(ViewError::new(ViewErrorKind::PatternError, Some(Box::new(e))))
|
||||
},
|
||||
if scmd.is_present("view-in-stdout") {
|
||||
} else if scmd.is_present("view-in-ui") {
|
||||
warn!("Viewing in UI is currently not supported, switch to stdout");
|
||||
} else if scmd.is_present("view-in-browser") {
|
||||
warn!("Viewing in browser is currently not supported, switch to stdout");
|
||||
} else if scmd.is_present("view-in-texteditor") {
|
||||
warn!("Viewing in texteditor is currently not supported, switch to stdout");
|
||||
} else if scmd.is_present("view-in-custom") {
|
||||
warn!("Viewing in custom is currently not supported, switch to stdout");
|
||||
}
|
||||
} else {
|
||||
warn!("Could not build glob() argument!");
|
||||
Err(ViewError::new(ViewErrorKind::GlobBuildError, None))
|
||||
|
||||
StdoutViewer::new(view_header, view_content).view_entry(&entry)
|
||||
};
|
||||
|
||||
if let Err(e) = res {
|
||||
trace_error(&e);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -10,14 +10,6 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
|
|||
.help("View this entry at this store path")
|
||||
.value_name("ID"))
|
||||
|
||||
.arg(Arg::with_name("version")
|
||||
.long("version")
|
||||
.short("V")
|
||||
.takes_value(true)
|
||||
.required(true)
|
||||
.help("View this version (youngest if not specified)")
|
||||
.value_name("VERSION"))
|
||||
|
||||
.arg(Arg::with_name("versions")
|
||||
.long("versions")
|
||||
.takes_value(false)
|
||||
|
@ -37,19 +29,6 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
|
|||
.required(false)
|
||||
.help("View content"))
|
||||
|
||||
.arg(Arg::with_name("view-copy")
|
||||
.long("copy")
|
||||
.takes_value(false)
|
||||
.required(false)
|
||||
.help("Copy before opening (copies to /tmp/) and removes the file after viewing."))
|
||||
|
||||
.arg(Arg::with_name("keep-copy")
|
||||
.long("keep-copy")
|
||||
.short("k")
|
||||
.takes_value(false)
|
||||
.required(false)
|
||||
.help("If --copy was passed, keep the copy after viewing."))
|
||||
|
||||
.subcommand(SubCommand::with_name("view-in")
|
||||
.about("View the entry in ...")
|
||||
.version("0.1")
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
pub mod stdout;
|
||||
|
||||
use libimagstore::store::FileLockEntry;
|
||||
|
||||
pub struct ViewInformation<'a> {
|
||||
pub entry: FileLockEntry<'a>,
|
||||
pub view_header: bool,
|
||||
pub view_content: bool,
|
||||
pub view_copy: bool,
|
||||
pub keep_copy: bool,
|
||||
}
|
||||
|
||||
pub trait Viewer {
|
||||
fn view(&self, vi: ViewInformation);
|
||||
}
|
||||
|
|
@ -1,40 +0,0 @@
|
|||
use std::io::{Stdout, stdout};
|
||||
|
||||
use toml::encode_str;
|
||||
|
||||
use viewer::{ViewInformation, Viewer};
|
||||
|
||||
pub struct StdoutViewer {
|
||||
out: Stdout,
|
||||
}
|
||||
|
||||
impl StdoutViewer {
|
||||
|
||||
pub fn new() -> StdoutViewer {
|
||||
StdoutViewer { out: stdout() }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl Viewer for StdoutViewer {
|
||||
|
||||
fn view(&self, vi: ViewInformation) {
|
||||
if vi.view_copy {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
if vi.view_header {
|
||||
debug!("Going to display header: {:?}", vi.entry.get_header().header());
|
||||
println!("{}", encode_str(vi.entry.get_header().header()));
|
||||
}
|
||||
|
||||
if vi.view_content {
|
||||
println!("{}", vi.entry.get_content());
|
||||
}
|
||||
|
||||
if vi.view_copy && !vi.keep_copy {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -4,6 +4,9 @@ version = "0.2.0"
|
|||
authors = ["Matthias Beyer <mail@beyermatthias.de>"]
|
||||
|
||||
[dependencies]
|
||||
log = "0.3"
|
||||
toml = "0.1.25"
|
||||
glob = "0.2.11"
|
||||
|
||||
[dependencies.libimagstore]
|
||||
path = "../libimagstore"
|
||||
|
|
|
@ -1 +1,3 @@
|
|||
pub mod plain;
|
||||
pub mod stdout;
|
||||
pub mod versions;
|
||||
|
|
38
libimagentryview/src/builtin/stdout.rs
Normal file
38
libimagentryview/src/builtin/stdout.rs
Normal file
|
@ -0,0 +1,38 @@
|
|||
use libimagstore::store::Entry;
|
||||
|
||||
use toml::encode_str;
|
||||
|
||||
use viewer::Viewer;
|
||||
use result::Result;
|
||||
|
||||
pub struct StdoutViewer {
|
||||
view_header: bool,
|
||||
view_content: bool,
|
||||
}
|
||||
|
||||
impl StdoutViewer {
|
||||
|
||||
pub fn new(view_header: bool, view_content: bool) -> StdoutViewer {
|
||||
StdoutViewer {
|
||||
view_header: view_header,
|
||||
view_content: view_content,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl Viewer for StdoutViewer {
|
||||
|
||||
fn view_entry(&self, e: &Entry) -> Result<()> {
|
||||
if self.view_header {
|
||||
println!("{}", encode_str(e.get_header().header()));
|
||||
}
|
||||
|
||||
if self.view_content {
|
||||
println!("{}", e.get_content());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
54
libimagentryview/src/builtin/versions.rs
Normal file
54
libimagentryview/src/builtin/versions.rs
Normal file
|
@ -0,0 +1,54 @@
|
|||
use libimagstore::store::Entry;
|
||||
use libimagstore::store::Store;
|
||||
use libimagerror::into::IntoError;
|
||||
|
||||
use viewer::Viewer;
|
||||
use result::Result;
|
||||
use error::ViewErrorKind as VEK;
|
||||
|
||||
pub struct VersionsViewer<'a> {
|
||||
store: &'a Store,
|
||||
}
|
||||
|
||||
impl<'a> VersionsViewer<'a> {
|
||||
|
||||
pub fn new(store: &'a Store) -> VersionsViewer<'a> {
|
||||
VersionsViewer {
|
||||
store: store,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl<'a> Viewer for VersionsViewer<'a> {
|
||||
|
||||
fn view_entry(&self, entr: &Entry) -> Result<()> {
|
||||
use glob::glob;
|
||||
|
||||
entr.get_location()
|
||||
.clone()
|
||||
.storified(self.store)
|
||||
.to_str()
|
||||
.and_then(|s| s.split("~").next())
|
||||
.map(|component| {
|
||||
glob(&format!("{}~*", component)[..])
|
||||
.map_err(|_| VEK::PatternError.into_error())
|
||||
.and_then(|paths| {
|
||||
for entry in paths {
|
||||
let p = match entry {
|
||||
Err(_) => return Err(VEK::GlobError.into_error()),
|
||||
Ok(p) => p,
|
||||
};
|
||||
let p = p.file_name()
|
||||
.and_then(|s| s.to_str())
|
||||
.unwrap(); // TODO
|
||||
println!("{}", p);
|
||||
};
|
||||
Ok(())
|
||||
})
|
||||
})
|
||||
.unwrap_or(Err(VEK::PatternBuildingError.into_error()))
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,9 +1,12 @@
|
|||
generate_error_module!(
|
||||
generate_error_types!(ViewError, ViewErrorKind,
|
||||
Unknown => "Unknown view error"
|
||||
Unknown => "Unknown view error",
|
||||
GlobError => "Error while glob()ing",
|
||||
PatternError => "Error in glob() pattern",
|
||||
PatternBuildingError => "Could not build glob() pattern"
|
||||
);
|
||||
);
|
||||
|
||||
pub use self::error::ViewError;
|
||||
pub use self::error::ViewErrorKind;
|
||||
|
||||
pub use self::error::MapErrInto;
|
||||
|
|
|
@ -14,6 +14,10 @@
|
|||
while_true,
|
||||
)]
|
||||
|
||||
#[macro_use] extern crate log;
|
||||
extern crate glob;
|
||||
extern crate toml;
|
||||
|
||||
extern crate libimagstore;
|
||||
#[macro_use] extern crate libimagerror;
|
||||
|
||||
|
|
Loading…
Reference in a new issue