1use std::{
2 ffi::OsStr,
3 path::Path,
4 process::{Command, Stdio},
5};
6
7pub trait CommandExtra: Sized {
8 fn with_current_dir(self, dir: impl AsRef<Path>) -> Self;
9 fn with_env(self, key: impl AsRef<OsStr>, value: impl AsRef<OsStr>) -> Self;
10 fn without_env(self, key: impl AsRef<OsStr>) -> Self;
11 fn with_no_env(self) -> Self;
12 fn with_arg(self, arg: impl AsRef<OsStr>) -> Self;
13 fn with_stdin(self, stdio: Stdio) -> Self;
14 fn with_stdout(self, stdio: Stdio) -> Self;
15 fn with_stderr(self, stdio: Stdio) -> Self;
16
17 fn with_args<Args>(self, args: Args) -> Self
18 where
19 Args: IntoIterator,
20 Args::Item: AsRef<OsStr>,
21 {
22 args.into_iter().fold(self, |cmd, arg| cmd.with_arg(arg))
23 }
24}
25
26impl CommandExtra for Command {
27 fn with_current_dir(mut self, dir: impl AsRef<Path>) -> Self {
28 self.current_dir(dir);
29 self
30 }
31
32 fn with_env(mut self, key: impl AsRef<OsStr>, value: impl AsRef<OsStr>) -> Self {
33 self.env(key, value);
34 self
35 }
36
37 fn without_env(mut self, key: impl AsRef<OsStr>) -> Self {
38 self.env_remove(key);
39 self
40 }
41
42 fn with_no_env(mut self) -> Self {
43 self.env_clear();
44 self
45 }
46
47 fn with_arg(mut self, arg: impl AsRef<OsStr>) -> Self {
48 self.arg(arg);
49 self
50 }
51
52 fn with_stdin(mut self, stdio: Stdio) -> Self {
53 self.stdin(stdio);
54 self
55 }
56
57 fn with_stdout(mut self, stdio: Stdio) -> Self {
58 self.stdout(stdio);
59 self
60 }
61
62 fn with_stderr(mut self, stdio: Stdio) -> Self {
63 self.stderr(stdio);
64 self
65 }
66}