diff --git a/docs/ENVIRONMENT_VARIABLES.md b/docs/ENVIRONMENT_VARIABLES.md index 0e12bee4a..cf8c54f83 100644 --- a/docs/ENVIRONMENT_VARIABLES.md +++ b/docs/ENVIRONMENT_VARIABLES.md @@ -2,6 +2,7 @@ | Variable | Description | | --- | --- | +| `QSV_DOTENV_PATH` | The filename of the dotenv file to load, OVERRIDING existing environment variables. This takes precedence over any other dotenv file in the filesystem. | | `QSV_DEFAULT_DELIMITER` | single ascii character to use as delimiter. Overrides `--delimiter` option. Defaults to "," (comma) for CSV files & "\t" (tab) for TSV files when not set. Note that this will also set the delimiter for qsv's output to stdout.
However, using the `--output` option, regardless of this environment variable, will automatically change the delimiter used in the generated file based on the file extension - i.e. comma for `.csv`, tab for `.tsv` & `.tab` files. | | `QSV_SNIFF_DELIMITER` | if set, the delimiter is automatically detected. Overrides `QSV_DEFAULT_DELIMITER` & `--delimiter` option. Note that this does not work with stdin. | | `QSV_NO_HEADERS` | if set, the first row will **NOT** be interpreted as headers. Supersedes `QSV_TOGGLE_HEADERS`. | @@ -54,7 +55,8 @@ qsv supports the use of `.env` files to set environment variables. The `.env` fi It processes `.env` files as follows: -* Upon invocation, qsv will look for a file named `.env` in the current working directory. If one is found, it will be processed. +* Upon invocation, qsv will check if the `QSV_DOTENV_PATH` environment variable is set. If it is, it will look for the file specified by the variable. If the file is found, it will be processed. +* If the `QSV_DOTENV_PATH` environment variable is not set, qsv will look for a file named `.env` in the current working directory. If one is found, it will be processed. * If no `.env` file is not found in the current working directory, qsv will next look for an `.env` file with the same filestem as the binary in the directory where the binary is (e.g. if `qsv`/`qsvlite`/`qsvdp` is in `/usr/local/bin`, it will look for `/usr/local/bin/qsv.env`, `/usr/local/bin/qsvlite.env` or `/usr/local/bin/qsvdp.env` respectively). * If no `.env` files are found, qsv will proceed with its default settings and the current environment variables, which may include "QSV_" variables. diff --git a/src/util.rs b/src/util.rs index c3e043331..925dd6fbc 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1216,12 +1216,24 @@ pub fn transform(bs: &[u8], casei: bool) -> ByteString { } pub fn load_dotenv() -> CliResult<()> { - // Use the default .env file in the current directory. + // First, check if there is a QSV_DOTENV_PATH environment variable set + // if there is, use that as the .env file. + // Second, use the default .env file in the current directory. // If there is no .env file in the current directory, check if there is // an .env file with the same filestem as the binary, in the same directory as the binary. // If there is, use that. Failing that, qsv proceeds with its default settings and // whatever manually set environment variables are present. + if let Ok(dotenv_path) = std::env::var("QSV_DOTENV_PATH") { + if let Err(e) = dotenvy::from_filename_override(dotenv_path.clone()) { + return fail_clierror!( + "Cannot process .env file set in QSV_DOTENV_PATH - {dotenv_path}: {e}" + ); + } + log::info!("Using .env file: {dotenv_path}"); + return Ok(()); + } + // check if there is an .env file in the current directory if dotenvy::dotenv_override().is_ok() { log::info!(