proposal: make env file optional
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 https://github.com/joho/godotenv/issues/99#issuecomment-673937686
I think an easy workaround would be adding an empty .env in Docker.
I think an easy workaround would be adding an empty .env in Docker.
Kinda a shitty solution.
You can check the error against os.IsNotExists instead of adding a workaround.
@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...
@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.
@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.
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")
}