Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: make env file optional #99

Open
urbanishimwe opened this issue Apr 12, 2020 · 7 comments
Open

proposal: make env file optional #99

urbanishimwe opened this issue Apr 12, 2020 · 7 comments

Comments

@urbanishimwe
Copy link

urbanishimwe commented Apr 12, 2020

When you call Load() or Overload() without args it returns an error open .env: no such file or directory. There is a situation where .env file is not necessary like deploying with docker or running tests using CI tools like CircleCI where the environment can be set in other ways.
as such, it will be good if .env file is made optional so the app can use the existing os Env or get env through os.Args
EDITED:
check this comment for more #99 (comment)

@ivan-avalos
Copy link

I think an easy workaround would be adding an empty .env in Docker.

@robbyoconnor
Copy link

I think an easy workaround would be adding an empty .env in Docker.

Kinda a shitty solution.

@DeadlySurgeon
Copy link

DeadlySurgeon commented Aug 13, 2020

You can check the error against os.IsNotExists instead of adding a workaround.

@urbanishimwe
Copy link
Author

@DeadlySurgeon the thing is, Overload and Load may receive a list of files to read envs from! when say one of the file doesn't exist those functions will return an error immediately without loading envs from subsequent files(the situation may turn worse if that file was not crucial and subsequent files was crucial), the workaround you provided would only work with loading from a single file! I raised a PR #100 for this...

@DeadlySurgeon
Copy link

DeadlySurgeon commented Aug 14, 2020

@urbanishimwe Sure, but in your example you're specifying with no args not with args, and in reality, in your CI/CD you shouldn't have any .env files so it should be fine to ignore the error if os.IsNotExist(err). Realistically, you should not be loading .env files outside of a local development machine and it boils down to a proper workflow. CI/CDs should store secrets, deployed environments should have their environmental variables managed by ops and not deployed in a repository.

@urbanishimwe
Copy link
Author

@DeadlySurgeon you can have secret environments in .env and non-secret environments in .noscret.env, if loading .env fails it shouldn't affect .noscret.env(optionally).
I also think that not every project has to follow this model(especially in large scale applications)

CI/CDs should store secrets, deployed environments should have their environmental variables managed by ops and not deployed in a repository.

@guizoxxv
Copy link

guizoxxv commented Jan 14, 2021

The method is os.IsNotExist() not os.IsNotExists() - https://golang.org/pkg/os/#IsNotExist. Here is my implementation:

if err := godotenv.Load(); err != nil && !os.IsNotExist(err) {
	log.Fatalln("Error loading .env")
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants