Merge pull request #249 from matthiasbeyer/libimagstore/store-id-build-helper

Libimagstore/store id build helper
This commit is contained in:
Matthias Beyer 2016-03-19 15:14:26 +01:00
commit 98df49b6eb
9 changed files with 111 additions and 99 deletions

View file

@ -13,10 +13,11 @@ use clap::ArgMatches;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagstore::store::Entry; use libimagstore::store::Entry;
use libimagstore::store::EntryHeader; use libimagstore::store::EntryHeader;
use libimagstore::storeid::build_entry_path;
use libimagutil::trace::trace_error;
use error::StoreError; use error::StoreError;
use error::StoreErrorKind; use error::StoreErrorKind;
use util::build_entry_path;
use util::build_toml_header; use util::build_toml_header;
type Result<T> = RResult<T, StoreError>; type Result<T> = RResult<T, StoreError>;
@ -35,7 +36,12 @@ pub fn create(rt: &Runtime) {
exit(1); exit(1);
} }
let path = build_entry_path(rt, path.unwrap()); let path = build_entry_path(rt.store(), path.unwrap());
if path.is_err() {
trace_error(&path.err().unwrap());
exit(1);
}
let path = path.unwrap();
debug!("path = {:?}", path); debug!("path = {:?}", path);
if scmd.subcommand_matches("entry").is_some() { if scmd.subcommand_matches("entry").is_some() {

View file

@ -1,8 +1,8 @@
use std::path::PathBuf; use std::path::PathBuf;
use libimagstore::storeid::build_entry_path;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagutil::trace::trace_error;
use util::build_entry_path;
pub fn delete(rt: &Runtime) { pub fn delete(rt: &Runtime) {
use std::process::exit; use std::process::exit;
@ -12,9 +12,16 @@ pub fn delete(rt: &Runtime) {
.map(|sub| { .map(|sub| {
sub.value_of("id") sub.value_of("id")
.map(|id| { .map(|id| {
let path = build_entry_path(rt.store(), id);
if path.is_err() {
trace_error(&path.err().unwrap());
exit(1);
}
let path = path.unwrap();
debug!("Deleting file at {:?}", id); debug!("Deleting file at {:?}", id);
rt.store() rt.store()
.delete(build_entry_path(rt, id)) .delete(path)
.map_err(|e| { .map_err(|e| {
warn!("Error: {:?}", e); warn!("Error: {:?}", e);
exit(1); exit(1);

View file

@ -1,21 +1,30 @@
use std::path::PathBuf; use std::path::PathBuf;
use std::ops::Deref; use std::ops::Deref;
use std::fmt::Display; use std::fmt::Display;
use std::process::exit;
use clap::ArgMatches; use clap::ArgMatches;
use toml::Value; use toml::Value;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagstore::storeid::build_entry_path;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagutil::trace::trace_error;
use util::build_entry_path;
pub fn retrieve(rt: &Runtime) { pub fn retrieve(rt: &Runtime) {
rt.cli() rt.cli()
.subcommand_matches("retrieve") .subcommand_matches("retrieve")
.map(|scmd| { .map(|scmd| {
let path = scmd.value_of("id").map(|id| build_entry_path(rt, id)).unwrap(); scmd.value_of("id")
.map(|id| {
let path = build_entry_path(rt.store(), id);
if path.is_err() {
trace_error(&path.err().unwrap());
exit(1);
}
let path = path.unwrap();
debug!("path = {:?}", path); debug!("path = {:?}", path);
rt.store() rt.store()
// "id" must be present, enforced via clap spec // "id" must be present, enforced via clap spec
.retrieve(path) .retrieve(path)
@ -24,7 +33,7 @@ pub fn retrieve(rt: &Runtime) {
debug!("No entry."); debug!("No entry.");
debug!("{}", e); debug!("{}", e);
}) })
})
}); });
} }

View file

@ -1,16 +1,28 @@
use std::path::PathBuf; use std::path::PathBuf;
use std::ops::DerefMut; use std::ops::DerefMut;
use std::process::exit;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagstore::storeid::build_entry_path;
use libimagutil::trace::trace_error;
use util::build_toml_header; use util::build_toml_header;
use util::build_entry_path;
pub fn update(rt: &Runtime) { pub fn update(rt: &Runtime) {
rt.cli() rt.cli()
.subcommand_matches("update") .subcommand_matches("update")
.map(|scmd| { .map(|scmd| {
scmd.value_of("id")
.map(|id| {
let path = build_entry_path(rt.store(), id);
if path.is_err() {
trace_error(&path.err().unwrap());
exit(1);
}
let path = path.unwrap();
rt.store() rt.store()
.retrieve(scmd.value_of("id").map(|id| build_entry_path(rt, id)).unwrap()) .retrieve(path)
.map(|mut locked_e| { .map(|mut locked_e| {
let mut e = locked_e.deref_mut(); let mut e = locked_e.deref_mut();
@ -23,6 +35,7 @@ pub fn update(rt: &Runtime) {
*e.get_header_mut() = build_toml_header(scmd, e.get_header().clone()); *e.get_header_mut() = build_toml_header(scmd, e.get_header().clone());
debug!("New header set"); debug!("New header set");
}) })
})
}); });
} }

View file

@ -10,36 +10,6 @@ use libimagstore::store::EntryHeader;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagutil::key_value_split::IntoKeyValue; use libimagutil::key_value_split::IntoKeyValue;
pub fn build_entry_path(rt: &Runtime, path_elem: &str) -> PathBuf {
debug!("Checking path element for version");
{
let contains_version = {
path_elem.split("~")
.last()
.map(|version| Version::parse(version).is_ok())
.unwrap_or(false)
};
if !contains_version {
debug!("Version cannot be parsed inside {:?}", path_elem);
warn!("Path does not contain version. Will panic now!");
panic!("No version in path");
}
}
debug!("Version checking succeeded");
debug!("Building path from {:?}", path_elem);
let mut path = rt.store().path().clone();
if path_elem.chars().next() == Some('/') {
path.push(&path_elem[1..path_elem.len()]);
} else {
path.push(path_elem);
}
path
}
pub fn build_toml_header(matches: &ArgMatches, mut header: EntryHeader) -> EntryHeader { pub fn build_toml_header(matches: &ArgMatches, mut header: EntryHeader) -> EntryHeader {
debug!("Building header from cli spec"); debug!("Building header from cli spec");
if let Some(headerspecs) = matches.values_of("header") { if let Some(headerspecs) = matches.values_of("header") {

View file

@ -13,12 +13,11 @@ use std::process::exit;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagtag::tagable::Tagable; use libimagtag::tagable::Tagable;
use libimagstore::storeid::build_entry_path;
mod ui; mod ui;
mod util;
use ui::build_ui; use ui::build_ui;
use util::build_entry_path;
use libimagutil::trace::trace_error; use libimagutil::trace::trace_error;
@ -68,8 +67,17 @@ fn main() {
} }
fn alter(rt: &Runtime, id: &str, add: Option<&str>, rem: Option<&str>, set: Option<&str>) { fn alter(rt: &Runtime, id: &str, add: Option<&str>, rem: Option<&str>, set: Option<&str>) {
let path = build_entry_path(rt, id); let path = {
match build_entry_path(rt.store(), id) {
Err(e) => {
trace_error(&e);
exit(1);
},
Ok(s) => s,
}
};
debug!("path = {:?}", path); debug!("path = {:?}", path);
rt.store() rt.store()
// "id" must be present, enforced via clap spec // "id" must be present, enforced via clap spec
.retrieve(path) .retrieve(path)
@ -110,7 +118,15 @@ fn alter(rt: &Runtime, id: &str, add: Option<&str>, rem: Option<&str>, set: Opti
} }
fn list(id: &str, rt: &Runtime) { fn list(id: &str, rt: &Runtime) {
let path = build_entry_path(rt, id); let path = {
match build_entry_path(rt.store(), id) {
Err(e) => {
trace_error(&e);
exit(1);
},
Ok(s) => s,
}
};
debug!("path = {:?}", path); debug!("path = {:?}", path);
let entry = rt.store().retrieve(path.clone()); let entry = rt.store().retrieve(path.clone());

View file

@ -1,36 +0,0 @@
use std::path::PathBuf;
use semver::Version;
use libimagrt::runtime::Runtime;
pub fn build_entry_path(rt: &Runtime, path_elem: &str) -> PathBuf {
debug!("Checking path element for version");
{
let contains_version = {
path_elem.split("~")
.last()
.map(|version| Version::parse(version).is_ok())
.unwrap_or(false)
};
if !contains_version {
debug!("Version cannot be parsed inside {:?}", path_elem);
warn!("Path does not contain version. Will panic now!");
panic!("No version in path");
}
}
debug!("Version checking succeeded");
debug!("Building path from {:?}", path_elem);
let mut path = rt.store().path().clone();
if path_elem.chars().next() == Some('/') {
path.push(&path_elem[1..path_elem.len()]);
} else {
path.push(path_elem);
}
path
}

View file

@ -32,6 +32,7 @@ pub enum StoreErrorKind {
HookExecutionError, HookExecutionError,
PreHookExecuteError, PreHookExecuteError,
PostHookExecuteError, PostHookExecuteError,
StorePathLacksVersion,
// maybe more // maybe more
} }
@ -60,6 +61,7 @@ fn store_error_type_as_str(e: &StoreErrorKind) -> &'static str {
&StoreErrorKind::HookExecutionError => "Hook execution error", &StoreErrorKind::HookExecutionError => "Hook execution error",
&StoreErrorKind::PreHookExecuteError => "Pre-Hook execution error", &StoreErrorKind::PreHookExecuteError => "Pre-Hook execution error",
&StoreErrorKind::PostHookExecuteError => "Post-Hook execution error", &StoreErrorKind::PostHookExecuteError => "Post-Hook execution error",
&StoreErrorKind::StorePathLacksVersion => "The supplied store path has no version part",
} }
} }

View file

@ -1,5 +1,10 @@
use std::path::PathBuf; use std::path::PathBuf;
use glob::Paths; use glob::Paths;
use semver::Version;
use error::{StoreError, StoreErrorKind};
use store::Result;
use store::Store;
/// The Index into the Store /// The Index into the Store
pub type StoreId = PathBuf; pub type StoreId = PathBuf;
@ -16,6 +21,26 @@ impl IntoStoreId for PathBuf {
} }
} }
pub fn build_entry_path(store: &Store, path_elem: &str) -> Result<PathBuf> {
debug!("Checking path element for version");
if path_elem.split("~").last().map(|v| Version::parse(v).is_err()).unwrap_or(false) {
debug!("Version cannot be parsed from {:?}", path_elem);
debug!("Path does not contain version!");
return Err(StoreError::new(StoreErrorKind::StorePathLacksVersion, None));
}
debug!("Version checking succeeded");
debug!("Building path from {:?}", path_elem);
let mut path = store.path().clone();
if path_elem.chars().next() == Some('/') {
path.push(&path_elem[1..path_elem.len()]);
} else {
path.push(path_elem);
}
Ok(path)
}
#[macro_export] #[macro_export]
macro_rules! module_entry_path_mod { macro_rules! module_entry_path_mod {