use super::prelude::*;
#[derive(PartialEq, Eq, Debug, PartialOrd, Ord, Serialize, Deserialize)]
#[allow(missing_copy_implementations)]
pub struct Host {}
impl fmt::Display for Host {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "host")
}
}
impl Host {
pub fn new() -> Host {
Host {}
}
}
impl std::default::Default for Host {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl environment::IsEnvironment for Host {
type Err = std::convert::Infallible;
async fn exists(&self) -> bool {
true
}
async fn execute(&self, command: CommandLine) -> Result<Command, Self::Err> {
debug!("preparing execution: {}", command);
let mut cmd: Command;
match environment::current() {
Environment::Host(_) => {
if command.get_privileged() {
if cfg!(unix) {
cmd = Command::new("sudo");
cmd.arg("-S");
if !command.get_interactive() {
cmd.arg("-n");
}
} else {
unimplemented!("cannot escalate privileges yet for platforms not Unix");
}
cmd.arg(command.command());
} else {
cmd = Command::new(command.command());
}
cmd.args(command.args());
}
Environment::Toolbx(_) | Environment::Distrobox(_) => {
cmd = Command::new("flatpak-spawn");
cmd.arg("--host");
for env in environment::read_env_vars() {
cmd.arg(format!("--env={}", env));
}
if command.get_privileged() {
if cfg!(unix) {
cmd.args(["sudo", "-S", "-E"]);
if !command.get_interactive() {
cmd.arg("-n");
}
} else {
unimplemented!("cannot escalate privileges yet for platforms not Unix");
}
}
cmd.arg(command.command());
cmd.args(command.args());
}
#[cfg(test)]
Environment::Mock(_) => {
unimplemented!()
}
}
trace!("full command: {:?}", cmd);
Ok(cmd)
}
}