Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement(config): Allow wildcards anywhere in identifiers #7873

Merged
merged 5 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions docs/reference/configuration.cue
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,9 @@ configuration: {
wildcards: {
title: "Wildcards in identifiers"
body: """
Vector supports wildcards (`*`) in component identifiers when building your topology, but only supports
them as the last character. For example:
Vector supports wildcard characters (`*`) in component identifiers when building your topology.

For example:

```toml
[sources.app1_logs]
Expand All @@ -238,7 +239,7 @@ configuration: {

[sinks.archive]
type = "aws_s3"
inputs = ["app*", "system_logs"]
inputs = ["*_logs"]
```
"""
}
Expand Down
33 changes: 19 additions & 14 deletions src/config/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use indexmap::IndexMap;
pub fn compile(mut builder: ConfigBuilder) -> Result<(Config, Vec<String>), Vec<String>> {
let mut errors = Vec::new();

expand_wildcards(&mut builder);
expand_globs(&mut builder);

let expansions = expand_macros(&mut builder)?;

Expand Down Expand Up @@ -85,8 +85,8 @@ pub(super) fn expand_macros(
}
}

/// Expand trailing `*` wildcards in input lists
fn expand_wildcards(config: &mut ConfigBuilder) {
/// Expand globs in input lists
fn expand_globs(config: &mut ConfigBuilder) {
let candidates = config
.sources
.keys()
Expand All @@ -95,26 +95,29 @@ fn expand_wildcards(config: &mut ConfigBuilder) {
.collect::<Vec<String>>();

for (name, transform) in config.transforms.iter_mut() {
expand_wildcards_inner(&mut transform.inputs, name, &candidates);
expand_globs_inner(&mut transform.inputs, name, &candidates);
}

for (name, sink) in config.sinks.iter_mut() {
expand_wildcards_inner(&mut sink.inputs, name, &candidates);
expand_globs_inner(&mut sink.inputs, name, &candidates);
}
}

fn expand_wildcards_inner(inputs: &mut Vec<String>, name: &str, candidates: &[String]) {
fn expand_globs_inner(inputs: &mut Vec<String>, name: &str, candidates: &[String]) {
let raw_inputs = std::mem::take(inputs);
for raw_input in raw_inputs {
if raw_input.ends_with('*') {
let prefix = &raw_input[0..raw_input.len() - 1];
for input in candidates {
if input.starts_with(prefix) && input != name {
inputs.push(input.clone())
match glob::Pattern::new(&raw_input) {
Ok(pattern) => {
for input in candidates {
if pattern.matches(input) && input != name {
inputs.push(input.clone())
}
}
}
} else {
inputs.push(raw_input);
Err(error) => {
error!(message = "Invalid glob pattern for input.", component_name = name, %error);
continue;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this error, or just do an exact match?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that's true, maybe an exact match would be acceptable. Although the user may be confused if they were trying to use a pattern that ended up being invalid.

Maybe we could emit a warning and then do an exact match?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds best, yes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 updated in 5344183

}
}
}
Expand Down Expand Up @@ -196,13 +199,14 @@ mod test {
}

#[test]
fn wildcard_expansion() {
fn glob_expansion() {
let mut builder = ConfigBuilder::default();
builder.add_source("foo1", MockSourceConfig);
builder.add_source("foo2", MockSourceConfig);
builder.add_source("bar", MockSourceConfig);
builder.add_transform("foos", &["foo*"], MockTransformConfig);
builder.add_sink("baz", &["foos*", "b*"], MockSinkConfig);
builder.add_sink("quix", &["*oo*"], MockSinkConfig);
builder.add_sink("quux", &["*"], MockSinkConfig);

let config = builder.build().expect("build should succeed");
Expand All @@ -213,5 +217,6 @@ mod test {
config.sinks["quux"].inputs,
vec!["foo1", "foo2", "bar", "foos"]
);
assert_eq!(config.sinks["quix"].inputs, vec!["foo1", "foo2", "foos"]);
}
}