diff --git a/imag-store/src/create.rs b/imag-store/src/create.rs index 03164581..9d90f642 100644 --- a/imag-store/src/create.rs +++ b/imag-store/src/create.rs @@ -13,10 +13,11 @@ use clap::ArgMatches; use libimagrt::runtime::Runtime; use libimagstore::store::Entry; use libimagstore::store::EntryHeader; +use libimagstore::storeid::build_entry_path; +use libimagutil::trace::trace_error; use error::StoreError; use error::StoreErrorKind; -use util::build_entry_path; use util::build_toml_header; type Result = RResult; @@ -35,7 +36,12 @@ pub fn create(rt: &Runtime) { 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); if scmd.subcommand_matches("entry").is_some() { diff --git a/imag-store/src/delete.rs b/imag-store/src/delete.rs index d645f743..71bf0f91 100644 --- a/imag-store/src/delete.rs +++ b/imag-store/src/delete.rs @@ -1,8 +1,8 @@ use std::path::PathBuf; +use libimagstore::storeid::build_entry_path; use libimagrt::runtime::Runtime; - -use util::build_entry_path; +use libimagutil::trace::trace_error; pub fn delete(rt: &Runtime) { use std::process::exit; @@ -12,9 +12,16 @@ pub fn delete(rt: &Runtime) { .map(|sub| { sub.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!("Deleting file at {:?}", id); + rt.store() - .delete(build_entry_path(rt, id)) + .delete(path) .map_err(|e| { warn!("Error: {:?}", e); exit(1); diff --git a/imag-store/src/retrieve.rs b/imag-store/src/retrieve.rs index 2c0d19c5..c2c2764a 100644 --- a/imag-store/src/retrieve.rs +++ b/imag-store/src/retrieve.rs @@ -1,30 +1,39 @@ use std::path::PathBuf; use std::ops::Deref; use std::fmt::Display; +use std::process::exit; use clap::ArgMatches; use toml::Value; use libimagstore::store::FileLockEntry; +use libimagstore::storeid::build_entry_path; use libimagrt::runtime::Runtime; - -use util::build_entry_path; +use libimagutil::trace::trace_error; pub fn retrieve(rt: &Runtime) { rt.cli() .subcommand_matches("retrieve") .map(|scmd| { - let path = scmd.value_of("id").map(|id| build_entry_path(rt, id)).unwrap(); - debug!("path = {:?}", path); - rt.store() - // "id" must be present, enforced via clap spec - .retrieve(path) - .map(|e| print_entry(rt, scmd, e)) - .map_err(|e| { - debug!("No entry."); - debug!("{}", e); - }) + 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); + rt.store() + // "id" must be present, enforced via clap spec + .retrieve(path) + .map(|e| print_entry(rt, scmd, e)) + .map_err(|e| { + debug!("No entry."); + debug!("{}", e); + }) + }) }); } diff --git a/imag-store/src/update.rs b/imag-store/src/update.rs index 25f1cd92..d37da1c2 100644 --- a/imag-store/src/update.rs +++ b/imag-store/src/update.rs @@ -1,27 +1,40 @@ use std::path::PathBuf; use std::ops::DerefMut; +use std::process::exit; use libimagrt::runtime::Runtime; +use libimagstore::storeid::build_entry_path; +use libimagutil::trace::trace_error; + use util::build_toml_header; -use util::build_entry_path; pub fn update(rt: &Runtime) { rt.cli() .subcommand_matches("update") .map(|scmd| { - rt.store() - .retrieve(scmd.value_of("id").map(|id| build_entry_path(rt, id)).unwrap()) - .map(|mut locked_e| { - let mut e = locked_e.deref_mut(); + 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(); - scmd.value_of("content") - .map(|new_content| { - *e.get_content_mut() = String::from(new_content); - debug!("New content set"); - }); + rt.store() + .retrieve(path) + .map(|mut locked_e| { + let mut e = locked_e.deref_mut(); - *e.get_header_mut() = build_toml_header(scmd, e.get_header().clone()); - debug!("New header set"); + scmd.value_of("content") + .map(|new_content| { + *e.get_content_mut() = String::from(new_content); + debug!("New content set"); + }); + + *e.get_header_mut() = build_toml_header(scmd, e.get_header().clone()); + debug!("New header set"); + }) }) }); diff --git a/imag-store/src/util.rs b/imag-store/src/util.rs index 9fb01720..61832e3a 100644 --- a/imag-store/src/util.rs +++ b/imag-store/src/util.rs @@ -10,36 +10,6 @@ use libimagstore::store::EntryHeader; use libimagrt::runtime::Runtime; 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 { debug!("Building header from cli spec"); if let Some(headerspecs) = matches.values_of("header") { diff --git a/imag-tag/src/main.rs b/imag-tag/src/main.rs index 87b9919d..52c39168 100644 --- a/imag-tag/src/main.rs +++ b/imag-tag/src/main.rs @@ -13,12 +13,11 @@ use std::process::exit; use libimagrt::runtime::Runtime; use libimagtag::tagable::Tagable; +use libimagstore::storeid::build_entry_path; mod ui; -mod util; use ui::build_ui; -use util::build_entry_path; 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>) { - 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); + rt.store() // "id" must be present, enforced via clap spec .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) { - 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); let entry = rt.store().retrieve(path.clone()); diff --git a/imag-tag/src/util.rs b/imag-tag/src/util.rs deleted file mode 100644 index b33e045d..00000000 --- a/imag-tag/src/util.rs +++ /dev/null @@ -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 -} - diff --git a/libimagstore/src/error.rs b/libimagstore/src/error.rs index 0e3fc32f..cdb21a41 100644 --- a/libimagstore/src/error.rs +++ b/libimagstore/src/error.rs @@ -32,6 +32,7 @@ pub enum StoreErrorKind { HookExecutionError, PreHookExecuteError, PostHookExecuteError, + StorePathLacksVersion, // maybe more } @@ -60,6 +61,7 @@ fn store_error_type_as_str(e: &StoreErrorKind) -> &'static str { &StoreErrorKind::HookExecutionError => "Hook execution error", &StoreErrorKind::PreHookExecuteError => "Pre-Hook execution error", &StoreErrorKind::PostHookExecuteError => "Post-Hook execution error", + &StoreErrorKind::StorePathLacksVersion => "The supplied store path has no version part", } } diff --git a/libimagstore/src/storeid.rs b/libimagstore/src/storeid.rs index 92eaec49..1a608565 100644 --- a/libimagstore/src/storeid.rs +++ b/libimagstore/src/storeid.rs @@ -1,5 +1,10 @@ use std::path::PathBuf; use glob::Paths; +use semver::Version; + +use error::{StoreError, StoreErrorKind}; +use store::Result; +use store::Store; /// The Index into the Store pub type StoreId = PathBuf; @@ -16,6 +21,26 @@ impl IntoStoreId for PathBuf { } } +pub fn build_entry_path(store: &Store, path_elem: &str) -> Result { + 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_rules! module_entry_path_mod {