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
use clap::{ArgMatches, Command};
use color_eyre::eyre::Result;
use itertools::Itertools;
use rayon::prelude::*;

use crate::config::Config;

pub fn commands(config: &Config) -> Vec<Command> {
    config
        .tools
        .values()
        .collect_vec()
        .into_par_iter()
        .flat_map(|p| match p.external_commands() {
            Ok(commands) => commands,
            Err(e) => {
                warn!(
                    "failed to load external commands for plugin {}: {:#}",
                    p.name, e
                );
                vec![]
            }
        })
        .collect()
}

pub fn execute(
    config: &Config,
    plugin: &str,
    args: &ArgMatches,
    external_commands: Vec<Command>,
) -> Result<()> {
    if let Some(_cmd) = external_commands.iter().find(|c| c.get_name() == plugin) {
        if let Some((subcommand, matches)) = args.subcommand() {
            let plugin = config.tools.get(&plugin.to_string()).unwrap();
            let args: Vec<String> = matches
                .get_raw("args")
                .unwrap_or_default()
                .map(|s| s.to_string_lossy().to_string())
                .collect();
            plugin.execute_external_command(config, subcommand, args)?;
        }
    }

    Ok(())
}