Skip to content

Commit b4efdf7

Browse files
committed
enhancement(cli): Shell autocompletion for vector cli
This adds support for generating autocompletion for different shells for the vector cli. Superseds [#9823](#9823)
1 parent 3749b70 commit b4efdf7

File tree

6 files changed

+35
-3
lines changed

6 files changed

+35
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ cfg-if = { version = "1.0.3", default-features = false }
145145
chrono = { version = "0.4.41", default-features = false, features = ["clock", "serde"] }
146146
chrono-tz = { version = "0.10.4", default-features = false, features = ["serde"] }
147147
clap = { version = "4.5.48", default-features = false, features = ["derive", "error-context", "env", "help", "std", "string", "usage", "wrap_help"] }
148+
clap_complete = "4.5.58"
148149
colored = { version = "3.0.0", default-features = false }
149150
crossbeam-utils = { version = "0.8.21", default-features = false }
150151
darling = { version = "0.20.11", default-features = false, features = ["suggestions"] }
@@ -208,6 +209,7 @@ serial_test = { version = "3.2" }
208209
[dependencies]
209210
cfg-if.workspace = true
210211
clap.workspace = true
212+
clap_complete.workspace = true
211213
indoc.workspace = true
212214
paste.workspace = true
213215
pin-project.workspace = true

src/cli.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use crate::tap;
1212
use crate::top;
1313

1414
use crate::{
15-
config, convert_config, generate, generate_schema, get_version, graph, list, signal, unit_test,
16-
validate,
15+
completion, config, convert_config, generate, generate_schema, get_version, graph, list,
16+
signal, unit_test, validate,
1717
};
1818

1919
#[derive(Parser, Debug)]
@@ -318,6 +318,10 @@ pub enum SubCommand {
318318
/// By default all output is writen to stdout. The `output_path` option can be used to redirect to a file.
319319
GenerateSchema(generate_schema::Opts),
320320

321+
/// Generate shell completion, then exit.
322+
#[command(hide = true)]
323+
Completion(completion::Opts),
324+
321325
/// Output a provided Vector configuration file/dir as a single JSON object, useful for checking in to version control.
322326
#[command(hide = true)]
323327
Config(config::Opts),
@@ -355,6 +359,7 @@ impl SubCommand {
355359
color: bool,
356360
) -> exitcode::ExitCode {
357361
match self {
362+
Self::Completion(s) => completion::cmd(s),
358363
Self::Config(c) => config::cmd(c),
359364
Self::ConvertConfig(opts) => convert_config::cmd(opts),
360365
Self::Generate(g) => generate::cmd(g),

src/completion.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![allow(missing_docs)]
2+
use clap::{CommandFactory, Parser};
3+
use clap_complete::{Shell, generate};
4+
use std::io;
5+
6+
use crate::cli::Opts as RootCli;
7+
8+
#[derive(Parser, Debug, Clone)]
9+
#[command(rename_all = "kebab-case")]
10+
pub struct Opts {
11+
/// Shell to generate completion for
12+
#[clap(value_enum)]
13+
shell: Shell,
14+
}
15+
16+
pub fn cmd(opts: &Opts) -> exitcode::ExitCode {
17+
let mut cmd = RootCli::command();
18+
let bin_name = cmd.get_name().to_string();
19+
20+
generate(opts.shell, &mut cmd, bin_name, &mut io::stdout());
21+
22+
exitcode::OK
23+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ pub mod aws;
7575
#[allow(unreachable_pub)]
7676
pub mod codecs;
7777
pub mod common;
78+
pub mod completion;
7879
mod convert_config;
7980
pub mod encoding_transcode;
8081
pub mod enrichment_tables;

vdev/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ anyhow.workspace = true
2121
chrono.workspace = true
2222
clap.workspace = true
2323
clap-verbosity-flag = "3.0.4"
24-
clap_complete = "4.5.58"
24+
clap_complete.workspace = true
2525
directories = "6.0.0"
2626
glob.workspace = true
2727
hex = "0.4.3"

0 commit comments

Comments
 (0)