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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/// Define a private namespace for all its items.
#[ allow( clippy ::std_instead_of_alloc, clippy ::std_instead_of_core ) ]
mod private
{
use std ::
{
fmt ::Formatter,
path ::{ Path, PathBuf },
process ::{ Command, Output, Stdio },
};
use std ::collections ::HashMap;
use std ::ffi ::OsString;
use duct ::cmd;
use error_tools ::
{
untyped ::{ Error, Context, format_err },
};
use former ::Former;
use iter_tools ::iter ::Itertools;
///
/// Executes a process with the given parameters.
///
/// Routes to the duct backend when `joining_streams` is true (stderr merged into stdout),
/// or to `std::process::Command` when false (separate stdout and stderr capture).
///
fn execute
(
bin_path : &Path,
args : &[ OsString ],
current_path : &Path,
joining_streams : bool,
env : HashMap< String, String >,
) -> Result< Output, Error >
{
if joining_streams
{
cmd( bin_path.as_os_str(), args )
.dir( current_path )
.full_env( env )
.stderr_to_stdout()
.stdout_capture()
.unchecked()
.run()
.map_err( Into ::into )
}
else
{
Command ::new( bin_path )
.args( args )
.envs( env )
.stdout( Stdio ::piped() )
.stderr( Stdio ::piped() )
.current_dir( current_path )
.spawn()
.context( "failed to spawn process" )?
.wait_with_output()
.context( "failed to wait on child" )
}
}
///
/// Executes an external process in a specified directory without using a shell.
///
/// # Arguments
/// - `options` : Configured [`Run`] describing the process to execute.
///
/// # Returns
/// `Ok( Report )` on successful execution (exit code zero).
/// `Err( Report )` when the process fails to spawn, exits non-zero, or produces non-UTF-8 output.
///
/// # Errors
/// Returns `Err( Report )` if:
/// - The binary cannot be found or spawned.
/// - The process exits with a non-zero exit code.
/// - Captured output is not valid UTF-8.
///
pub fn run( options : Run ) -> Result< Report, Report >
{
// Destructure upfront to avoid partial-move conflicts when consuming env_variable.
let Run { bin_path, current_path, args, joining_streams, env_variable } = options;
let bin_path_ref : &Path = bin_path.as_ref();
let current_path_ref : &Path = current_path.as_ref();
let mut env : HashMap< String, String > = std ::env ::vars().collect();
env.extend( env_variable );
let mut report = Report
{
command : format!( "{} {}", bin_path_ref.display(), args.iter().map( | a | a.to_string_lossy() ).join( " " ) ),
current_path : current_path_ref.to_path_buf(),
..Report ::default()
};
let output = match execute( bin_path_ref, &args, current_path_ref, joining_streams, env )
{
Ok( o ) => o,
Err( e ) =>
{
report.error = Err( e );
return Err( report );
}
};
let out = match String ::from_utf8( output.stdout ).context( "Found invalid UTF-8" )
{
Ok( s ) => s,
Err( e ) =>
{
report.error = Err( e );
return Err( report );
}
};
report.out = out;
let err = match String ::from_utf8( output.stderr ).context( "Found invalid UTF-8" )
{
Ok( s ) => s,
Err( e ) =>
{
report.error = Err( e );
return Err( report );
}
};
report.err = err;
if output.status.success()
{
Ok( report )
}
else
{
report.error = Err( format_err!( "Process was finished with error code: {}", output.status ) );
Err( report )
}
}
/// Options for the [`run`] function.
#[ derive( Debug, Former ) ]
pub struct Run
{
/// Path to the executable to run.
bin_path : PathBuf,
/// Working directory for the process.
current_path : PathBuf,
/// Command-line arguments passed to the executable.
args : Vec< OsString >,
/// When `true`, stderr is merged into stdout via duct. When `false`, streams are captured separately.
#[ former( default = false ) ]
joining_streams : bool,
/// Additional environment variables merged on top of the current process environment.
env_variable : HashMap< String, String >,
}
impl RunFormer
{
/// Execute the configured process, returning a [`Report`].
///
/// # Returns
/// `Ok( Report )` on zero exit code, `Err( Report )` on failure.
///
/// # Example
///
/// ```rust
/// # use process_tools ::process ::Run;
/// let report = Run ::former()
/// .bin_path( "echo" )
/// .args( vec![ "hello".into() ] )
/// .current_path( "." )
/// .run()
/// .expect( "echo should succeed" );
///
/// assert!( report.out.contains( "hello" ) );
/// ```
pub fn run( self ) -> Result< Report, Report >
{
run( self.form() )
}
/// Executes an external process using the system shell.
///
/// Abstracts over shell differences between Windows (`cmd /C`) and Unix (`sh -c`),
/// enabling shell features such as pipes and redirections in the command string.
///
/// # Arguments
/// - `exec_path` : The shell command string to execute.
///
/// # Returns
/// `Ok( Report )` on zero exit code, `Err( Report )` on failure.
///
/// # Example
///
/// ```rust
/// # use process_tools ::process ::Run;
/// let report = Run ::former()
/// .current_path( "." )
/// .run_with_shell( "echo hello | grep hello" )
/// .expect( "piped command should succeed" );
///
/// assert!( report.out.contains( "hello" ) );
/// ```
pub fn run_with_shell( self, exec_path : &str, ) -> Result< Report, Report >
{
let ( program, args ) =
if cfg!( target_os = "windows" )
{
( "cmd", [ "/C", exec_path ] )
}
else
{
( "sh", [ "-c", exec_path ] )
};
self
.args( args.into_iter().map( OsString ::from ).collect ::< Vec< _ > >() )
.bin_path( program )
.run()
}
}
/// Process command output.
#[ derive( Debug, ) ]
pub struct Report
{
/// Command that was executed.
pub command : String,
/// Path where command was executed.
pub current_path : PathBuf,
/// Captured stdout.
pub out : String,
/// Captured stderr.
pub err : String,
/// `Ok(())` if the process succeeded, `Err` with the failure reason otherwise.
pub error : Result< (), Error >,
}
impl Clone for Report
{
fn clone( &self ) -> Self
{
Self
{
command : self.command.clone(),
current_path : self.current_path.clone(),
out : self.out.clone(),
err : self.err.clone(),
// Error is not Clone; stringify to preserve the message across the clone boundary.
error : self.error.as_ref().map_err( | e | Error ::msg( e.to_string() ) ).copied(),
}
}
}
impl Default for Report
{
fn default() -> Self
{
Report
{
command : String ::default(),
current_path : PathBuf ::new(),
out : String ::default(),
err : String ::default(),
error : Ok( () ),
}
}
}
impl core ::fmt ::Display for Report
{
fn fmt( &self, f : &mut Formatter< '_ > ) -> core ::fmt ::Result
{
// Trim prevents writing unnecessary whitespace or empty lines
f.write_fmt( format_args!( "> {}\n", self.command ) )?;
f.write_fmt( format_args!( " @ {}\n\n", self.current_path.display() ) )?;
if !self.out.trim().is_empty()
{
f.write_fmt( format_args!( " {}\n", self.out.replace( '\n', "\n " ) ) )?;
}
if !self.err.trim().is_empty()
{
f.write_fmt( format_args!( " {}\n", self.err.replace( '\n', "\n " ) ) )?;
}
Ok( () )
}
}
}
crate ::mod_interface!
{
own use run;
own use Run;
own use Report;
}