Skip to content

Commit

Permalink
Merge pull request #1562 from jqnatividad/qsv_dotenv_path-envvar
Browse files Browse the repository at this point in the history
Add QSV_DOTENV_PATH env var
  • Loading branch information
jqnatividad authored Jan 25, 2024
2 parents 55123ec + 328dee9 commit 73c8acf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 3 additions & 1 deletion docs/ENVIRONMENT_VARIABLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br>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`. |
Expand Down Expand Up @@ -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.

Expand Down
14 changes: 13 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down

0 comments on commit 73c8acf

Please sign in to comment.