vdev/commands/
mod.rs

1use clap::Parser;
2use clap_verbosity_flag::{InfoLevel, Verbosity};
3
4mod compose_tests;
5
6/// This macro simplifies the generation of CLI subcommand invocation structures by combining the
7/// creation of the command enum and implementation of the dispatch function into one simple list.
8// Module declaration in here was removed due to https://github.com/rust-lang/rustfmt/issues/3253
9#[macro_export]
10macro_rules! cli_commands {
11    ( :: $( $list:ident, )* :: $mod:ident, $( $rest:tt )* ) => {
12        $crate::cli_commands! { :: $( $list, )* $mod, :: $( $rest )* }
13    };
14    // All the identifiers are parsed out, build up the enum and impl blocks
15    ( :: $( $mod:ident, )* :: ) => {
16        pastey::paste! {
17            #[derive(clap::Subcommand, Debug)]
18            enum Commands {
19                $( [<$mod:camel>]($mod::Cli), )*
20            }
21
22            impl Cli {
23                pub fn exec(self) -> anyhow::Result<()> {
24                    match self.command {
25                        $( Commands::[<$mod:camel>](cli) => cli.exec(), )*
26                    }
27                }
28            }
29        }
30    };
31    // Start the above patterns
32    ( $( $rest:tt )+ ) => { $crate::cli_commands! { :: :: $( $rest )+ } };
33}
34
35#[macro_export]
36macro_rules! cli_subcommands {
37    ( $doc:literal $( $rest:tt )* ) => {
38        #[derive(clap::Args, Debug)]
39        #[doc = $doc]
40        #[command()]
41        pub(super) struct Cli {
42            #[command(subcommand)]
43            command: Commands,
44        }
45
46        $crate::cli_commands! { $( $rest )* }
47    }
48}
49
50/// Vector's unified dev tool
51#[derive(Parser, Debug)]
52#[command(
53    version,
54    bin_name = "vdev",
55    infer_subcommands = true,
56    disable_help_subcommand = true,
57    after_help = r#"Environment variables:
58  $CONTAINER_TOOL  Set the tool used to run containers (Defaults to autodetect)
59                   Valid values are either "docker" or "podman".
60"#
61)]
62pub struct Cli {
63    #[clap(flatten)]
64    pub verbose: Verbosity<InfoLevel>,
65
66    #[command(subcommand)]
67    command: Commands,
68}
69
70mod build;
71mod check;
72mod complete;
73mod crate_versions;
74mod e2e;
75mod features;
76mod fmt;
77mod info;
78mod integration;
79mod meta;
80mod package;
81mod release;
82mod run;
83mod status;
84mod test;
85mod test_vrl;
86mod version;
87
88cli_commands! {
89    build,
90    check,
91    complete,
92    crate_versions,
93    e2e,
94    features,
95    fmt,
96    info,
97    integration,
98    meta,
99    package,
100    release,
101    run,
102    status,
103    test,
104    test_vrl,
105    version,
106}
107
108/// This macro creates a wrapper for an existing script.
109#[macro_export]
110macro_rules! script_wrapper {
111    ( $mod:ident = $doc:literal => $script:literal ) => {
112        pastey::paste! {
113            mod $mod {
114                #[doc = $doc]
115                #[derive(clap::Args, Debug)]
116                #[command()]
117                pub(super) struct Cli {
118                    args: Vec<String>,
119                }
120
121                impl Cli {
122                    pub(super) fn exec(self) -> anyhow::Result<()> {
123                        $crate::app::exec(concat!("scripts/", $script), self.args, true)
124                    }
125                }
126            }
127        }
128    };
129}