hurl icon indicating copy to clipboard operation
hurl copied to clipboard

Automatic .env file support

Open ilog2000 opened this issue 2 years ago • 7 comments

It would be good to have automatic .env file detection and parsing, while variables-file option and environment variables should work as overrides.

ilog2000 avatar Mar 30 '22 09:03 ilog2000

Hi @ilog2000 , Hurl can use environment variables HURL_xxx from the system. https://hurl.dev/docs/man-page.html#environment

Therefore you can just put these variables in your .env file and source it before running Hurl. for example

source .env || true

fabricereix avatar Mar 30 '22 11:03 fabricereix

Hi @fabricereix , I understand what you are saying. I tried hurl on Windows, where source command does not exist, and I need to make some BAT file tricks to do the same. So, I know it is possible, but not convenient. Main goal of this ticket is to make hurl use as short as

hurl my-script.hurl

without long typing or batch files.

ilog2000 avatar Mar 31 '22 19:03 ilog2000

Hi @ilog2000

Software magic/automatic *.env file detection/parsing always depends on user's specific needs and developers patterns (there is no real IT cross-platform specification) so there are many ways to achieve what you expect:

  • env file location has to be in: current directory ? User home directory ? Hurl exec directory ? Hurl file directory ? Etc ...
  • env file name has to be: same as hurl file base name ? Prefixed with "hurl" ? Both ? Etc ...
  • env file extension has to be: *.env ? *.properties ? *.conf ? Etc ...

As hurl philosophy is to keep the code simple, high-performing and explicit, we plan to allow the creation of variables directly inside hurl files, we can also consider using multiple --variable-file like we did with --variable.

This would allow you to manage all cases:

# one file, one var
hurl --test --variable myvar=1 test_dir/test.hurl
hurl --test test_dir/test.hurl (with myvar=1 inside hurl file)

# one file, common vars and one var override
hurl --test --variable-file common_dir/common.env --variable myvar=1 test_dir/test.hurl
hurl --test --variable-file common_dir/common.env test_dir/test.hurl (with myvar=1 inside hurl file)

# one file, common vars and multiple vars overrides
hurl --test --variable-file common_dir/common.env --variable-file test_dir/test.hurl test_dir/test.hurl

# multiple files, common vars and one or multiple vars overrides according to the file
hurl --test --variable-file common_dir/common.env test_dir/*.hurl (with myvar=1 and yourvar=1 only setted inside needed files)

lepapareil avatar Apr 01 '22 12:04 lepapareil

Hi @lepapareil

Sorry, my bad, I had to explain the concept of .env (no claim it was my idea). It is no "magic", just a very simple convention consisting of 4 rules:

  1. File is always named .env (not .conf, not my.cfg, not anything else)
  2. Only file in the current directory where scripts located is picked up (no user home dir, no global configuration)
  3. File contents are lines in name=value format
  4. Those name=value lines are set as environment variables overriding global environment variables, so they add "environmental" context to the particular directory.

Practically any programming language has an implementation of this convention:

  • Rust https://github.com/dotenv-rs/dotenv
  • Go https://github.com/joho/godotenv
  • NodeJS https://github.com/motdotla/dotenv
  • Python https://github.com/theskumar/python-dotenv
  • Java https://github.com/cdimascio/java-dotenv https://github.com/cdimascio/dotenv-java
  • .NET https://github.com/bolorundurowb/dotenv.net https://github.com/tonerdo/dotnet-env https://github.com/codeyu/net-dotenv

Stars in corresponding repositories will reflect the popularity of this idea. On the other hand I know at least one very popular piece of software which does not process .env automatically, but only via command line option - this is Docker.

I am not trying to push hurl authors to accept this my suggestion, the decision is definitely up to them. I am just saying I personally find the idea of automatic .env file processing very convenient.

ilog2000 avatar Apr 04 '22 08:04 ilog2000

Hi @ilog2000,

I definitively agree with you about the ease of use of .env files for final users and that simplifying vars overrides for each executed hurl.file is a real need 👍🏻.

I just wanted to point out that there is not specs and even though it looks simple and is developed in several languages, it is not implemented the same way (except the first rule):

  • some override vars by default, others not
  • some are compatible with multiline var value, others not
  • some source only from root dir, others are recursive by default
  • etc ...

Sorry if i may have misspoken, i am not at all trying to push hurl authors not to accept your suggestion 😄, i just wanted to debate the pros and cons.

lepapareil avatar Apr 04 '22 09:04 lepapareil

There's a sort of spec: https://hexdocs.pm/dotenvy/dotenv-file-format.html

luolong avatar Dec 01 '22 14:12 luolong

See also #290

jcamiel avatar Dec 21 '23 15:12 jcamiel