Skip to content

Commit

Permalink
add save method
Browse files Browse the repository at this point in the history
Signed-off-by: Freyskeyd <[email protected]>
  • Loading branch information
Freyskeyd committed Oct 13, 2017
1 parent 21c1ede commit f33b192
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ readme = "README.md"
keywords = ["environment", "env"]

[dependencies]
regex = "0.2"
62 changes: 61 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
extern crate regex;
use regex::Regex;
use std::ffi::OsString;

/// Structure to deal with environment variables
Expand All @@ -7,13 +9,15 @@ pub struct Environment {
vars: Vec<(OsString, OsString)>,
/// Define if the structure must inherit
inherit: bool,
save_pattern: Option<String>,
}

impl Default for Environment {
fn default() -> Self {
Self {
vars: vec![],
inherit: false,
save_pattern: None,
}
}
}
Expand All @@ -36,6 +40,7 @@ impl Environment {
Self {
vars: vec![],
inherit: true,
save_pattern: None,
}
}

Expand Down Expand Up @@ -70,12 +75,49 @@ impl Environment {
self
}

/// Define a regex that will be used to save some existing variables.
/// It's only used when having an inherit equal to false.
///
/// # Examples
///
/// ```rust
/// extern crate environment;
///
/// use std::ffi::OsString;
///
/// ::std::env::set_var("BAR", "BAZ");
///
/// let e = environment::Environment::empty().insert("foo", "bar").compile();
///
/// assert_eq!(e, vec![(OsString::from("foo"), OsString::from("bar"))]);
///
/// let e = environment::Environment::empty().save("BAR").insert("foo", "bar").compile();
///
/// assert_eq!(e, vec![(OsString::from("BAR"), OsString::from("BAZ")), (OsString::from("foo"), OsString::from("bar"))]);
/// ```
pub fn save<S: Into<String>>(mut self, pattern: S) -> Self {
self.save_pattern = Some(pattern.into());

self
}

/// Compile Environment object
pub fn compile(self) -> Vec<(OsString, OsString)> {
if self.inherit {
::std::env::vars_os().chain(self.vars).collect()
} else {
self.vars
match self.save_pattern {
Some(v) => {
let re = Regex::new(&v).unwrap();
::std::env::vars_os()
.filter(|&(ref k, _)|
re.is_match(k.to_str().unwrap())
)
.chain(self.vars)
.collect()
},
None => self.vars
}
}
}
}
Expand Down Expand Up @@ -118,6 +160,7 @@ where
Self {
vars: v.into_iter().map(|k| k.to_environment_tuple()).collect(),
inherit: false,
save_pattern: None,
}
}
}
Expand Down Expand Up @@ -233,4 +276,21 @@ mod test {
assert!(output.contains("bar=vv"));
assert!(!output.contains("bar=baz"));
}

#[test]
fn save_pattern() {
let e: Environment = vec![("foo", "bar")].into();

let mut c = Command::new("printenv");

let output = c.env_clear()
.envs(e.clone().save("PAT.*").compile())
.output()
.expect("failed to execute command");

let output = String::from_utf8_lossy(&output.stdout);

assert!(output.contains("foo=bar"));
assert!(output.contains("PATH"));
}
}

0 comments on commit f33b192

Please sign in to comment.