Merge pull request #680 from matthiasbeyer/rewrite-storeid-type-imag-store-cleanup

imag-store cleanup
This commit is contained in:
Matthias Beyer 2016-09-02 08:25:21 +02:00 committed by GitHub
commit 901df1fb3a

View file

@ -13,6 +13,9 @@ 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::StoreId;
use libimagerror::trace::trace_error_exit;
use libimagutil::debug_result::*;
use error::StoreError; use error::StoreError;
use error::StoreErrorKind; use error::StoreErrorKind;
@ -34,10 +37,15 @@ pub fn create(rt: &Runtime) {
exit(1); exit(1);
} }
let path = PathBuf::from(path.unwrap()); let store_path = rt.store().path().clone();
let path = match StoreId::new(Some(store_path), PathBuf::from(path.unwrap())) {
Err(e) => trace_error_exit(&e, 1),
Ok(o) => o,
};
debug!("path = {:?}", path); debug!("path = {:?}", path);
if scmd.subcommand_matches("entry").is_some() { if scmd.subcommand_matches("entry").is_some() {
debug!("Creating entry from CLI specification");
create_from_cli_spec(rt, scmd, &path) create_from_cli_spec(rt, scmd, &path)
.or_else(|_| create_from_source(rt, scmd, &path)) .or_else(|_| create_from_source(rt, scmd, &path))
.or_else(|_| create_with_content_and_header(rt, .or_else(|_| create_with_content_and_header(rt,
@ -45,13 +53,17 @@ pub fn create(rt: &Runtime) {
String::new(), String::new(),
EntryHeader::new())) EntryHeader::new()))
} else { } else {
debug!("Creating entry");
create_with_content_and_header(rt, &path, String::new(), EntryHeader::new()) create_with_content_and_header(rt, &path, String::new(), EntryHeader::new())
} }
.unwrap_or_else(|e| debug!("Error building Entry: {:?}", e)) .unwrap_or_else(|e| {
error!("Error building Entry");
trace_error_exit(&e, 1);
})
}); });
} }
fn create_from_cli_spec(rt: &Runtime, matches: &ArgMatches, path: &PathBuf) -> Result<()> { fn create_from_cli_spec(rt: &Runtime, matches: &ArgMatches, path: &StoreId) -> Result<()> {
let content = matches.subcommand_matches("entry") let content = matches.subcommand_matches("entry")
.map_or_else(|| { .map_or_else(|| {
debug!("Didn't find entry subcommand, getting raw content"); debug!("Didn't find entry subcommand, getting raw content");
@ -75,7 +87,7 @@ fn create_from_cli_spec(rt: &Runtime, matches: &ArgMatches, path: &PathBuf) -> R
create_with_content_and_header(rt, path, content, header) create_with_content_and_header(rt, path, content, header)
} }
fn create_from_source(rt: &Runtime, matches: &ArgMatches, path: &PathBuf) -> Result<()> { fn create_from_source(rt: &Runtime, matches: &ArgMatches, path: &StoreId) -> Result<()> {
let content = matches let content = matches
.value_of("from-raw") .value_of("from-raw")
.ok_or(StoreError::new(StoreErrorKind::NoCommandlineCall, None)) .ok_or(StoreError::new(StoreErrorKind::NoCommandlineCall, None))
@ -88,9 +100,11 @@ fn create_from_source(rt: &Runtime, matches: &ArgMatches, path: &PathBuf) -> Res
debug!("Content with len = {}", content.len()); debug!("Content with len = {}", content.len());
Entry::from_str(path.clone(), &content[..]) Entry::from_str(path.clone(), &content[..])
.map_dbg_err(|e| format!("Error building entry: {:?}", e))
.and_then(|new_e| { .and_then(|new_e| {
let r = rt.store() let r = rt.store()
.create(path.clone()) .create(path.clone())
.map_dbg_err(|e| format!("Error in Store::create(): {:?}", e))
.map(|mut old_e| { .map(|mut old_e| {
*old_e.deref_mut() = new_e; *old_e.deref_mut() = new_e;
}); });
@ -98,17 +112,19 @@ fn create_from_source(rt: &Runtime, matches: &ArgMatches, path: &PathBuf) -> Res
debug!("Entry build"); debug!("Entry build");
r r
}) })
.map_dbg_err(|e| format!("Error storing entry: {:?}", e))
.map_err(|serr| StoreError::new(StoreErrorKind::BackendError, Some(Box::new(serr)))) .map_err(|serr| StoreError::new(StoreErrorKind::BackendError, Some(Box::new(serr))))
} }
fn create_with_content_and_header(rt: &Runtime, fn create_with_content_and_header(rt: &Runtime,
path: &PathBuf, path: &StoreId,
content: String, content: String,
header: EntryHeader) -> Result<()> header: EntryHeader) -> Result<()>
{ {
debug!("Creating entry with content at {:?}", path); debug!("Creating entry with content at {:?}", path);
rt.store() rt.store()
.create(PathBuf::from(path)) .create(path.clone())
.map_dbg_err(|e| format!("Error in Store::create(): {:?}", e))
.map(|mut element| { .map(|mut element| {
{ {
let mut e_content = element.get_content_mut(); let mut e_content = element.get_content_mut();