Skip to content

ENV configuration provider implementation for Microsoft.Extensions.Configuration.

License

Notifications You must be signed in to change notification settings

YuKitsune/Configuration.Env

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

YuKitsune.Configuration.Env

ENV configuration provider implementation for Microsoft.Extensions.Configuration.

GitHub Workflow Status Nuget

Installation

You can install the package via the .NET Core CLI:

dotnet add package YuKitsune.Configuration.Env

Usage

1. Create the .env file

Create a .env file in the root of your project.

Note: Any file with .env in the name will get copied to the output directory. Example: .env, dev.env, .env.prod, etc.

2. Configure the host builder

public static IHostBuilder CreateHostBuilder(string[] args)
{
  Host.CreateDefaultBuilder(args)
    .ConfigureAppConfiguration((context, config) =>
      {
        config.AddEnvFile(".env");
      })
    .ConfigureWebHostDefaults(webBuilder =>
      {
        webBuilder.UseStartup<Startup>();
      });
}

That's it! Your program is now reading from .env files!

For more information about how configuration works in .NET Core (and in ASP.NET Core specifically), please see the official docs.

ENV file format

The .env file parser is designed to be compatable with docker compose env files, and thus uses the same format.

The following syntax rules apply to the .env file:

  • Compose expects each line in an env file to be in VAR=VAL format.
  • Lines beginning with # are processed as comments and ignored.
  • Blank lines are ignored.
  • There is no special handling of quotation marks. This means that they are part of the VAL.

For more information, please see the official docs.

Caveats

This implementation works similarly to how the implementation for Environment Variables works.

The : separator doesn't work with environment variable hierarchical keys on all platforms. __, the double underscore, is:

  • Supported by all platforms. For example, the : separator is not supported by Bash, but __ is.
  • Automatically replaced by a :

For more information, please see the official docs.

Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.

Credits

This implementation is heavily based on the Microsoft.Extensions.Configurations.Ini package. Special thanks to all those who've contributed to it!

.NET Core Runtime repo: https://github.com/dotnet/runtime

Original source: https://github.com/dotnet/runtime/tree/main/src/libraries/Microsoft.Extensions.Configuration.Ini

About

ENV configuration provider implementation for Microsoft.Extensions.Configuration.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages