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
6 6
7/// A structure to keep settings to use when invoking a command via [`spawn()`][Prepare::spawn()], after creating it with [`prepare()`].7/// A structure to keep settings to use when invoking a command via [`spawn()`][Prepare::spawn()], after creating it with [`prepare()`].
8pub struct Prepare {8pub struct Prepare {
9 command: OsString,9 /// The command to invoke (either with or without shell depending on `use_shell`.
10 stdin: std::process::Stdio,10 pub command: OsString,
11 stdout: std::process::Stdio,11 /// The way standard input is configured.
12 stderr: std::process::Stdio,12 pub stdin: std::process::Stdio,
13 args: Vec<OsString>,13 /// The way standard output is configured.
14 use_shell: bool,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
17mod prepare {25mod prepare {
26 use std::ffi::OsString;
18 use std::process::{Command, Stdio};27 use std::process::{Command, Stdio};
19 28
20 use bstr::ByteSlice;29 use bstr::ByteSlice;
@@ -34,6 +43,14 @@ mod prepare { · 13 unmodified lines
34 self43 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 /// Configure the process to use `stdio` for _stdin.54 /// Configure the process to use `stdio` for _stdin.
38 pub fn stdin(mut self, stdio: Stdio) -> Self {55 pub fn stdin(mut self, stdio: Stdio) -> Self {
39 self.stdin = stdio;56 self.stdin = stdio;
@@ -51,18 +68,30 @@ mod prepare { · 11 unmodified lines
51 }68 }
52 69
53 /// Add `arg` to the list of arguments to call the command with.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 self.args.push(arg.into());72 self.args.push(arg.into());
56 self73 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 /// Finalization90 /// Finalization
61 impl Prepare {91 impl Prepare {
62 /// Spawn the command as configured.92 /// Spawn the command as configured.
63 pub fn spawn(self) -> std::io::Result<std::process::Child> {93 pub fn spawn(self) -> std::io::Result<std::process::Child> {
64 let mut cmd: Command = self.into();94 Command::from(self).spawn()
65 cmd.spawn()
66 }95 }
67 }96 }
68 97
@@ -83,6 +112,7 @@ mod prepare { · 14 unmodified lines
83 cmd.stdin(prep.stdin)112 cmd.stdin(prep.stdin)
84 .stdout(prep.stdout)113 .stdout(prep.stdout)
85 .stderr(prep.stderr)114 .stderr(prep.stderr)
115 .envs(prep.env)
86 .args(prep.args);116 .args(prep.args);
87 cmd117 cmd
88 }118 }
@@ -103,6 +133,7 @@ pub fn prepare(cmd: impl Into<OsString>) -> Prepare { · 14 unmodified lines
103 stdout: std::process::Stdio::piped(),133 stdout: std::process::Stdio::piped(),
104 stderr: std::process::Stdio::inherit(),134 stderr: std::process::Stdio::inherit(),
105 args: Vec::new(),135 args: Vec::new(),
136 env: Vec::new(),
106 use_shell: false,137 use_shell: false,
107 }138 }
108}139}

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