1use std::{
2 ffi::{OsStr, OsString},
3 os::unix::process::CommandExt,
4 path::Path,
5 process::{Child, Command, Stdio},
6};
7
8use crate::error::{Error, Result};
9
10pub(crate) fn exec_command(cmd: &str, args: Vec<OsString>, dir: impl AsRef<Path>) -> Error {
11 Error::Io {
12 inner: Command::new(cmd).current_dir(dir).args(args).exec(),
13 }
14}
15
16pub(crate) fn run_foreground_command(
17 cmd: &str,
18 args: Vec<impl AsRef<OsStr>>,
19 dir: impl AsRef<Path>,
20) -> Result<()> {
21 let status = run_background_command(cmd, args, dir)?.wait()?;
22
23 if !status.success() {
24 return Err(Error::FailedSubprocess {
25 command: cmd.to_string(),
26 exit_code: status.code(),
27 });
28 }
29
30 Ok(())
31}
32
33pub(crate) fn run_background_command(
34 cmd: &str,
35 args: Vec<impl AsRef<OsStr>>,
36 dir: impl AsRef<Path>,
37) -> Result<Child> {
38 let child = Command::new(cmd)
39 .current_dir(dir)
40 .args(args)
41 .stdin(Stdio::null())
42 .stdout(Stdio::null())
43 .spawn()?;
44
45 Ok(child)
46}