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::process::Command;

use crate::{
    args::{EnvArgs, WorkspaceArgs},
    config::Config,
    process::CommandExt,
    Result, Run,
};

/// Arguments definition of the `exec` subcommand.
#[cfg_attr(doc, doc = include_str!("../../doc/cargo-xtask-exec.md"))]
#[derive(Debug, Clone, Default, clap::Args)]
#[non_exhaustive]
pub struct Exec {
    /// Environment variables to set for the command.
    #[clap(flatten)]
    pub env_args: EnvArgs,
    /// Workspaces where the the command runs on.
    #[clap(flatten)]
    pub workspace_args: WorkspaceArgs,
    /// Command to execute
    pub command: String,
    /// Arguments to pass to the command
    pub command_options: Vec<String>,
}

impl Run for Exec {
    fn run(&self, config: &Config) -> Result<()> {
        self.run(config)
    }
}

impl Exec {
    /// Runs the `exec` subcommand.
    #[tracing::instrument(name = "exec", skip_all, err)]
    pub fn run(&self, _config: &Config) -> Result<()> {
        let Self {
            env_args,
            workspace_args,
            command,
            command_options,
        } = self;

        for workspace in workspace_args.workspaces() {
            Command::new(command)
                .args(command_options)
                .envs(env_args.env.clone())
                .workspace_spawn(workspace)?;
        }

        Ok(())
    }
}