use std::fs;
use std::path::Path;
use std::process::{Command, Stdio};
use super::{Cli, CliError};
impl Cli {
pub fn spawn_detached(
&self,
cwd: Option<&Path>,
stderr: &Path,
args: &[&str],
) -> Result<u32, CliError> {
use std::os::unix::process::CommandExt;
let mut cmd = Command::new(self.binary());
cmd.args(args)
.envs(self.standing_env())
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(sink(stderr))
.process_group(0);
if let Some(dir) = cwd {
cmd.current_dir(dir);
}
let child = cmd.spawn().map_err(|e| CliError::Spawn {
binary: self.binary().to_path_buf(),
source: e,
})?;
Ok(child.id())
}
}
fn sink(path: &Path) -> Stdio {
let reachable = path.parent().is_none_or(|p| fs::create_dir_all(p).is_ok());
match reachable
.then(|| fs::File::create(path))
.and_then(Result::ok)
{
Some(file) => Stdio::from(file),
None => Stdio::null(),
}
}