diff --git a/src/app.rs b/src/app.rs index 8e1c01d808f2e..d0dfc13d45d4a 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,6 +1,6 @@ use crate::{ cli::{handle_config_errors, Color, LogFormat, Opts, RootOpts, SubCommand}, - config, generate, graph, heartbeat, list, metrics, + completion, config, generate, graph, heartbeat, list, metrics, signal::{self, SignalTo}, topology::{self, RunningTopology}, trace, unit_test, validate, @@ -143,6 +143,8 @@ impl Application { SubCommand::Validate(v) => validate::validate(&v, color).await, #[cfg(feature = "vrl-cli")] SubCommand::Vrl(s) => vrl_cli::cmd::cmd(&s), + + SubCommand::Completion(c) => completion::cmd(&c), }; return Err(code); diff --git a/src/cli.rs b/src/cli.rs index 317396118a265..18c055caee6fc 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,4 +1,4 @@ -use crate::{config, generate, get_version, graph, list, unit_test, validate}; +use crate::{completion, config, generate, get_version, graph, list, unit_test, validate}; use std::path::PathBuf; use structopt::{clap::AppSettings, StructOpt}; @@ -205,6 +205,9 @@ pub enum SubCommand { /// Vector Remap Language CLI #[cfg(feature = "vrl-cli")] Vrl(vrl_cli::Opts), + + /// Output autocompletion scripts for the Vector CLI. + Completion(completion::Opts), } #[derive(Debug, Clone, PartialEq)] diff --git a/src/completion.rs b/src/completion.rs new file mode 100644 index 0000000000000..1c273e10c3597 --- /dev/null +++ b/src/completion.rs @@ -0,0 +1,15 @@ +use crate::cli::Opts as RootOpts; +use structopt::{clap::Shell, StructOpt}; + +#[derive(StructOpt, Debug)] +#[structopt(rename_all = "kebab-case")] +pub struct Opts { + #[structopt(name = "SHELL", possible_values = &Shell::variants())] + shell: Shell, +} + +pub fn cmd(opts: &Opts) -> exitcode::ExitCode { + RootOpts::clap().gen_completions_to("vector", opts.shell, &mut std::io::stdout()); + + exitcode::OK +} diff --git a/src/lib.rs b/src/lib.rs index af89f05c270a0..3df876d81066b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,6 +25,7 @@ extern crate vrl_cli; #[macro_use] pub mod config; pub mod cli; +pub mod completion; pub mod conditions; pub mod dns; #[cfg(feature = "docker")]