1
use std::ffi::OsStr;
2

            
3
use async_process::Command;
4

            
5
pub mod protontricks;
6

            
7
pub trait CLI {
8
    fn exec<I, S>(args: I) -> impl std::future::Future<Output = Result<String, String>>
9
    where
10
        I: IntoIterator<Item = S>,
11
        S: AsRef<OsStr>;
12
    fn is_installed() -> impl std::future::Future<Output = bool> + Send;
13
    fn version() -> impl std::future::Future<Output = Result<String, String>> + Send;
14
}
15

            
16
6
pub async fn exec<I, S>(name: &'static str, args: I) -> Result<String, String>
17
6
where
18
6
    I: IntoIterator<Item = S>,
19
6
    S: AsRef<OsStr>,
20
6
{
21
6
    let mut command = Command::new(name);
22

            
23
6
    command.args(args);
24

            
25
6
    trace!("Running command... {:?}", command);
26

            
27
6
    match command.output().await {
28
        Ok(output) => {
29
            if output.status.success() {
30
                Ok(String::from_utf8(output.stdout).unwrap())
31
            } else {
32
                Err(String::from_utf8(output.stderr).unwrap())
33
            }
34
        }
35
6
        Err(_) => Err(format!("Failed to call {name}")),
36
    }
37
6
}