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,49 @@ 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!(e, vec![(OsString::from("BAR"), OsString::from("BAZ")), (OsString::from("foo"), OsString::from("bar"))]);
97
+ /// ```
98
+ pub fn save < S : Into < String > > ( mut self , pattern : S ) -> Self {
99
+ self . save_pattern = Some ( pattern. into ( ) ) ;
100
+
101
+ self
102
+ }
103
+
73
104
/// Compile Environment object
74
105
pub fn compile ( self ) -> Vec < ( OsString , OsString ) > {
75
106
if self . inherit {
76
107
:: std:: env:: vars_os ( ) . chain ( self . vars ) . collect ( )
77
108
} else {
78
- self . vars
109
+ match self . save_pattern {
110
+ Some ( v) => {
111
+ let re = Regex :: new ( & v) . unwrap ( ) ;
112
+ :: std:: env:: vars_os ( )
113
+ . filter ( |& ( ref k, _) |
114
+ re. is_match ( k. to_str ( ) . unwrap ( ) )
115
+ )
116
+ . chain ( self . vars )
117
+ . collect ( )
118
+ } ,
119
+ None => self . vars
120
+ }
79
121
}
80
122
}
81
123
}
@@ -118,6 +160,7 @@ where
118
160
Self {
119
161
vars : v. into_iter ( ) . map ( |k| k. to_environment_tuple ( ) ) . collect ( ) ,
120
162
inherit : false ,
163
+ save_pattern : None ,
121
164
}
122
165
}
123
166
}
@@ -233,4 +276,21 @@ mod test {
233
276
assert ! ( output. contains( "bar=vv" ) ) ;
234
277
assert ! ( !output. contains( "bar=baz" ) ) ;
235
278
}
279
+
280
+ #[ test]
281
+ fn save_pattern ( ) {
282
+ let e: Environment = vec ! [ ( "foo" , "bar" ) ] . into ( ) ;
283
+
284
+ let mut c = Command :: new ( "printenv" ) ;
285
+
286
+ let output = c. env_clear ( )
287
+ . envs ( e. clone ( ) . save ( "PAT.*" ) . compile ( ) )
288
+ . output ( )
289
+ . expect ( "failed to execute command" ) ;
290
+
291
+ let output = String :: from_utf8_lossy ( & output. stdout ) ;
292
+
293
+ assert ! ( output. contains( "foo=bar" ) ) ;
294
+ assert ! ( output. contains( "PATH" ) ) ;
295
+ }
236
296
}
0 commit comments