History

feat: allow setting more information when preparing commands.

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

Sebastian Thiel committed 3 years ago

Commit 8a67c131 parent 6b375d32 files · +85 −9Browse files

Showing the changes to git-command/src (1 of 2 files). Show all

Unified · Split

git-command/src/lib.rs+40−9
@@ -6,15 +6,24 @@ use std::ffi::OsString; · 5 unmodified lines
66
77 /// A structure to keep settings to use when invoking a command via [`spawn()`][Prepare::spawn()], after creating it with [`prepare()`].
88 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,
1523 }
1624
1725 mod prepare {
26+ use std::ffi::OsString;
1827 use std::process::{Command, Stdio};
1928
2029 use bstr::ByteSlice;
@@ -34,6 +43,14 @@ mod prepare { · 13 unmodified lines
3443 self
3544 }
3645
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+
3754 /// Configure the process to use `stdio` for _stdin.
3855 pub fn stdin(mut self, stdio: Stdio) -> Self {
3956 self.stdin = stdio;
@@ -51,18 +68,30 @@ mod prepare { · 11 unmodified lines
5168 }
5269
5370 /// 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 {
5572 self.args.push(arg.into());
5673 self
5774 }
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+ }
5888 }
5989
6090 /// Finalization
6191 impl Prepare {
6292 /// Spawn the command as configured.
6393 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()
6695 }
6796 }
6897
@@ -83,6 +112,7 @@ mod prepare { · 14 unmodified lines
83112 cmd.stdin(prep.stdin)
84113 .stdout(prep.stdout)
85114 .stderr(prep.stderr)
115+ .envs(prep.env)
86116 .args(prep.args);
87117 cmd
88118 }
@@ -103,6 +133,7 @@ pub fn prepare(cmd: impl Into<OsString>) -> Prepare { · 14 unmodified lines
103133 stdout: std::process::Stdio::piped(),
104134 stderr: std::process::Stdio::inherit(),
105135 args: Vec::new(),
136+ env: Vec::new(),
106137 use_shell: false,
107138 }
108139 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t