get_root_flags/
lib.rs

1use std::process::Command;
2
3use thiserror::Error;
4
5#[derive(Debug, Error)]
6pub enum Error {
7    #[error("Failed to run {cmd}")]
8    Cmd{cmd: String, source: std::io::Error},
9    #[error("'{cmd}' failed without error message")]
10    RunNoErr{cmd: String},
11    #[error("'{cmd}' failed: {err}")]
12    Run{cmd: String, err: String},
13}
14
15/// Get flags returned from `root-config` with the given argument flags
16pub fn get_root_flags(flags: &str) -> Result<Vec<String>, Error> {
17    use Error::*;
18
19    const CFG_CMD: &str = "root-config";
20
21    let output = Command::new(CFG_CMD)
22        .arg(flags)
23        .output()
24        .map_err(|source| {
25            let cmd = format!("{CFG_CMD} {flags}");
26            Cmd{cmd, source}
27        })?;
28    if !output.status.success() {
29        let cmd = format!("{CFG_CMD} {flags}");
30        if output.stderr.is_empty() {
31            return Err(RunNoErr{cmd});
32        } else {
33            let err = String::from_utf8_lossy(&output.stderr).to_string();
34            return Err(Run{cmd, err});
35        }
36    }
37    let args = String::from_utf8_lossy(&output.stdout);
38    Ok(args.split_whitespace().map(|arg| arg.to_owned()).collect())
39}