|
| 1 | +use crate::{ |
| 2 | + Backend, ChildProcess, ChildProcessOptions, DominionRef, InputSpecification, |
| 3 | + OutputSpecification, StdioSpecification, |
| 4 | +}; |
| 5 | +use std::{ |
| 6 | + collections::HashMap, |
| 7 | + ffi::{OsStr, OsString}, |
| 8 | + path::{Path, PathBuf}, |
| 9 | +}; |
| 10 | + |
| 11 | +/// Child process builder |
| 12 | +#[derive(Default)] |
| 13 | +pub struct Command { |
| 14 | + dominion: Option<DominionRef>, |
| 15 | + exe: Option<PathBuf>, |
| 16 | + argv: Vec<OsString>, |
| 17 | + env: HashMap<OsString, OsString>, |
| 18 | + stdin: Option<InputSpecification>, |
| 19 | + stdout: Option<OutputSpecification>, |
| 20 | + stderr: Option<OutputSpecification>, |
| 21 | + current_dir: Option<PathBuf>, |
| 22 | +} |
| 23 | + |
| 24 | +impl Command { |
| 25 | + pub fn build(&self) -> Option<ChildProcessOptions> { |
| 26 | + let create_default_in_channel = || InputSpecification::empty(); |
| 27 | + let create_default_out_channel = || OutputSpecification::ignore(); |
| 28 | + let opts = ChildProcessOptions { |
| 29 | + path: self.exe.clone()?, |
| 30 | + arguments: self.argv.clone(), |
| 31 | + environment: self.env.clone(), |
| 32 | + dominion: self.dominion.clone()?, |
| 33 | + stdio: StdioSpecification { |
| 34 | + stdin: self.stdin.clone().unwrap_or_else(create_default_in_channel), |
| 35 | + stdout: self |
| 36 | + .stdout |
| 37 | + .clone() |
| 38 | + .unwrap_or_else(create_default_out_channel), |
| 39 | + stderr: self |
| 40 | + .stderr |
| 41 | + .clone() |
| 42 | + .unwrap_or_else(create_default_out_channel), |
| 43 | + }, |
| 44 | + pwd: self.current_dir.clone().unwrap_or_else(|| "/".into()), |
| 45 | + }; |
| 46 | + Some(opts) |
| 47 | + } |
| 48 | + |
| 49 | + pub fn new() -> Command { |
| 50 | + Default::default() |
| 51 | + } |
| 52 | + |
| 53 | + pub fn spawn(&self, backend: &dyn Backend) -> crate::Result<Box<dyn ChildProcess>> { |
| 54 | + let options = self |
| 55 | + .build() |
| 56 | + .expect("spawn() was requested, but required fields were not set"); |
| 57 | + backend.spawn(options) |
| 58 | + } |
| 59 | + |
| 60 | + pub fn dominion(&mut self, dominion: DominionRef) -> &mut Self { |
| 61 | + self.dominion.replace(dominion); |
| 62 | + self |
| 63 | + } |
| 64 | + |
| 65 | + pub fn path<S: AsRef<Path>>(&mut self, path: S) -> &mut Self { |
| 66 | + self.exe.replace(path.as_ref().to_path_buf()); |
| 67 | + self |
| 68 | + } |
| 69 | + |
| 70 | + pub fn arg<S: AsRef<OsStr>>(&mut self, a: S) -> &mut Self { |
| 71 | + self.argv.push(a.as_ref().to_os_string()); |
| 72 | + self |
| 73 | + } |
| 74 | + |
| 75 | + pub fn args(&mut self, args: impl IntoIterator<Item = impl AsRef<OsStr>>) -> &mut Self { |
| 76 | + self.argv |
| 77 | + .extend(args.into_iter().map(|s| s.as_ref().to_os_string())); |
| 78 | + self |
| 79 | + } |
| 80 | + |
| 81 | + pub fn env(&mut self, key: impl AsRef<OsStr>, value: impl AsRef<OsStr>) -> &mut Self { |
| 82 | + self.env |
| 83 | + .insert(key.as_ref().to_os_string(), value.as_ref().to_os_string()); |
| 84 | + self |
| 85 | + } |
| 86 | + |
| 87 | + pub fn envs( |
| 88 | + &mut self, |
| 89 | + items: impl IntoIterator<Item = (impl AsRef<OsStr>, impl AsRef<OsStr>)>, |
| 90 | + ) -> &mut Self { |
| 91 | + self.env.extend( |
| 92 | + items |
| 93 | + .into_iter() |
| 94 | + .map(|(k, v)| (k.as_ref().to_os_string(), v.as_ref().to_os_string())), |
| 95 | + ); |
| 96 | + self |
| 97 | + } |
| 98 | + |
| 99 | + pub fn current_dir<S: AsRef<Path>>(&mut self, a: S) -> &mut Self { |
| 100 | + self.current_dir.replace(a.as_ref().to_path_buf()); |
| 101 | + self |
| 102 | + } |
| 103 | + |
| 104 | + pub fn stdin(&mut self, stdin: InputSpecification) -> &mut Self { |
| 105 | + self.stdin.replace(stdin); |
| 106 | + self |
| 107 | + } |
| 108 | + |
| 109 | + pub fn stdout(&mut self, stdout: OutputSpecification) -> &mut Self { |
| 110 | + self.stdout.replace(stdout); |
| 111 | + self |
| 112 | + } |
| 113 | + |
| 114 | + pub fn stderr(&mut self, stderr: OutputSpecification) -> &mut Self { |
| 115 | + self.stderr.replace(stderr); |
| 116 | + self |
| 117 | + } |
| 118 | +} |
0 commit comments