-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: prefactor process manager #7034
Merged
chris-olszewski
merged 8 commits into
main
from
olszewski/chore_prefactor_process_manager
Jan 22, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2a2ad8f
chore: hide command builder implementation
chris-olszewski 7c6a6f6
chore: switch process manager interface to use our command builder
chris-olszewski 1a812f4
chore: move child label logic into command builder
chris-olszewski e1a55dc
chore: remove ability to independently capture stderr
chris-olszewski ce35a1d
chore: make stdout/stderr/stdin getters private
chris-olszewski ad5a558
chore: PR feedback, alter API to better match stdlib
chris-olszewski af0381a
Merge branch 'main' into olszewski/chore_prefactor_process_manager
chris-olszewski 6bcb011
Merge branch 'main' into olszewski/chore_prefactor_process_manager
chris-olszewski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
use std::{ | ||
collections::BTreeMap, | ||
ffi::{OsStr, OsString}, | ||
process::Stdio, | ||
}; | ||
|
||
use itertools::Itertools; | ||
use turbopath::AbsoluteSystemPathBuf; | ||
|
||
/// A command builder that can be used to build both regular | ||
/// child processes and ones spawned hooked up to a PTY | ||
pub struct Command { | ||
program: OsString, | ||
args: Vec<OsString>, | ||
cwd: Option<AbsoluteSystemPathBuf>, | ||
env: BTreeMap<OsString, OsString>, | ||
open_stdin: bool, | ||
env_clear: bool, | ||
} | ||
|
||
impl Command { | ||
pub fn new(program: impl AsRef<OsStr>) -> Self { | ||
let program = program.as_ref().to_os_string(); | ||
Self { | ||
program, | ||
args: Vec::new(), | ||
cwd: None, | ||
env: BTreeMap::new(), | ||
open_stdin: false, | ||
env_clear: false, | ||
} | ||
} | ||
|
||
pub fn args<I, S>(&mut self, args: I) -> &mut Self | ||
where | ||
I: IntoIterator<Item = S>, | ||
S: AsRef<OsStr>, | ||
{ | ||
self.args = args | ||
.into_iter() | ||
.map(|arg| arg.as_ref().to_os_string()) | ||
.collect(); | ||
self | ||
} | ||
|
||
pub fn current_dir(&mut self, dir: AbsoluteSystemPathBuf) -> &mut Self { | ||
self.cwd = Some(dir); | ||
self | ||
} | ||
|
||
pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Self | ||
chris-olszewski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
where | ||
I: IntoIterator<Item = (K, V)>, | ||
K: AsRef<OsStr>, | ||
V: AsRef<OsStr>, | ||
{ | ||
for (ref key, ref val) in vars { | ||
self.env | ||
.insert(key.as_ref().to_os_string(), val.as_ref().to_os_string()); | ||
} | ||
self | ||
} | ||
|
||
pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Self | ||
where | ||
K: AsRef<OsStr>, | ||
V: AsRef<OsStr>, | ||
{ | ||
self.env | ||
.insert(key.as_ref().to_os_string(), val.as_ref().to_os_string()); | ||
self | ||
} | ||
|
||
/// Configure the child process to spawn with a piped stdin | ||
pub fn open_stdin(&mut self) -> &mut Self { | ||
self.open_stdin = true; | ||
self | ||
} | ||
|
||
/// Clears the environment variables for the child process | ||
pub fn env_clear(&mut self) -> &mut Self { | ||
chris-olszewski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.env_clear = true; | ||
self.env.clear(); | ||
self | ||
} | ||
|
||
pub fn label(&self) -> String { | ||
format!( | ||
"({}) {} {}", | ||
self.cwd | ||
.as_deref() | ||
.map(|dir| dir.as_str()) | ||
.unwrap_or_default(), | ||
self.program.to_string_lossy(), | ||
self.args.iter().map(|s| s.to_string_lossy()).join(" ") | ||
) | ||
} | ||
} | ||
|
||
impl From<Command> for tokio::process::Command { | ||
fn from(value: Command) -> Self { | ||
let Command { | ||
program, | ||
args, | ||
cwd, | ||
env, | ||
open_stdin, | ||
env_clear, | ||
} = value; | ||
|
||
let mut cmd = tokio::process::Command::new(program); | ||
if env_clear { | ||
cmd.env_clear(); | ||
} | ||
cmd.args(args) | ||
.envs(env) | ||
// We always pipe stdout/stderr to allow us to capture task output | ||
.stdout(Stdio::piped()) | ||
.stderr(Stdio::piped()) | ||
// Only open stdin if configured to do so | ||
.stdin(if open_stdin { | ||
Stdio::piped() | ||
} else { | ||
Stdio::null() | ||
}); | ||
Comment on lines
+121
to
+125
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a behavior change as we will no longer have our children inherit our stdin There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (but that matches the Go eversion) |
||
if let Some(cwd) = cwd { | ||
cmd.current_dir(cwd.as_std_path()); | ||
} | ||
cmd | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Independently capturing stderr wasn't an used outside of tests and can't be easily done with processes that are hooked up to a pseudo-terminal (possible, but involves redirecting stderr to a file and streaming that). In preparation we remove this feature.