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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//! [`Command`] — a builder describing a process to run.
use std::ffi::{OsStr, OsString};
use std::fmt;
use std::path::Path;
use std::process::Stdio;
use std::sync::Arc;
use std::time::Duration;
use encoding_rs::{Encoding, UTF_8};
use crate::buffer::OutputBufferPolicy;
use crate::error::Result;
use crate::pump::LineHandler;
use crate::result::ProcessResult;
use crate::runner::{JobRunner, ProcessRunnerExt};
use crate::running::RunningProcess;
use crate::stdin::Stdin;
/// A description of a child process to launch: program, arguments, working
/// directory, environment, stdin source, and an optional timeout.
///
/// This collapses the .NET `ProcessStartInfo` + `ProcessRunOptions` pair into a
/// single Rust builder. Build it, then either drive it to completion with a
/// helper ([`output_string`](Self::output_string), [`run`](Self::run), …) or
/// start it via a [`ProcessRunner`](crate::ProcessRunner) for streaming/shared
/// groups.
#[derive(Clone)]
pub struct Command {
program: OsString,
args: Vec<OsString>,
cwd: Option<OsString>,
envs: Vec<(OsString, Option<OsString>)>,
env_clear: bool,
stdin: Option<Stdin>,
keep_stdin_open: bool,
timeout: Option<Duration>,
stdout_handler: Option<LineHandler>,
stderr_handler: Option<LineHandler>,
output_buffer: OutputBufferPolicy,
stdout_encoding: &'static Encoding,
stderr_encoding: &'static Encoding,
}
impl Command {
/// Start a command for `program` (resolved on `PATH`).
pub fn new(program: impl AsRef<OsStr>) -> Self {
Self {
program: program.as_ref().to_os_string(),
args: Vec::new(),
cwd: None,
envs: Vec::new(),
env_clear: false,
stdin: None,
keep_stdin_open: false,
timeout: None,
stdout_handler: None,
stderr_handler: None,
output_buffer: OutputBufferPolicy::unbounded(),
stdout_encoding: UTF_8,
stderr_encoding: UTF_8,
}
}
/// Append a single argument.
pub fn arg(mut self, arg: impl AsRef<OsStr>) -> Self {
self.args.push(arg.as_ref().to_os_string());
self
}
/// Append several arguments.
pub fn args<I, S>(mut self, args: I) -> Self
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
self.args
.extend(args.into_iter().map(|a| a.as_ref().to_os_string()));
self
}
/// Set the working directory.
pub fn current_dir(mut self, dir: impl AsRef<Path>) -> Self {
self.cwd = Some(dir.as_ref().as_os_str().to_os_string());
self
}
/// Set (or, with a `None` value, remove) an environment variable.
pub fn env(mut self, key: impl AsRef<OsStr>, value: impl AsRef<OsStr>) -> Self {
self.envs.push((
key.as_ref().to_os_string(),
Some(value.as_ref().to_os_string()),
));
self
}
/// Remove an environment variable inherited from the parent.
pub fn env_remove(mut self, key: impl AsRef<OsStr>) -> Self {
self.envs.push((key.as_ref().to_os_string(), None));
self
}
/// Clear all inherited environment variables before applying any set here.
pub fn env_clear(mut self) -> Self {
self.env_clear = true;
self
}
/// Provide standard input for the child (see [`Stdin`]).
pub fn stdin(mut self, stdin: Stdin) -> Self {
self.stdin = Some(stdin);
self
}
/// Kill the run if it exceeds `timeout`.
pub fn timeout(mut self, timeout: Duration) -> Self {
self.timeout = Some(timeout);
self
}
/// Leave stdin open after start so the child can be driven interactively via
/// [`RunningProcess::standard_input`](crate::RunningProcess::standard_input).
/// Has no effect on the bulk run helpers (they always close stdin).
pub fn keep_stdin_open(mut self) -> Self {
self.keep_stdin_open = true;
self
}
/// Invoke `handler` for each decoded stdout line as it is read (in addition
/// to capture/streaming). Runs on the pump task; keep it cheap and panic-free.
pub fn on_stdout_line<F>(mut self, handler: F) -> Self
where
F: Fn(&str) + Send + Sync + 'static,
{
self.stdout_handler = Some(Arc::new(handler));
self
}
/// Invoke `handler` for each decoded stderr line as it is read.
pub fn on_stderr_line<F>(mut self, handler: F) -> Self
where
F: Fn(&str) + Send + Sync + 'static,
{
self.stderr_handler = Some(Arc::new(handler));
self
}
/// Cap the in-memory backlog of captured output lines (see
/// [`OutputBufferPolicy`]). The pump still drains the pipe; only retention is
/// bounded.
pub fn output_buffer(mut self, policy: OutputBufferPolicy) -> Self {
self.output_buffer = policy;
self
}
/// Decode stdout with `encoding` instead of UTF-8 (e.g.
/// `encoding_rs::SHIFT_JIS`).
pub fn stdout_encoding(mut self, encoding: &'static Encoding) -> Self {
self.stdout_encoding = encoding;
self
}
/// Decode stderr with `encoding` instead of UTF-8.
pub fn stderr_encoding(mut self, encoding: &'static Encoding) -> Self {
self.stderr_encoding = encoding;
self
}
/// Decode both stdout and stderr with `encoding`.
pub fn encoding(mut self, encoding: &'static Encoding) -> Self {
self.stdout_encoding = encoding;
self.stderr_encoding = encoding;
self
}
// --- Accessors used by the runner layer --------------------------------
pub(crate) fn keeps_stdin_open(&self) -> bool {
self.keep_stdin_open
}
pub(crate) fn stdout_handler(&self) -> Option<LineHandler> {
self.stdout_handler.clone()
}
pub(crate) fn stderr_handler(&self) -> Option<LineHandler> {
self.stderr_handler.clone()
}
pub(crate) fn output_buffer_policy(&self) -> OutputBufferPolicy {
self.output_buffer
}
pub(crate) fn out_encoding(&self) -> &'static Encoding {
self.stdout_encoding
}
pub(crate) fn err_encoding(&self) -> &'static Encoding {
self.stderr_encoding
}
pub(crate) fn program_name(&self) -> String {
self.program.to_string_lossy().into_owned()
}
// ----- Public accessors -----------------------------------------------
// Exposed so external `ScriptedRunner::when(|cmd| …)` predicates and other
// inspection can read what a command will run. Named to avoid clashing with
// the same-named builder methods (`program` has no builder; `arguments` vs
// `args`, `working_dir` vs `current_dir`, etc.).
/// The program to launch.
pub fn program(&self) -> &OsStr {
&self.program
}
/// The arguments, in order.
pub fn arguments(&self) -> &[OsString] {
&self.args
}
/// The working-directory override, if one was set.
pub fn working_dir(&self) -> Option<&Path> {
self.cwd.as_deref().map(Path::new)
}
/// The environment overrides, in order (a `None` value removes the variable).
pub fn env_overrides(&self) -> &[(OsString, Option<OsString>)] {
&self.envs
}
/// The configured stdin source, if any.
pub fn stdin_source(&self) -> Option<&Stdin> {
self.stdin.as_ref()
}
/// The configured timeout, if any.
pub fn configured_timeout(&self) -> Option<Duration> {
self.timeout
}
/// Build a `tokio::process::Command` with this command's program, args,
/// working dir, and environment — stdio wired for capture. Use it to feed
/// the low-level [`ProcessGroup::spawn`](crate::ProcessGroup::spawn) escape
/// hatch directly (which returns a raw [`tokio::process::Child`]).
pub fn to_tokio_command(&self) -> tokio::process::Command {
self.build_tokio()
}
/// Build the `tokio` command with stdio wired for capture. Containment
/// (cgroup/job/process-group) is added by the group's `spawn`.
pub(crate) fn build_tokio(&self) -> tokio::process::Command {
let mut cmd = tokio::process::Command::new(&self.program);
cmd.args(&self.args);
if let Some(cwd) = &self.cwd {
cmd.current_dir(cwd);
}
if self.env_clear {
cmd.env_clear();
}
for (key, value) in &self.envs {
match value {
Some(val) => {
cmd.env(key, val);
}
None => {
cmd.env_remove(key);
}
}
}
cmd.stdout(Stdio::piped());
cmd.stderr(Stdio::piped());
if self.keep_stdin_open {
// Interactive: keep a pipe open for the caller to write to.
cmd.stdin(Stdio::piped());
} else {
match &self.stdin {
Some(src) => {
cmd.stdin(src.stdio());
}
// No source given: close stdin so the child reads EOF at start.
None => {
cmd.stdin(Stdio::null());
}
}
}
cmd
}
// --- Live handle (private one-shot group) ------------------------------
/// Start the command and return a live [`RunningProcess`] backed by a fresh
/// private group. Use this for streaming stdout
/// ([`RunningProcess::stdout_lines`]) or inspecting the process while it
/// runs; keep the handle in scope, as dropping it tears the tree down.
pub async fn start(&self) -> Result<RunningProcess> {
JobRunner::new().start(self).await
}
// --- High-level run helpers (private one-shot group) -------------------
/// Run to completion and capture stdout as text, stderr, and the exit code.
/// A non-zero exit is reported, not raised — call
/// [`ProcessResult::ensure_success`] to turn it into an error.
pub async fn output_string(&self) -> Result<ProcessResult<String>> {
JobRunner::new().start(self).await?.output_string().await
}
/// Run to completion and capture stdout as raw bytes (plus stderr/exit code).
pub async fn output_bytes(&self) -> Result<ProcessResult<Vec<u8>>> {
JobRunner::new().start(self).await?.output_bytes().await
}
/// Run to completion and return just the exit code (output is discarded). A
/// run killed by its timeout has no meaningful code, so it surfaces as
/// [`Error::Timeout`](crate::Error::Timeout) — consistent with
/// [`ProcessRunnerExt::exit_code`](crate::ProcessRunnerExt::exit_code) and
/// [`CliClient::code`](crate::CliClient::code).
pub async fn exit_code(&self) -> Result<i32> {
JobRunner::new().exit_code(self).await
}
/// Run to completion, requiring a zero exit, and return trimmed stdout.
pub async fn run(&self) -> Result<String> {
let result = self.output_string().await?.ensure_success()?;
Ok(result.into_stdout().trim_end().to_owned())
}
/// Return the first stdout line matching `predicate` (or the first line when
/// the predicate is trivial), then tear the process down.
pub async fn first_line<F>(&self, predicate: F) -> Result<Option<String>>
where
F: Fn(&str) -> bool,
{
use tokio_stream::StreamExt;
let mut process = JobRunner::new().start(self).await?;
let mut lines = process.stdout_lines();
while let Some(line) = lines.next().await {
if predicate(&line) {
return Ok(Some(line));
}
}
Ok(None)
}
}
impl fmt::Debug for Command {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Command")
.field("program", &self.program)
.field("args", &self.args)
.field("cwd", &self.cwd)
.field("envs", &self.envs)
.field("env_clear", &self.env_clear)
.field("stdin", &self.stdin)
.field("keep_stdin_open", &self.keep_stdin_open)
.field("timeout", &self.timeout)
.field("has_stdout_handler", &self.stdout_handler.is_some())
.field("has_stderr_handler", &self.stderr_handler.is_some())
.field("output_buffer", &self.output_buffer)
.field("stdout_encoding", &self.stdout_encoding.name())
.field("stderr_encoding", &self.stderr_encoding.name())
.finish()
}
}