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
use std::{path::PathBuf, process::Command};

use anyhow::{bail, Result};
use clap::Args;

use crate::{app::CommandExt as _, features};

/// Run `vector` with the minimum set of features required by the config file
#[derive(Args, Debug)]
#[command()]
pub struct Cli {
    /// Build and run `vector` in debug mode (default)
    #[arg(long, default_value_t = true)]
    debug: bool,

    /// Build and run `vector` in release mode
    #[arg(long)]
    release: bool,

    /// Name an additional feature to add to the build
    #[arg(short = 'F', long)]
    feature: Vec<String>,

    /// Path to configuration file
    config: PathBuf,

    /// Non-config arguments to `vector`
    args: Vec<String>,
}

impl Cli {
    pub(super) fn exec(self) -> Result<()> {
        if self.debug && self.release {
            bail!("Can only set one of `--debug` and `--release`");
        }

        let mut features = features::load_and_extract(&self.config)?;
        features.extend(self.feature);
        let features = features.join(",");
        let mut command = Command::new("cargo");
        command.args(["run", "--no-default-features", "--features", &features]);
        if self.release {
            command.arg("--release");
        }
        command.args([
            "--",
            "--config",
            self.config.to_str().expect("Invalid config file name"),
        ]);
        command.args(self.args);
        command.check_run()
    }
}