This includes - disallowing the usage of a shell - adding mutiple args at once - adding environment variables - make all fields public to allow non-build manipualation of the `Prepare` type
Showing the changes to git-command/src (1 of 2 files). Show all
Unified · Split
| @@ -6,15 +6,24 @@ use std::ffi::OsString; · 5 unmodified lines | ||
| 6 | 6 | |
| 7 | 7 | /// A structure to keep settings to use when invoking a command via [`spawn()`][Prepare::spawn()], after creating it with [`prepare()`]. |
| 8 | 8 | pub struct Prepare { |
| 9 | − command: OsString, | |
| 10 | − stdin: std::process::Stdio, | |
| 11 | − stdout: std::process::Stdio, | |
| 12 | − stderr: std::process::Stdio, | |
| 13 | − args: Vec<OsString>, | |
| 14 | − use_shell: bool, | |
| 9 | + /// The command to invoke (either with or without shell depending on `use_shell`. | |
| 10 | + pub command: OsString, | |
| 11 | + /// The way standard input is configured. | |
| 12 | + pub stdin: std::process::Stdio, | |
| 13 | + /// The way standard output is configured. | |
| 14 | + pub stdout: std::process::Stdio, | |
| 15 | + /// The way standard error is configured. | |
| 16 | + pub stderr: std::process::Stdio, | |
| 17 | + /// The arguments to pass to the spawned process. | |
| 18 | + pub args: Vec<OsString>, | |
| 19 | + /// environment variables to set in the spawned process. | |
| 20 | + pub env: Vec<(OsString, OsString)>, | |
| 21 | + /// If `true`, we will use `sh` to execute the `command`. | |
| 22 | + pub use_shell: bool, | |
| 15 | 23 | } |
| 16 | 24 | |
| 17 | 25 | mod prepare { |
| 26 | + use std::ffi::OsString; | |
| 18 | 27 | use std::process::{Command, Stdio}; |
| 19 | 28 | |
| 20 | 29 | use bstr::ByteSlice; |
| @@ -34,6 +43,14 @@ mod prepare { · 13 unmodified lines | ||
| 34 | 43 | self |
| 35 | 44 | } |
| 36 | 45 | |
| 46 | + /// Unconditionally turn off using the shell when spawning the command. | |
| 47 | + /// Note that not using the shell is the default so an effective use of this method | |
| 48 | + /// is some time after [`with_shell()`][Prepare::with_shell()] was called. | |
| 49 | + pub fn without_shell(mut self) -> Self { | |
| 50 | + self.use_shell = false; | |
| 51 | + self | |
| 52 | + } | |
| 53 | + | |
| 37 | 54 | /// Configure the process to use `stdio` for _stdin. |
| 38 | 55 | pub fn stdin(mut self, stdio: Stdio) -> Self { |
| 39 | 56 | self.stdin = stdio; |
| @@ -51,18 +68,30 @@ mod prepare { · 11 unmodified lines | ||
| 51 | 68 | } |
| 52 | 69 | |
| 53 | 70 | /// Add `arg` to the list of arguments to call the command with. |
| 54 | − pub fn arg(mut self, arg: impl Into<std::ffi::OsString>) -> Self { | |
| 71 | + pub fn arg(mut self, arg: impl Into<OsString>) -> Self { | |
| 55 | 72 | self.args.push(arg.into()); |
| 56 | 73 | self |
| 57 | 74 | } |
| 75 | + | |
| 76 | + /// Add `args` to the list of arguments to call the command with. | |
| 77 | + pub fn args(mut self, args: impl IntoIterator<Item = impl Into<OsString>>) -> Self { | |
| 78 | + self.args | |
| 79 | + .append(&mut args.into_iter().map(Into::into).collect::<Vec<_>>()); | |
| 80 | + self | |
| 81 | + } | |
| 82 | + | |
| 83 | + /// Add `key` with `value` to the environment of the spawned command. | |
| 84 | + pub fn env(mut self, key: impl Into<OsString>, value: impl Into<OsString>) -> Self { | |
| 85 | + self.env.push((key.into(), value.into())); | |
| 86 | + self | |
| 87 | + } | |
| 58 | 88 | } |
| 59 | 89 | |
| 60 | 90 | /// Finalization |
| 61 | 91 | impl Prepare { |
| 62 | 92 | /// Spawn the command as configured. |
| 63 | 93 | pub fn spawn(self) -> std::io::Result<std::process::Child> { |
| 64 | − let mut cmd: Command = self.into(); | |
| 65 | − cmd.spawn() | |
| 94 | + Command::from(self).spawn() | |
| 66 | 95 | } |
| 67 | 96 | } |
| 68 | 97 | |
| @@ -83,6 +112,7 @@ mod prepare { · 14 unmodified lines | ||
| 83 | 112 | cmd.stdin(prep.stdin) |
| 84 | 113 | .stdout(prep.stdout) |
| 85 | 114 | .stderr(prep.stderr) |
| 115 | + .envs(prep.env) | |
| 86 | 116 | .args(prep.args); |
| 87 | 117 | cmd |
| 88 | 118 | } |
| @@ -103,6 +133,7 @@ pub fn prepare(cmd: impl Into<OsString>) -> Prepare { · 14 unmodified lines | ||
| 103 | 133 | stdout: std::process::Stdio::piped(), |
| 104 | 134 | stderr: std::process::Stdio::inherit(), |
| 105 | 135 | args: Vec::new(), |
| 136 | + env: Vec::new(), | |
| 106 | 137 | use_shell: false, |
| 107 | 138 | } |
| 108 | 139 | } |