Skip to main content

cnf_lib/environment/
host.rs

1// SPDX-License-Identifier: GPL-3.0-or-later
2// SPDX-FileCopyrightText: (C) 2023 Andreas Hartmann <hartan@7x.de>
3// This file is part of cnf-lib, available at <https://gitlab.com/hartang/rust/cnf>
4
5//! # Host Environment Handler
6//!
7//! This module handles command-not-found errors that occur while executing on the host OS.
8//! Currently this means that the command is forwarded to a Toolbx container of choice, preferrably
9//! one specified in the configuration file or the default one otherwise.
10use super::prelude::*;
11
12/// Environment of the native host operating system.
13#[derive(PartialEq, Eq, Debug, PartialOrd, Ord, Serialize, Deserialize)]
14// In the future these may get (mutable) internal state.
15#[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    /// Create a new instance.
26    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                // flatpak-spawn --host ARGS
71                cmd = Command::new("flatpak-spawn");
72                cmd.arg("--host");
73
74                // Preserve environment
75                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}