1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (C) 2023 Andreas Hartmann <hartan@7x.de>
// GNU General Public License v3.0+ (https://www.gnu.org/licenses/gpl-3.0.txt)
// SPDX-License-Identifier: GPL-3.0-or-later

//! # Host Environment Handler.
//!
//! This module handles command-not-found errors that occur while executing on the host OS.
//! Currently this means that the command is forwarded to a Toolbx container of choice, preferrably
//! one specified in the configuration file or the default one otherwise.
use super::prelude::*;

#[derive(PartialEq, Eq, Debug, PartialOrd, Ord, Serialize, Deserialize)]
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()
    }
}

impl environment::IsEnvironment for Host {
    type Err = std::convert::Infallible;

    fn exists(&self) -> bool {
        true
    }

    fn execute(&self, command: CommandLine) -> Result<Command, Self::Err> {
        log::debug!("Executing command '{}' on host", 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());
                Ok(cmd)
            }
            Environment::Toolbx(_) | Environment::Distrobox(_) => {
                // flatpak-spawn --host ARGS
                cmd = Command::new("flatpak-spawn");
                cmd.arg("--host");

                // Preserve environment
                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());

                log::debug!("Calling: {:?}", cmd);
                Ok(cmd)
            }
            #[cfg(test)]
            Environment::Mock(_) => {
                unimplemented!()
            }
        }
    }
}