#[cfg(not(feature = "tokio"))]
use async_process::{unix::CommandExt, Child};
#[cfg(target_os = "macos")]
use std::process::Output;
use std::{ffi::OsStr, io::Error, process::Stdio};
#[cfg(feature = "tokio")]
use tokio::process::Child;
use crate::address::transport::Unixexec;
pub struct Command(
#[cfg(not(feature = "tokio"))] async_process::Command,
#[cfg(feature = "tokio")] tokio::process::Command,
);
impl Command {
pub fn new<S>(program: S) -> Self
where
S: AsRef<OsStr>,
{
#[cfg(not(feature = "tokio"))]
return Self(async_process::Command::new(program));
#[cfg(feature = "tokio")]
return Self(tokio::process::Command::new(program));
}
pub fn for_unixexec(unixexec: &Unixexec) -> Self {
let mut command = Self::new(unixexec.path());
command.args(unixexec.args());
if let Some(arg0) = unixexec.arg0() {
command.arg0(arg0);
}
command
}
pub fn arg0<S>(&mut self, arg: S) -> &mut Self
where
S: AsRef<OsStr>,
{
self.0.arg0(arg);
self
}
pub fn args<I, S>(&mut self, args: I) -> &mut Self
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
self.0.args(args);
self
}
#[cfg(target_os = "macos")]
pub async fn output(&mut self) -> Result<Output, Error> {
self.0.output().await
}
pub fn stdin<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self {
self.0.stdin(cfg);
self
}
pub fn stdout<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self {
self.0.stdout(cfg);
self
}
pub fn stderr<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self {
self.0.stderr(cfg);
self
}
pub fn spawn(&mut self) -> Result<Child, Error> {
self.0.spawn()
}
}
#[cfg(target_os = "macos")]
pub async fn run<I, S>(program: S, args: I) -> Result<Output, Error>
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
Command::new(program).args(args).output().await
}