summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyan Leckey <ryan@launchbadge.com>2017-06-13 18:34:28 -0700
committerRyan Leckey <ryan@launchbadge.com>2017-06-13 18:34:28 -0700
commit4573e911816ecba90565d33d219e6f04ee7e2f05 (patch)
treeb85fcacf2daa6959def1d6b9b3f7a3f66c882a66
parent3fdb2a3a19bc0248763d2bf15a152b8661534a82 (diff)
Add Environment
-rw-r--r--src/env.rs88
-rw-r--r--src/lib.rs2
2 files changed, 90 insertions, 0 deletions
diff --git a/src/env.rs b/src/env.rs
new file mode 100644
index 0000000..c49bb0e
--- /dev/null
+++ b/src/env.rs
@@ -0,0 +1,88 @@
+use std::env;
+use std::collections::HashMap;
+use error::*;
+use source::Source;
+use value::{Value, ValueKind};
+
+pub struct Environment {
+ /// Optional prefix that will limit access to the environment to only keys that
+ /// begin with the defined prefix.
+ ///
+ /// A prefix, followed by `_` (the seperator),
+ /// is tested to be present on each key before its considered
+ /// to be part of the source environment.
+ ///
+ /// For example, the key `CONFIG_DEBUG` would become `DEBUG` with a prefix of `config`.
+ prefix: Option<String>,
+
+ /// The character sequence that separates each key segment in an environment key pattern.
+ /// Consider a nested configuration such as `redis.password`, a separator of `_` would allow
+ /// an environment key of `REDIS_PASSWORD` to match.
+ ///
+ /// The default separator is `_`.
+ separator: String,
+}
+
+impl Environment {
+ pub fn new() -> Self {
+ Environment::default()
+ }
+
+ pub fn with_prefix(s: String) -> Self {
+ Environment { separator: s, ..Environment::default() }
+ }
+
+ pub fn prefix(&mut self, s: String) -> &mut Self {
+ self.prefix = s.into();
+ self
+ }
+
+ pub fn separator(&mut self, s: String) -> &mut Self {
+ self.separator = s;
+ self
+ }
+}
+
+impl Default for Environment {
+ fn default() -> Environment {
+ Environment {
+ prefix: None,
+ separator: "_".into(),
+ }
+ }
+}
+
+impl Source for Environment {
+ fn collect(&self) -> Result<HashMap<String, Value>> {
+ let mut m = HashMap::new();
+ let uri: String = "the environment".into();
+
+ // Define a prefiux pattern to test and exclude from keys
+ let prefix_pattern = match self.prefix {
+ Some(ref prefix) => Some(prefix.clone() + &self.separator),
+ _ => None,
+ };
+
+ for (key, value) in env::vars() {
+ let mut key = key.to_string();
+
+ // Check for prefix
+ if let Some(ref prefix_pattern) = prefix_pattern {
+ if key.starts_with(prefix_pattern) {
+ // Remove this prefix from the key
+ key = key[prefix_pattern.len()..].to_string();
+ } else {
+ // Skip this key
+ continue;
+ }
+ }
+
+ // Replace `separator` with `.`
+ key = key.replace(&self.separator, ".");
+
+ m.insert(key, Value::new(Some(&uri), ValueKind::String(value)));
+ }
+
+ Ok(m)
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
index 534533d..fe02a93 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -43,9 +43,11 @@ mod path;
mod source;
mod config;
mod file;
+mod env;
pub use config::Config;
pub use error::ConfigError;
pub use value::Value;
pub use source::Source;
pub use file::{File, FileFormat};
+pub use env::Environment;