air-verse / air

☁️ Live reload for Go apps
GNU General Public License v3.0
17.63k stars 802 forks source link

Load environment variables from file #405

Open ZaninAndrea opened 1 year ago

ZaninAndrea commented 1 year ago

This pull request adds a new key env_file to the configuration, the key specifies a file from which the environment variables for running bin should be sourced.
If env_file is set, the environment file is sourced every time the bin is run, so that it works as expected when "env" is in include_ext and the env file is updated.

The new feature was suggested in #47 and removes the need for less elegant solutions like the one suggested in #58.

silverwind commented 1 year ago

Does it also work when cmd and full_bin run? Please add some docs on where it exactly applies. I guess some people might also prefer a separate vars file between cmd vars and bin / full_bin, so that will need two options.

I would suggest setting the default to "", so this is not enabled by default and does not break existing setups.

Another problem I see that this does not support multipe files, so it would not be useable as-is to me, as I use setups with at least two files, like this.

mfechner commented 1 year ago

I think using cmd or full_bin will not work if you use air on e.g. Linux and Windows for the same project, as both environment are defining env variables differently. I think using a env_file configuration parameter is the only way to make this feature for all operating systems available.

ZaninAndrea commented 1 year ago

The implementation doesn't break existing setups, if the env_file parameter is not specified it defaults to "" (due to how the parameters are parsed) and thus no environment file is loaded. I added the env_file = ".env" parameter in the example configuration as a form of documentation, but that file is meant to be modified by the user when doing the first configuration.

The environment file is loaded when running bin/full_bin but not when running cmd. I think that this feature is much more important for bin/full_bin rather than cmd, because the environment variables used in the build command can generally be safely committed in git, while the variables used at runtime may contain secrets; either way we could extend this implementation with another cmd_env_file (or something similar) quite easily.

Multiple environment files are indeed not supported, I could extend this to accept a list of files instead of a single file.

whoiscarlo commented 1 year ago

Any updates on this?

whoiscarlo commented 1 year ago

I think using cmd or full_bin will not work if you use air on e.g. Linux and Windows for the same project, as both environment are defining env variables differently. I think using a env_file configuration parameter is the only way to make this feature for all operating systems available.

~I found that it also doesn't work on Mac Os, not sure what's missing but my environment variables aren't being brought over~

Solution is to use bin instead of full_bin. i.e bin=";APP_ENV= local APP_USER=me ./tmp/main"