Merge pull request #1176 from matthiasbeyer/libimagstore/remove-config

libimagstore: remove config
This commit is contained in:
Matthias Beyer 2017-12-25 17:58:16 +01:00 committed by GitHub
commit 986dbad2fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 44 additions and 92 deletions

View file

@ -24,7 +24,6 @@ use std::process::exit;
pub use clap::App; pub use clap::App;
use toml::Value; use toml::Value;
use toml_query::read::TomlValueReadExt;
use clap::{Arg, ArgMatches}; use clap::{Arg, ArgMatches};
use log; use log;
@ -138,22 +137,12 @@ impl<'a> Runtime<'a> {
debug!("RTP path = {:?}", rtp); debug!("RTP path = {:?}", rtp);
debug!("Store path = {:?}", storepath); debug!("Store path = {:?}", storepath);
let store_config = match config {
Some(ref c) => c.read("store").chain_err(|| RuntimeErrorKind::Instantiate)?.cloned(),
None => None,
};
if matches.is_present(Runtime::arg_debugging_name()) {
debug!("Config: {:?}\n", config);
debug!("Store-config: {:?}\n", store_config);
}
let store_result = if cli_app.use_inmemory_fs() { let store_result = if cli_app.use_inmemory_fs() {
Store::new_with_backend(storepath, Store::new_with_backend(storepath,
store_config, &config,
Box::new(InMemoryFileAbstraction::new())) Box::new(InMemoryFileAbstraction::new()))
} else { } else {
Store::new(storepath, store_config) Store::new(storepath, &config)
}; };
store_result.map(|store| { store_result.map(|store| {

View file

@ -33,6 +33,7 @@ serde = "1"
serde_json = "1" serde_json = "1"
serde_derive = "1" serde_derive = "1"
error-chain = "0.11" error-chain = "0.11"
toml-query = "0.4"
libimagerror = { version = "0.5.0", path = "../../../lib/core/libimagerror" } libimagerror = { version = "0.5.0", path = "../../../lib/core/libimagerror" }
libimagutil = { version = "0.5.0", path = "../../../lib/etc/libimagutil" } libimagutil = { version = "0.5.0", path = "../../../lib/etc/libimagutil" }

View file

@ -21,48 +21,23 @@ use toml::Value;
use store::Result; use store::Result;
use error::StoreError as SE; use error::StoreError as SE;
use error::StoreErrorKind as SEK;
/// Check whether the configuration is valid for the store use toml_query::read::TomlValueReadExt;
pub fn config_is_valid(config: &Option<Value>) -> Result<()> {
use error::StoreErrorKind as SEK;
if config.is_none() {
return Ok(());
}
match *config {
Some(Value::Table(_)) => Ok(()),
_ => {
warn!("Store config is no table");
Err(SE::from_kind(SEK::ConfigTypeError))
},
}
}
/// Checks whether the store configuration has a key "implicit-create" which maps to a boolean /// Checks whether the store configuration has a key "implicit-create" which maps to a boolean
/// value. If that key is present, the boolean is returned, otherwise false is returned. /// value. If that key is present, the boolean is returned, otherwise false is returned.
pub fn config_implicit_store_create_allowed(config: Option<&Value>) -> bool { pub fn config_implicit_store_create_allowed(config: &Option<Value>) -> Result<bool> {
config.map(|t| { let key = "store.implicit-create";
match *t {
Value::Table(ref t) => { if let Some(ref t) = *config {
match t.get("implicit-create") { t.read(key)?
Some(&Value::Boolean(b)) => b, .ok_or(SE::from_kind(SEK::ConfigKeyMissingError(key)))?
Some(_) => { .as_bool()
warn!("Key 'implicit-create' does not contain a Boolean value"); .ok_or(SE::from_kind(SEK::ConfigTypeError(key, "boolean")))
false } else {
} Ok(false)
None => { }
warn!("Key 'implicit-create' in store configuration missing");
false
},
}
}
_ => {
warn!("Store configuration seems to be no Table");
false
},
}
}).unwrap_or(false)
} }
#[cfg(test)] #[cfg(test)]
@ -72,31 +47,33 @@ mod tests {
#[test] #[test]
fn test_implicit_store_create_allowed_no_toml() { fn test_implicit_store_create_allowed_no_toml() {
assert!(!config_implicit_store_create_allowed(None)); assert!(!config_implicit_store_create_allowed(&None).unwrap());
} }
#[test] #[test]
fn test_implicit_store_create_allowed_toml_empty() { fn test_implicit_store_create_allowed_toml_empty() {
let config = toml_from_str("").unwrap(); let config = toml_from_str("").unwrap();
assert!(!config_implicit_store_create_allowed(Some(config).as_ref())); assert!(config_implicit_store_create_allowed(&Some(config)).is_err());
} }
#[test] #[test]
fn test_implicit_store_create_allowed_toml_false() { fn test_implicit_store_create_allowed_toml_false() {
let config = toml_from_str(r#" let config = toml_from_str(r#"
[store]
implicit-create = false implicit-create = false
"#).unwrap(); "#).unwrap();
assert!(!config_implicit_store_create_allowed(Some(config).as_ref())); assert!(!config_implicit_store_create_allowed(&Some(config)).unwrap());
} }
#[test] #[test]
fn test_implicit_store_create_allowed_toml_true() { fn test_implicit_store_create_allowed_toml_true() {
let config = toml_from_str(r#" let config = toml_from_str(r#"
[store]
implicit-create = true implicit-create = true
"#).unwrap(); "#).unwrap();
assert!(config_implicit_store_create_allowed(Some(config).as_ref())); assert!(config_implicit_store_create_allowed(&Some(config)).unwrap());
} }
} }

View file

@ -30,6 +30,7 @@ error_chain! {
Io(::std::io::Error); Io(::std::io::Error);
TomlDeserError(::toml::de::Error); TomlDeserError(::toml::de::Error);
GlobPatternError(::glob::PatternError); GlobPatternError(::glob::PatternError);
TomlQueryError(::toml_query::error::Error);
} }
errors { errors {
@ -39,14 +40,14 @@ error_chain! {
display("Store Configuration Error") display("Store Configuration Error")
} }
ConfigTypeError { ConfigTypeError(key: &'static str, expected: &'static str) {
description("Store configuration type error") description("Store configuration type error")
display("Store configuration type error") display("Store configuration type error at '{}', expected {}", key, expected)
} }
ConfigKeyMissingError { ConfigKeyMissingError(key: &'static str) {
description("Configuration Key missing") description("Configuration Key missing")
display("Configuration Key missing") display("Configuration Key missing: '{}'", key)
} }
VersionError { VersionError {

View file

@ -48,6 +48,7 @@ extern crate walkdir;
extern crate serde_json; extern crate serde_json;
#[macro_use] extern crate serde_derive; #[macro_use] extern crate serde_derive;
#[macro_use] extern crate error_chain; #[macro_use] extern crate error_chain;
extern crate toml_query;
extern crate libimagerror; extern crate libimagerror;
extern crate libimagutil; extern crate libimagutil;

View file

@ -204,11 +204,6 @@ impl Drop for StoreEntry {
pub struct Store { pub struct Store {
location: PathBuf, location: PathBuf,
///
/// Configuration object of the store
///
configuration: Option<Value>,
/// ///
/// Internal Path->File cache map /// Internal Path->File cache map
/// ///
@ -228,10 +223,8 @@ impl Store {
/// Create a new Store object /// Create a new Store object
/// ///
/// This opens a Store in `location` using the configuration from `store_config` (if absent, it /// This opens a Store in `location`. The store_config is used to check whether creating the
/// uses defaults). /// store implicitely is allowed.
///
/// If the configuration is not valid, this fails.
/// ///
/// If the location does not exist, creating directories is by default denied and the operation /// If the location does not exist, creating directories is by default denied and the operation
/// fails, if not configured otherwise. /// fails, if not configured otherwise.
@ -243,7 +236,7 @@ impl Store {
/// ///
/// - On success: Store object /// - On success: Store object
/// ///
pub fn new(location: PathBuf, store_config: Option<Value>) -> Result<Store> { pub fn new(location: PathBuf, store_config: &Option<Value>) -> Result<Store> {
let backend = Box::new(FSFileAbstraction::new()); let backend = Box::new(FSFileAbstraction::new());
Store::new_with_backend(location, store_config, backend) Store::new_with_backend(location, store_config, backend)
} }
@ -253,16 +246,13 @@ impl Store {
/// ///
/// Do not use directly, only for testing purposes. /// Do not use directly, only for testing purposes.
pub fn new_with_backend(location: PathBuf, pub fn new_with_backend(location: PathBuf,
store_config: Option<Value>, store_config: &Option<Value>,
backend: Box<FileAbstraction>) -> Result<Store> { backend: Box<FileAbstraction>) -> Result<Store> {
use configuration::*; use configuration::*;
debug!("Validating Store configuration");
let _ = config_is_valid(&store_config).chain_err(|| SEK::ConfigurationError)?;
debug!("Building new Store object"); debug!("Building new Store object");
if !location.exists() { if !location.exists() {
if !config_implicit_store_create_allowed(store_config.as_ref()) { if !config_implicit_store_create_allowed(store_config)? {
return Err(SE::from_kind(SEK::CreateStoreDirDenied)) return Err(SE::from_kind(SEK::CreateStoreDirDenied))
.chain_err(|| SEK::FileError) .chain_err(|| SEK::FileError)
.chain_err(|| SEK::IoError); .chain_err(|| SEK::IoError);
@ -279,7 +269,6 @@ impl Store {
let store = Store { let store = Store {
location: location.clone(), location: location.clone(),
configuration: store_config,
entries: Arc::new(RwLock::new(HashMap::new())), entries: Arc::new(RwLock::new(HashMap::new())),
backend: backend, backend: backend,
}; };
@ -319,11 +308,6 @@ impl Store {
.map(|_| self.backend = backend) .map(|_| self.backend = backend)
} }
/// Get the store configuration
pub fn config(&self) -> Option<&Value> {
self.configuration.as_ref()
}
/// Creates the Entry at the given location (inside the entry) /// Creates the Entry at the given location (inside the entry)
/// ///
/// # Return value /// # Return value
@ -786,7 +770,6 @@ impl Debug for Store {
write!(fmt, " --- Store ---\n")?; write!(fmt, " --- Store ---\n")?;
write!(fmt, "\n")?; write!(fmt, "\n")?;
write!(fmt, " - location : {:?}\n", self.location)?; write!(fmt, " - location : {:?}\n", self.location)?;
write!(fmt, " - configuration : {:?}\n", self.configuration)?;
write!(fmt, "\n")?; write!(fmt, "\n")?;
write!(fmt, "Entries:\n")?; write!(fmt, "Entries:\n")?;
write!(fmt, "{:?}", self.entries)?; write!(fmt, "{:?}", self.entries)?;
@ -1310,7 +1293,7 @@ mod store_tests {
pub fn get_store() -> Store { pub fn get_store() -> Store {
let backend = Box::new(InMemoryFileAbstraction::new()); let backend = Box::new(InMemoryFileAbstraction::new());
Store::new_with_backend(PathBuf::from("/"), None, backend).unwrap() Store::new_with_backend(PathBuf::from("/"), &None, backend).unwrap()
} }
#[test] #[test]
@ -1363,7 +1346,7 @@ mod store_tests {
let backend = StdIoFileAbstraction::new(&mut input, output.clone(), mapper).unwrap(); let backend = StdIoFileAbstraction::new(&mut input, output.clone(), mapper).unwrap();
let backend = Box::new(backend); let backend = Box::new(backend);
Store::new_with_backend(PathBuf::from("/"), None, backend).unwrap() Store::new_with_backend(PathBuf::from("/"), &None, backend).unwrap()
}; };
for n in 1..100 { for n in 1..100 {
@ -1636,7 +1619,7 @@ mod store_tests {
let backend = InMemoryFileAbstraction::new(); let backend = InMemoryFileAbstraction::new();
let backend = Box::new(backend); let backend = Box::new(backend);
Store::new_with_backend(PathBuf::from("/"), None, backend).unwrap() Store::new_with_backend(PathBuf::from("/"), &None, backend).unwrap()
}; };
for n in 1..100 { for n in 1..100 {
@ -1711,7 +1694,7 @@ mod store_tests {
let backend = StdIoFileAbstraction::new(&mut input, output, mapper).unwrap(); let backend = StdIoFileAbstraction::new(&mut input, output, mapper).unwrap();
let backend = Box::new(backend); let backend = Box::new(backend);
Store::new_with_backend(PathBuf::from("/"), None, backend).unwrap() Store::new_with_backend(PathBuf::from("/"), &None, backend).unwrap()
}; };
// Replacing the backend // Replacing the backend

View file

@ -38,7 +38,7 @@ mod test {
use libimagstore::file_abstraction::InMemoryFileAbstraction; use libimagstore::file_abstraction::InMemoryFileAbstraction;
let backend = Box::new(InMemoryFileAbstraction::new()); let backend = Box::new(InMemoryFileAbstraction::new());
Store::new_with_backend(PathBuf::from("/"), None, backend).unwrap() Store::new_with_backend(PathBuf::from("/"), &None, backend).unwrap()
} }
#[test] #[test]

View file

@ -130,7 +130,7 @@ mod tests {
pub fn get_store() -> Store { pub fn get_store() -> Store {
use libimagstore::store::InMemoryFileAbstraction; use libimagstore::store::InMemoryFileAbstraction;
let backend = Box::new(InMemoryFileAbstraction::new()); let backend = Box::new(InMemoryFileAbstraction::new());
Store::new_with_backend(PathBuf::from("/"), None, backend).unwrap() Store::new_with_backend(PathBuf::from("/"), &None, backend).unwrap()
} }
#[test] #[test]

View file

@ -225,7 +225,7 @@ mod tests {
pub fn get_store() -> Store { pub fn get_store() -> Store {
use libimagstore::store::InMemoryFileAbstraction; use libimagstore::store::InMemoryFileAbstraction;
let backend = Box::new(InMemoryFileAbstraction::new()); let backend = Box::new(InMemoryFileAbstraction::new());
Store::new_with_backend(PathBuf::from("/"), None, backend).unwrap() Store::new_with_backend(PathBuf::from("/"), &None, backend).unwrap()
} }
#[test] #[test]

View file

@ -116,7 +116,7 @@ mod tests {
fn get_store() -> Store { fn get_store() -> Store {
use libimagstore::file_abstraction::InMemoryFileAbstraction; use libimagstore::file_abstraction::InMemoryFileAbstraction;
let backend = Box::new(InMemoryFileAbstraction::new()); let backend = Box::new(InMemoryFileAbstraction::new());
Store::new_with_backend(PathBuf::from("/"), None, backend).unwrap() Store::new_with_backend(PathBuf::from("/"), &None, backend).unwrap()
} }
#[test] #[test]

View file

@ -421,7 +421,7 @@ mod tests {
pub fn get_store() -> Store { pub fn get_store() -> Store {
use libimagstore::file_abstraction::InMemoryFileAbstraction; use libimagstore::file_abstraction::InMemoryFileAbstraction;
let backend = Box::new(InMemoryFileAbstraction::new()); let backend = Box::new(InMemoryFileAbstraction::new());
Store::new_with_backend(PathBuf::from("/"), None, backend).unwrap() Store::new_with_backend(PathBuf::from("/"), &None, backend).unwrap()
} }

View file

@ -778,7 +778,7 @@ mod test {
pub fn get_store() -> Store { pub fn get_store() -> Store {
use libimagstore::file_abstraction::InMemoryFileAbstraction; use libimagstore::file_abstraction::InMemoryFileAbstraction;
let backend = Box::new(InMemoryFileAbstraction::new()); let backend = Box::new(InMemoryFileAbstraction::new());
Store::new_with_backend(PathBuf::from("/"), None, backend).unwrap() Store::new_with_backend(PathBuf::from("/"), &None, backend).unwrap()
} }
#[test] #[test]

View file

@ -225,7 +225,7 @@ mod tests {
pub fn get_store() -> Store { pub fn get_store() -> Store {
use libimagstore::file_abstraction::InMemoryFileAbstraction; use libimagstore::file_abstraction::InMemoryFileAbstraction;
let fs = InMemoryFileAbstraction::new(); let fs = InMemoryFileAbstraction::new();
Store::new_with_backend(PathBuf::from("/"), None, Box::new(fs)).unwrap() Store::new_with_backend(PathBuf::from("/"), &None, Box::new(fs)).unwrap()
} }
#[test] #[test]