diff --git a/changelog.d/9766_data_dir_cli_env.feature.md b/changelog.d/9766_data_dir_cli_env.feature.md new file mode 100644 index 0000000000000..e9cabc57a18f5 --- /dev/null +++ b/changelog.d/9766_data_dir_cli_env.feature.md @@ -0,0 +1,3 @@ +Added a `--data-dir` command-line flag (and corresponding `VECTOR_DATA_DIR` environment variable) to override the `data_dir` global configuration option. When set, it takes precedence over any `data_dir` value in the configuration file. This is useful for keeping deployment-specific paths out of the configuration file, for example when validating a configuration in a CI environment where the configured `data_dir` may not exist. + +authors: xfocus3 diff --git a/src/app.rs b/src/app.rs index 580c06642a6e3..ba14d158d17c5 100644 --- a/src/app.rs +++ b/src/app.rs @@ -85,6 +85,7 @@ impl ApplicationConfig { &config_paths, watcher_conf, opts.require_healthy, + opts.data_dir.clone(), opts.allow_empty_config, !opts.disable_env_var_interpolation, graceful_shutdown_duration, @@ -555,10 +556,12 @@ pub fn build_runtime(threads: Option, thread_name: &str) -> Result, require_healthy: Option, + data_dir: Option, allow_empty_config: bool, interpolate_env: bool, graceful_shutdown_duration: Option, @@ -648,6 +651,13 @@ pub async fn load_configs( info!("Health checks are disabled."); } config.healthchecks.set_require_healthy(require_healthy); + if let Some(data_dir) = data_dir { + debug!( + message = "Overriding data_dir from command line.", + ?data_dir + ); + config.global.data_dir = Some(data_dir); + } config.graceful_shutdown_duration = graceful_shutdown_duration; Ok(config) diff --git a/src/cli.rs b/src/cli.rs index 2282d22f3689c..5955376c57fa3 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -125,6 +125,15 @@ pub struct RootOpts { #[arg(short, long, env = "VECTOR_REQUIRE_HEALTHY")] pub require_healthy: Option, + /// The directory used for persisting Vector state data. + /// + /// This overrides the `data_dir` global option set in the configuration file. It is + /// useful for keeping deployment-specific paths out of the configuration file, for + /// example when validating a configuration in a CI environment where the configured + /// `data_dir` may not exist. + #[arg(long, env = "VECTOR_DATA_DIR")] + pub data_dir: Option, + /// Number of threads to use for processing (default is number of available cores) #[arg(short, long, env = "VECTOR_THREADS")] pub threads: Option, @@ -424,3 +433,24 @@ pub fn handle_config_errors(errors: Vec) -> exitcode::ExitCode { exitcode::CONFIG } + +#[cfg(test)] +mod tests { + use std::path::PathBuf; + + use clap::Parser; + + use super::RootOpts; + + #[test] + fn data_dir_defaults_to_none() { + let opts = RootOpts::try_parse_from(["vector"]).unwrap(); + assert_eq!(opts.data_dir, None); + } + + #[test] + fn data_dir_parsed_from_flag() { + let opts = RootOpts::try_parse_from(["vector", "--data-dir", "/tmp/vector"]).unwrap(); + assert_eq!(opts.data_dir, Some(PathBuf::from("/tmp/vector"))); + } +}