(mut cmd: Command, stdin: Option<String>)
| 14 | const CREATE_NO_WINDOW: u32 = 0x08000000; |
| 15 | |
| 16 | pub fn exec(mut cmd: Command, stdin: Option<String>) -> Result<String> { |
| 17 | #[cfg(windows)] |
| 18 | cmd.creation_flags(CREATE_NO_WINDOW); |
| 19 | |
| 20 | log::debug!("command: {:?}", cmd); |
| 21 | let out = if let Some(stdin) = stdin { |
| 22 | for line in stdin.lines() { |
| 23 | log::trace!("stdin: {line:?}"); |
| 24 | } |
| 25 | let mut child = cmd |
| 26 | .stdin(Stdio::piped()) |
| 27 | .stdout(Stdio::piped()) |
| 28 | .spawn() |
| 29 | .with_context(|| format!("failed to run command {cmd:?}"))?; |
| 30 | child |
| 31 | .stdin |
| 32 | .as_mut() |
| 33 | .with_context(|| format!("failed to get stdin handle to command {cmd:?}"))? |
| 34 | .write_all(stdin.as_bytes()) |
| 35 | .with_context(|| format!("failed to write stdin to command {cmd:?}"))?; |
| 36 | child |
| 37 | .wait_with_output() |
| 38 | .with_context(|| format!("failed to wait command {cmd:?}"))? |
| 39 | } else { |
| 40 | cmd.output() |
| 41 | .with_context(|| format!("failed to read stdout from command {cmd:?}"))? |
| 42 | }; |
| 43 | let stderr = String::from_utf8(out.stderr) |
| 44 | .with_context(|| format!("failed to encoding stderr by UTF-8"))?; |
| 45 | for line in stderr.lines() { |
| 46 | log::debug!("stderr: {line:?}"); |
| 47 | } |
| 48 | let stdout = String::from_utf8(out.stdout) |
| 49 | .with_context(|| format!("failed to encoding stdout by UTF-8"))?; |
| 50 | for line in stdout.lines() { |
| 51 | log::trace!("stdout: {line:?}"); |
| 52 | } |
| 53 | anyhow::ensure!( |
| 54 | out.status.success(), |
| 55 | "command {cmd:?} failed: {}", |
| 56 | stderr.trim() |
| 57 | ); |
| 58 | Ok(stdout) |
| 59 | } |
| 60 | |
| 61 | pub fn git_cmd( |
| 62 | git_dir: impl AsRef<OsStr>, |
no outgoing calls
no test coverage detected