All Projects → bolorundurowb → dotenv.net

bolorundurowb / dotenv.net

Licence: MIT License
A library to read .env files in a .NET Core environment

Programming Languages

C#
18002 projects
shell
77523 projects

Projects that are alternatives of or similar to dotenv.net

ini
📝 Go INI config management. support multi file load, data override merge. parse ENV variable, parse variable reference. Dotenv file parse and loader. INI配置读取管理,支持多文件加载,数据覆盖合并, 解析ENV变量, 解析变量引用。DotEnv 解析加载
Stars: ✭ 72 (-42.86%)
Mutual labels:  dotenv, environment-variables
dart environment config
Environment specific config generator for Dart and Flutter applications during CI/CD builds
Stars: ✭ 87 (-30.95%)
Mutual labels:  dotenv, environment-variables
webpack-dotenv-plugin
Use dotenv with webpack.
Stars: ✭ 53 (-57.94%)
Mutual labels:  dotenv, environment-variables
Env Var
Verification, sanitization, and type coercion for environment variables in Node.js
Stars: ✭ 201 (+59.52%)
Mutual labels:  dotenv, environment-variables
angular-cli-envvars
Example project for my article "Angular CLI and OS Environment Variables"
Stars: ✭ 56 (-55.56%)
Mutual labels:  dotenv, environment-variables
dotenvy
Speed up your production sites by ditching .env for key/value variable pairs as Apache, Nginx, and shell equivalents
Stars: ✭ 31 (-75.4%)
Mutual labels:  dotenv, environment-variables
checkdotenv
Verify environment variables presence for Node JS.
Stars: ✭ 12 (-90.48%)
Mutual labels:  dotenv, environment-variables
Dotenv Flow
Loads environment variables from .env.[development|test|production][.local] files for Node.js® projects.
Stars: ✭ 537 (+326.19%)
Mutual labels:  dotenv, environment-variables
gconfigs
gConfigs - Config and Secret parser
Stars: ✭ 42 (-66.67%)
Mutual labels:  dotenv, environment-variables
dotenv
Load .env files in crystal
Stars: ✭ 16 (-87.3%)
Mutual labels:  dotenv, environment-variables
React Native Dotenv
Load react native environment variables using import statements for multiple env files.
Stars: ✭ 190 (+50.79%)
Mutual labels:  dotenv, environment-variables
ngx-env
Easily inject environment variables into your Angular applications
Stars: ✭ 73 (-42.06%)
Mutual labels:  dotenv, environment-variables
Dotenv Java
🗝️ Dotenv is a no-dep, pure Java module that loads environment variables from a .env file
Stars: ✭ 72 (-42.86%)
Mutual labels:  dotenv, environment-variables
cypress-dotenv
Cypress plugin that enables compatability with dotenv
Stars: ✭ 47 (-62.7%)
Mutual labels:  dotenv, environment-variables
Dotenv Webpack
A secure webpack plugin that supports dotenv and other environment variables and only exposes what you choose and use.
Stars: ✭ 1,022 (+711.11%)
Mutual labels:  dotenv, environment-variables
superconfig
Access environment variables. Also includes presence validation, type coercion and default values.
Stars: ✭ 33 (-73.81%)
Mutual labels:  dotenv, environment-variables
Dotenv Kotlin
🗝️ Dotenv is a module that loads environment variables from a .env file
Stars: ✭ 326 (+158.73%)
Mutual labels:  dotenv, environment-variables
Sync Dotenv
Keep your .env in sync with .env.example
Stars: ✭ 393 (+211.9%)
Mutual labels:  dotenv, environment-variables
exenv
Exenv makes loading environment variables from external sources easy.
Stars: ✭ 35 (-72.22%)
Mutual labels:  dotenv, environment-variables
php-env
A small and fast .env loader for PHP
Stars: ✭ 19 (-84.92%)
Mutual labels:  dotenv, environment-variables

dotenv.net

CircleCI License: MIT Coverage Status NuGet Badge

project icon

dotenv.net is a group of projects that aim to make the process of reading .env files as simple and pain-free as possible in the dotnet ecosystem. It contains a core library that holds the env reading functionality and two libraries that add dependency injection (DI) support for two popular DI systems. If you have ideas or issues, feel free to create an issue.

Important

Version 3 is a huge departure from the previous system of accessing the library functionality. Deprecated methods from version 2.x have been removed as indicated. New functionality has been added but the reading and parsing logic has mostly stayed unchanged meaning at it's heart, is still the same ol' reliable.

Installation

If you are hardcore and want to go the manual route. Then add the following to your csproj file:

<PackageReference Include="dotenv.net" Version="3.0.0"/>

If you're using the Visual Studio package manager console, then run the following:

Install-Package dotenv.net

If you are making use of the dotnet CLI, then run the following in your terminal:

dotnet add package dotenv.net

Usage

Ensure you have declared the necessary namespace at the head of your class file:

using dotenv.net;

Load Environment Variables

Calling the Load() method with no parameters would locate and load the .env file in the same directory that the library is if one exists:

DotEnv.Load();

If you want to be notified of exceptions that occur in the process of loading env files then you can specify that via the configuration options:

DotEnv.Load(options: new DotEnvOptions(ignoreExceptions: false));

You can specify the env files to be loaded. One or more can be loaded. (NOTE: the order in which the env paths are provided is crucial. If there is a duplicate key and value specified in an env file specified later in the list, that value would overwrite the earlier values read). The default is .env:

DotEnv.Load(options: new DotEnvOptions(envFilePaths: new[] {"./path/to/env", "./path/to/second/env"}));

To search up from the executing library's directory for an env file. The directories would be searched upwards i.e given a directory path /path/to/var, The var directory would be searched first, then the to directory and then the path directory. The options allow for probing the directories as well as specifying how high up to search. The defaults are true and 4 directories up:

DotEnv.Load(options: new DotEnvOptions(probeForEnv: true, probeLevelsToSearch: 2)); // this would only search 2 directories up from the executing directory.

If the provided env files are not UTF-8 encoded, then the encoding to use in reading the files can be specified. The default is UTF-8:

using System.Text;
...
DotEnv.Load(options: new DotEnvOptions(encoding: Encoding.ASCII));

To trim extraneous whitespace from the values read from the file(s). The default is false:

DotEnv.Load(options: new DotEnvOptions(trimValues: true));

To skip overwriting an environment variable if it is set. The default is true:

DotEnv.Load(options: new DotEnvOptions(overwriteExistingVars: false));

Read Environment Variables

The Read() method returns a IDictionary<string, string> instance detailing the keys and associated values read from the env files provided. This hase the added advantage of not modifying your system environment variables. The same options as apply to the Load() method, apply to the Read() method as well.

var envVars = DotEnv.Read();
Console.WriteLine(envVars["KEY"]); // would print out whatever value was associated with the 'KEY'

Fluent API

There is a fluent API analogue to the static methods documented above and can be terminated with a Read() or Load() call to return the env value or write to the environment variables .

// to load env vars with the specified options
DotEnv.Fluent()
    .WithExceptions()
    .WithEnvFiles("./path/to/env")
    .WithTrimValues()
    .WithEncoding()
    .WithOverwriteExistingVars()
    .WithProbeForEnv(probeLevelsToSearch: 6)
    .Load();

// to read the env vars with the specified options
var envVars = DotEnv.Fluent()
    .WithoutExceptions()
    .WithEnvFiles() // revert to the default .env file
    .WithoutTrimValues()
    .WithDefaultEncoding()
    .WithoutOverwriteExistingVars()
    .WithoutProbeForEnv()
    .Read();

Environment Variable Helpers

The Utilities namespace provides additional classes to aid in reading environment in a typed manner as well as other sundry assistive methods. Ensure you have declared the necessary namespace at the head of your class file:

using dotenv.net.Utilities;
...
var value = EnvReader.GetStringValue("KEY");

EnvReader Methods

Method Name Description Return Type Default (if applicable)
HasValue(string key) States whether the given key has a value set or not. bool N/A
GetStringValue(string key) Retrieve a value from the current environment by the given key and throws an exception if not found. string N/A
GetIntValue(string key) Retrieve a value from the current environment by the given key and throws an exception if not found. int N/A
GetDoubleValue(string key) Retrieve a value from the current environment by the given key and throws an exception if not found. double N/A
GetDecimalValue(string key) Retrieve a value from the current environment by the given key and throws an exception if not found. decimal N/A
GetBooleanValue(string key) Retrieve a value from the current environment by the given key and throws an exception if not found. bool N/A
TryGetStringValue(string key, out string value) A safer method to use when retrieving values from the environment as it returns a boolean value stating whether it successfully retrieved the value required. bool null
TryGetIntValue(string key, out int value) A safer method to use when retrieving values from the environment as it returns a boolean value stating whether it successfully retrieved the value required. bool 0
TryGetDoubleValue(string key, out double value) A safer method to use when retrieving values from the environment as it returns a boolean value stating whether it successfully retrieved the value required. bool 0.0
TryGetDecimalValue(string key, out decimal value) A safer method to use when retrieving values from the environment as it returns a boolean value stating whether it successfully retrieved the value required. bool 0.0m
TryGetBooleanValue(string key, out bool value) A safer method to use when retrieving values from the environment as it returns a boolean value stating whether it successfully retrieved the value required. bool false

Contributors

Big ups to those who have contributed to these libraries. 👏

@bolorundurowb @joliveros @vizeke @merqlove @tracker1 @NaturalWill @texyh @jonlabelle @Gounlaf @DTTerastar @Mondonno

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].