blob: 5c80badddbe8adeae7565386e5776a62411d72df (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
use std::env;
use std::error::Error;
use std::collections::HashMap;
use source;
use value::Value;
#[derive(Clone)]
pub struct Environment {
/// Optional prefix that would restrict environment consideration
/// to only variables which begin with that prefix.
prefix: Option<String>,
}
impl Environment {
pub fn new<'a, T>(prefix: T) -> Environment
where T: Into<Option<&'a str>>
{
Environment { prefix: prefix.into().map(String::from) }
}
}
impl source::SourceBuilder for Environment {
fn build(&self) -> Result<Box<source::Source + Send + Sync>, Box<Error>> {
Ok(Box::new(self.clone()))
}
}
impl source::Source for Environment {
fn collect(&self) -> HashMap<String, Value> {
// Iterate through environment variables
let mut r = HashMap::new();
// Make prefix pattern
let prefix_pat = if let Some(ref prefix) = self.prefix {
Some(prefix.clone() + "_".into())
} else {
None
};
for (key, value) in env::vars() {
let mut key = key.to_string();
// Check if key matches prefix
if let Some(ref prefix_pat) = prefix_pat {
if key.starts_with(prefix_pat) {
// Remove the prefix from the key
key = key[prefix_pat.len()..].to_string();
} else {
// Skip this key
continue;
}
}
r.insert(key, Value::String(value));
}
r
}
}
|