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
use std::{ ffi::OsStr, path::Path, process::{Command, Stdio}, }; pub trait CommandExtra: Sized { fn with_current_dir(self, dir: impl AsRef<Path>) -> Self; fn with_env(self, key: impl AsRef<OsStr>, value: impl AsRef<OsStr>) -> Self; fn without_env(self, key: impl AsRef<OsStr>) -> Self; fn with_no_env(self) -> Self; fn with_arg(self, arg: impl AsRef<OsStr>) -> Self; fn with_stdin(self, stdio: Stdio) -> Self; fn with_stdout(self, stdio: Stdio) -> Self; fn with_stderr(self, stdio: Stdio) -> Self; fn with_args<Args>(self, args: Args) -> Self where Args: IntoIterator, Args::Item: AsRef<OsStr>, { args.into_iter().fold(self, |cmd, arg| cmd.with_arg(arg)) } } impl CommandExtra for Command { fn with_current_dir(mut self, dir: impl AsRef<Path>) -> Self { self.current_dir(dir); self } fn with_env(mut self, key: impl AsRef<OsStr>, value: impl AsRef<OsStr>) -> Self { self.env(key, value); self } fn without_env(mut self, key: impl AsRef<OsStr>) -> Self { self.env_remove(key); self } fn with_no_env(mut self) -> Self { self.env_clear(); self } fn with_arg(mut self, arg: impl AsRef<OsStr>) -> Self { self.arg(arg); self } fn with_stdin(mut self, stdio: Stdio) -> Self { self.stdin(stdio); self } fn with_stdout(mut self, stdio: Stdio) -> Self { self.stdout(stdio); self } fn with_stderr(mut self, stdio: Stdio) -> Self { self.stderr(stdio); self } }