bolorundurowb / dotenv.net Goto Github PK
View Code? Open in Web Editor NEWA library to read .env files in a .NET Core environment
License: MIT License
A library to read .env files in a .NET Core environment
License: MIT License
Hi,
You recently decided to provide easy DI for your package, the thing is, you provided your package with direct dependency of AutoFac on version 2.0+
Do you think you can split the AutoFac DI extensions to another package ? like dotenv.net.AutoFac
Thanks for your library.
After working with another library that does this with node projects, it's enabled an easier path for configuring/using .env
out of the box with projects.
In addition to default searching for .env
file, search for the following file(s) in the following order...
.env.%DOTNET_ENV%.local
.env.%DOTNET_ENV%
.env.local
.env
Where %DOTNET_ENV%
uses the environment variable if already set.
What is that dependency for? I couldn't find any references to it.
How do I load it async?
There's a slightly confusing difference between using envFilePaths
and probeForEnv
.
When you specify envFilePaths
, the paths are relative to the current directory (i.e. Directory.GetCurrentDirectory();
). For example if I specify a relative path ..\.env
it's one directory up from the current directory.
When you specify probeForEnv
, it begins searching from AppContext.BaseDirectory
. This isn't usually the same as the current directory. For example:
AppContext.BaseDirectory => C:\MyApp\MyApp.Web\bin\Debug\net6.0\
Directory.GetCurrentDirectory() => C:\MyApp\MyApp.Web\
So if I had a file C:\MyApp\.env
using probeForEnv wouldn't find it (with the default 4 levels to search), but using the explicit path ..\.env
would find it. I found this confusing until I looked at the source code.
it would be nice if in addition to
KC_ADDR=...
that this also was supported
export KC_ADDR=...
If I put any "=" in the value side, like:
variable=123456789=
With any subsequente "=", the value returned will be: 123456789 because of dotEnvRow.Split and the fact that you just use the first and second keyValue's.
I think you could find the first "=" and manually separate the line in a key/value pair, value being the rest of the line.
On the nuget site (https://www.nuget.org/packages/dotenv.net/), the project site link is incorrect. I know this isn't specifically a github issue, but thought you would want to know.
As per title it would be good if multi-line values would be supported.
For example certificate public/private keys are not supported.
There is a lot of project still in .NET Framework and maybe it will be a good idea to add support for them or add a section with examples if this is now possible
I want to load envvars from a list of files, such as a duplicated key in a later file does not overwrite a previously set value. So if .env.local
has VAL=hello
and .env
has VAL=default
, using WithoutOverwriteExistingVars()
would end up having VAL=hello
.
DotEnv.Fluent()
.WithEnvFiles([".env.local", ".local.env", ".env"])
.WithoutOverwriteExistingVars()
.Load();
As per the README, I though that WithoutOverwriteExistingVars()
was exactly for this purpose.
To skip overwriting an environment variable if it is set.
However, it only consider environment variable that was existing prior running the Load()
method.
Not sure if bug or feature request, but I would like that setting OverwriteExistingVars
to false also consider the files provided. Could be in a option flag to prevent breaking change.
Right now, i dont think there is an easy way of configuring this with autofac. please can that feature be added. I can also raise a PR for that
in Microsoft.Extensions.Configuration, ConfigurationBuilder has the ability to load configuration from a dictionary.
Because of that, it would be nice to support using dotenv.net to parse the .env file and return it as a dictionary then allow the caller to use that dictionary.
Currently because it doesn't support returning a dictionary instead of modifying the environment, a .net core test project wanting to use it must switch their <Project Sdk= value from Microsoft.NET.Sdk to Microsoft.NET.Sdk.Web so they can use AddEnvironmentVariables
Ideally this would also support the "look up the directory chain for the .env file" like AutoConfig, but if not by default then maybe by separate Parse and AutoParse (?) methods. If that's not allowed, maybe refactor out the "find nearest .env up the directory chain" from AutoConfig into a FindNearestDotEnv (or whatever name) and then callers could use it and then pass to Parse.
It would be nice if it searched up from the current directory in order to find a .env
up the chain without erroring.
using System;
using System.IO;
using System.Linq;
using DotEnv = dotenv.net.DotEnv;
...
namespace dotenv.net
{
public class DotEnv
{
...
public static void AutoConfig() {
var levelsToCheck = 4;
var currentDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
for (; currentDirectory != null && levelsToCheck > 0; levelsToCheck--, currentDirectory = currentDirectory.Parent)
{
foreach (var fi in currentDirectory.GetFiles(".env", SearchOption.TopDirectoryOnly))
{
Config(false, fi.FullName);
return; // found .env file
}
};
}
}
}
I keep getting the error The file path cannot be null, empty or whitespace. (Parameter 'envFilePath')
.
I'm using it from F# like this:
[<EntryPoint>]
let main _ =
let opts =
new DotEnvOptions(probeForEnv = true, probeLevelsToSearch = 2, ignoreExceptions = false)
DotEnv.Load(opts)
As far as I can tell from the docs envFilePaths
isn't a required parameter and either way it's envFilePath
in the singular that's in the error message so it looks to me like a bug.
Awesome Project!
However, I think since it's supposed to work in .NET core, it should be injectable as a dependency in classes.
Especially since .NET core supports and encourages dependency injection.
If I have a variable with a value that has a space in it, I have to use double quotation marks. However, when running this in ASP.NET Core 2.2, DotEnv includes the quotations marks in variable rather than understanding it's marking the start and end of the string. could it be possible to fix this bug? I require quotation marks for file paths in which folders have spaces, and I say it's a bug because this type of value is handled well in other dotenv package equivalents in other languages.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.