From b8b49b32408ba34db2102ebd2d2462879b04dd08 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 13 Mar 2016 20:49:26 +0100 Subject: [PATCH 1/4] Move: build_entry_path() from imag-store to libimagstore --- imag-store/src/create.rs | 2 +- imag-store/src/delete.rs | 3 +-- imag-store/src/retrieve.rs | 3 +-- imag-store/src/update.rs | 2 +- imag-store/src/util.rs | 30 ------------------------------ libimagstore/src/storeid.rs | 29 +++++++++++++++++++++++++++++ 6 files changed, 33 insertions(+), 36 deletions(-) diff --git a/imag-store/src/create.rs b/imag-store/src/create.rs index 03164581..5306ad62 100644 --- a/imag-store/src/create.rs +++ b/imag-store/src/create.rs @@ -13,10 +13,10 @@ use clap::ArgMatches; use libimagrt::runtime::Runtime; use libimagstore::store::Entry; use libimagstore::store::EntryHeader; +use libimagstore::storeid::build_entry_path; use error::StoreError; use error::StoreErrorKind; -use util::build_entry_path; use util::build_toml_header; type Result = RResult; diff --git a/imag-store/src/delete.rs b/imag-store/src/delete.rs index d645f743..182dad49 100644 --- a/imag-store/src/delete.rs +++ b/imag-store/src/delete.rs @@ -1,9 +1,8 @@ use std::path::PathBuf; +use libimagstore::storeid::build_entry_path; use libimagrt::runtime::Runtime; -use util::build_entry_path; - pub fn delete(rt: &Runtime) { use std::process::exit; diff --git a/imag-store/src/retrieve.rs b/imag-store/src/retrieve.rs index 2c0d19c5..6a00dfc2 100644 --- a/imag-store/src/retrieve.rs +++ b/imag-store/src/retrieve.rs @@ -6,10 +6,9 @@ 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; - pub fn retrieve(rt: &Runtime) { rt.cli() .subcommand_matches("retrieve") diff --git a/imag-store/src/update.rs b/imag-store/src/update.rs index 25f1cd92..2a017c94 100644 --- a/imag-store/src/update.rs +++ b/imag-store/src/update.rs @@ -2,8 +2,8 @@ use std::path::PathBuf; use std::ops::DerefMut; use libimagrt::runtime::Runtime; -use util::build_toml_header; use util::build_entry_path; +use util::build_toml_header; pub fn update(rt: &Runtime) { rt.cli() 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/libimagstore/src/storeid.rs b/libimagstore/src/storeid.rs index 92eaec49..9b6420e7 100644 --- a/libimagstore/src/storeid.rs +++ b/libimagstore/src/storeid.rs @@ -16,6 +16,35 @@ impl IntoStoreId for PathBuf { } } +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 +} #[macro_export] macro_rules! module_entry_path_mod { From 80945fcd16f053b90e387fc4fd74d978216486e6 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 13 Mar 2016 20:56:23 +0100 Subject: [PATCH 2/4] Rewrite build_entry_path() so it does not panic!() anymore --- libimagstore/src/error.rs | 2 ++ libimagstore/src/storeid.rs | 28 ++++++++++++---------------- 2 files changed, 14 insertions(+), 16 deletions(-) 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 9b6420e7..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,26 +21,17 @@ impl IntoStoreId for PathBuf { } } -pub fn build_entry_path(rt: &Runtime, path_elem: &str) -> PathBuf { +pub fn build_entry_path(store: &Store, path_elem: &str) -> Result { 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"); - } + 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 = rt.store().path().clone(); + let mut path = store.path().clone(); if path_elem.chars().next() == Some('/') { path.push(&path_elem[1..path_elem.len()]); @@ -43,7 +39,7 @@ pub fn build_entry_path(rt: &Runtime, path_elem: &str) -> PathBuf { path.push(path_elem); } - path + Ok(path) } #[macro_export] From df740ac63a2eae960321195bf405e37a0c0ccda9 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 13 Mar 2016 21:04:06 +0100 Subject: [PATCH 3/4] Adapt to new function interface of build_entry_path() --- imag-store/src/create.rs | 8 +++++++- imag-store/src/delete.rs | 10 +++++++++- imag-store/src/retrieve.rs | 30 ++++++++++++++++++++---------- imag-store/src/update.rs | 37 +++++++++++++++++++++++++------------ 4 files changed, 61 insertions(+), 24 deletions(-) diff --git a/imag-store/src/create.rs b/imag-store/src/create.rs index 5306ad62..9d90f642 100644 --- a/imag-store/src/create.rs +++ b/imag-store/src/create.rs @@ -14,6 +14,7 @@ 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; @@ -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 182dad49..71bf0f91 100644 --- a/imag-store/src/delete.rs +++ b/imag-store/src/delete.rs @@ -2,6 +2,7 @@ use std::path::PathBuf; use libimagstore::storeid::build_entry_path; use libimagrt::runtime::Runtime; +use libimagutil::trace::trace_error; pub fn delete(rt: &Runtime) { use std::process::exit; @@ -11,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 6a00dfc2..c2c2764a 100644 --- a/imag-store/src/retrieve.rs +++ b/imag-store/src/retrieve.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; use std::ops::Deref; use std::fmt::Display; +use std::process::exit; use clap::ArgMatches; use toml::Value; @@ -8,22 +9,31 @@ use toml::Value; use libimagstore::store::FileLockEntry; use libimagstore::storeid::build_entry_path; use libimagrt::runtime::Runtime; +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 2a017c94..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 util::build_entry_path; +use libimagstore::storeid::build_entry_path; +use libimagutil::trace::trace_error; + use util::build_toml_header; 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"); + }) }) }); From affa850bc0c97dac1f0fccd9206e86f64f2e5eb4 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 18 Mar 2016 14:38:16 +0100 Subject: [PATCH 4/4] imag-tag: Fix use of build_entry_path() --- imag-tag/src/main.rs | 24 ++++++++++++++++++++---- imag-tag/src/util.rs | 36 ------------------------------------ 2 files changed, 20 insertions(+), 40 deletions(-) delete mode 100644 imag-tag/src/util.rs 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 -} -