1
+ extern crate regex;
2
+ use regex:: Regex ;
1
3
use std:: ffi:: OsString ;
2
4
3
5
/// Structure to deal with environment variables
@@ -7,13 +9,15 @@ pub struct Environment {
7
9
vars : Vec < ( OsString , OsString ) > ,
8
10
/// Define if the structure must inherit
9
11
inherit : bool ,
12
+ save_pattern : Option < String > ,
10
13
}
11
14
12
15
impl Default for Environment {
13
16
fn default ( ) -> Self {
14
17
Self {
15
18
vars : vec ! [ ] ,
16
19
inherit : false ,
20
+ save_pattern : None ,
17
21
}
18
22
}
19
23
}
@@ -36,6 +40,7 @@ impl Environment {
36
40
Self {
37
41
vars : vec ! [ ] ,
38
42
inherit : true ,
43
+ save_pattern : None ,
39
44
}
40
45
}
41
46
@@ -70,12 +75,53 @@ impl Environment {
70
75
self
71
76
}
72
77
78
+ /// Define a regex that will be used to save some existing variables.
79
+ /// It's only used when having an inherit equal to false.
80
+ ///
81
+ /// # Examples
82
+ ///
83
+ /// ```rust
84
+ /// extern crate environment;
85
+ ///
86
+ /// use std::ffi::OsString;
87
+ ///
88
+ /// ::std::env::set_var("BAR", "BAZ");
89
+ ///
90
+ /// let e = environment::Environment::empty().insert("foo", "bar").compile();
91
+ ///
92
+ /// assert_eq!(e, vec![(OsString::from("foo"), OsString::from("bar"))]);
93
+ ///
94
+ /// let e = environment::Environment::empty().save("BAR").insert("foo", "bar").compile();
95
+ ///
96
+ /// assert_eq!(
97
+ /// e,
98
+ /// vec![
99
+ /// (OsString::from("BAR"), OsString::from("BAZ")),
100
+ /// (OsString::from("foo"), OsString::from("bar"))
101
+ /// ]
102
+ /// );
103
+ /// ```
104
+ pub fn save < S : Into < String > > ( mut self , pattern : S ) -> Self {
105
+ self . save_pattern = Some ( pattern. into ( ) ) ;
106
+
107
+ self
108
+ }
109
+
73
110
/// Compile Environment object
74
111
pub fn compile ( self ) -> Vec < ( OsString , OsString ) > {
75
112
if self . inherit {
76
113
:: std:: env:: vars_os ( ) . chain ( self . vars ) . collect ( )
77
114
} else {
78
- self . vars
115
+ match self . save_pattern {
116
+ Some ( v) => {
117
+ let re = Regex :: new ( & v) . unwrap ( ) ;
118
+ :: std:: env:: vars_os ( )
119
+ . filter ( |& ( ref k, _) | re. is_match ( k. to_str ( ) . unwrap ( ) ) )
120
+ . chain ( self . vars )
121
+ . collect ( )
122
+ }
123
+ None => self . vars
124
+ }
79
125
}
80
126
}
81
127
}
@@ -118,6 +164,7 @@ where
118
164
Self {
119
165
vars : v. into_iter ( ) . map ( |k| k. to_environment_tuple ( ) ) . collect ( ) ,
120
166
inherit : false ,
167
+ save_pattern : None ,
121
168
}
122
169
}
123
170
}
@@ -233,4 +280,21 @@ mod test {
233
280
assert ! ( output. contains( "bar=vv" ) ) ;
234
281
assert ! ( !output. contains( "bar=baz" ) ) ;
235
282
}
283
+
284
+ #[ test]
285
+ fn save_pattern ( ) {
286
+ let e: Environment = vec ! [ ( "foo" , "bar" ) ] . into ( ) ;
287
+
288
+ let mut c = Command :: new ( "printenv" ) ;
289
+
290
+ let output = c. env_clear ( )
291
+ . envs ( e. clone ( ) . save ( "PAT.*" ) . compile ( ) )
292
+ . output ( )
293
+ . expect ( "failed to execute command" ) ;
294
+
295
+ let output = String :: from_utf8_lossy ( & output. stdout ) ;
296
+
297
+ assert ! ( output. contains( "foo=bar" ) ) ;
298
+ assert ! ( output. contains( "PATH" ) ) ;
299
+ }
236
300
}
0 commit comments