cnf_lib/environment/
host.rs1use super::prelude::*;
11
12#[derive(PartialEq, Eq, Debug, PartialOrd, Ord, Serialize, Deserialize)]
14#[allow(missing_copy_implementations)]
16pub struct Host {}
17
18impl fmt::Display for Host {
19 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20 write!(f, "host")
21 }
22}
23
24impl Host {
25 pub fn new() -> Host {
27 Host {}
28 }
29}
30
31impl std::default::Default for Host {
32 fn default() -> Self {
33 Self::new()
34 }
35}
36
37#[async_trait]
38impl environment::IsEnvironment for Host {
39 type Err = std::convert::Infallible;
40
41 async fn exists(&self) -> bool {
42 true
43 }
44
45 async fn execute(&self, command: CommandLine) -> Result<Command, Self::Err> {
46 debug!("preparing execution: {}", command);
47 let mut cmd: Command;
48
49 match environment::current() {
50 Environment::Host(_) => {
51 if command.get_privileged() {
52 if cfg!(unix) {
53 cmd = Command::new("sudo");
54 cmd.arg("-S");
55 if !command.get_interactive() {
56 cmd.arg("-n");
57 }
58 } else {
59 unimplemented!("cannot escalate privileges yet for platforms not Unix");
60 }
61
62 cmd.arg(command.command());
63 } else {
64 cmd = Command::new(command.command());
65 }
66
67 cmd.args(command.args());
68 }
69 Environment::Toolbx(_) | Environment::Distrobox(_) => {
70 cmd = Command::new("flatpak-spawn");
72 cmd.arg("--host");
73
74 for env in environment::read_env_vars() {
76 cmd.arg(format!("--env={}", env));
77 }
78
79 if command.get_privileged() {
80 if cfg!(unix) {
81 cmd.args(["sudo", "-S", "-E"]);
82 if !command.get_interactive() {
83 cmd.arg("-n");
84 }
85 } else {
86 unimplemented!("cannot escalate privileges yet for platforms not Unix");
87 }
88 }
89
90 cmd.arg(command.command());
91 cmd.args(command.args());
92 }
93 #[cfg(test)]
94 Environment::Mock(_) => {
95 unimplemented!()
96 }
97 }
98
99 trace!("full command: {:?}", cmd);
100 Ok(cmd)
101 }
102}