dotenv-cli icon indicating copy to clipboard operation
dotenv-cli copied to clipboard

Specify directory containing .env files

Open Stadly opened this issue 1 year ago • 3 comments

When using cascading env variables, you might end up with a lot of .env files. It is then common to group the .env files in a folder. Would it be possible to add a flag for specifying where dotenv should look for .env files? I suggest -d <dir>. For consistency, I think it should also work without -c, though that is probably the main use case.

# Load vars/.env (-d without -e or -c)
dotenv -d vars <cmd>

# Load vars/.env2 (-d combined with -e)
dotenv -d vars -e .env2 <cmd>

# Load env/.env.local and env/.env (-d combined with -c)
dotenv -d env -c <cmd>

Stadly avatar Jan 19 '24 09:01 Stadly

I see how that could be useful. I will accept a PR like this

entropitor avatar Jan 19 '24 13:01 entropitor

After looking at the source code, I'm not sure -d is needed after all. Since -c cascades all files added with -e, the behavior provided by -d can already be achieved with the current functionality:

# Load vars/.env
dotenv -e vars/.env <cmd>

# Load vars/.env2
dotenv -e vars/.env2 <cmd>

# Load env/.env.local and env/.env (the use case I thought wasn't possible without -d)
dotenv -e env/.env -c <cmd>

What do you think?

Stadly avatar Jan 22 '24 06:01 Stadly

Yeah, I guess it might be better to keep the cli clean and lean and not add it. I guess if we really want to improve something, we could update the readme.

entropitor avatar Jan 23 '24 13:01 entropitor