Kuchulem.DotNet.Env
1.0.0
dotnet add package Kuchulem.DotNet.Env --version 1.0.0
NuGet\Install-Package Kuchulem.DotNet.Env -Version 1.0.0
<PackageReference Include="Kuchulem.DotNet.Env" Version="1.0.0" />
paket add Kuchulem.DotNet.Env --version 1.0.0
#r "nuget: Kuchulem.DotNet.Env, 1.0.0"
// Install Kuchulem.DotNet.Env as a Cake Addin #addin nuget:?package=Kuchulem.DotNet.Env&version=1.0.0 // Install Kuchulem.DotNet.Env as a Cake Tool #tool nuget:?package=Kuchulem.DotNet.Env&version=1.0.0
Introduction
The Kuchulem.DotNet.Env
library provides the EnvironmentParser
class to manage environment variables.
This class provides advanced mechanics to get environment variables values and map them to an object or class.
Getting Started
Add the nuget package to your project
Connect to the feed :
Install the library in your project :
From the CLI (powershell) :
Install-Package Kuchulem.DotNet.Env
or from the package manager in Visual Studio :
seach Kuchulem.DotNet.Env
and click on install
Include the package
include the Kuchulem.DotNet.Env
namespace when you need to use the EnvironmentParser
.
using Kuchulem.DotNet.Env;
Usage
To get environment variable value you have to instanciate the parser :
var parser = new EvironmentParser();
Get values
To retrieve the value of MY_ENV_VAR
environment variable as string :
string myEnvVar = parser.GetVariable("MY_ENV_VAR");
You can also get a converted value :
object dbPort = parser.GetVariable("BD_PORT", typeof(int));
DateTime releaseDate = parser.GetVariable<DateTime>("RELEASE_DATE");
Map environment variables to an object
Usefull when creating configuration objects from environment variables, the
library provides the MapEnvVar
attribute to configure mapping between environment
variables and object.
Considering the following environement variables :
DB_NAME='my_db'
DB_USER='application_user'
DB_PASSWORD='the_password_is_in_keypass'
DB_ADDRESS='127.0.0.1'
DB_PORT='3333'
Define a configuration class with the MapEnvVar
attribute on each property :
using Kuchulem.DotNet.Env.Attributes;
namespace MyApp.Configurations
{
class DbConfig
{
// The property DbName will be mapped with the
// DB_NAME environement variable
[MapEnvVar("DB_NAME")]
public string? DbName { get; set; }
// The property DbUser will be mapped with the
// DB_USER environement variable
[MapEnvVar("DB_USER")]
public string? DbUser { get; set; }
// The property DbPassword will be mapped with the
// DB_PASSWORD environement variable
[MapEnvVar("DB_PASSWORD")]
public string? DbPassword { get; set; }
// The property DbAddress will be mapped with the
// DB_ADDRESS environement variable
[MapEnvVar("DB_ADDRESS")]
public string? DbAddress { get; set; }
// The property DbPort will be mapped with the
// DB_PORT environement variable
[MapEnvVar("DB_PORT")]
public int DbPort { get; set; }
}
}
Then invoke the MapVariables<T>
method of the parser :
var dbConfig = parser.MapVariables<DbConfig>();
Console.WriteLine(dbConfig.DbName); // outputs "my_db"
You can also populate an existing object :
var dbConfig = new DbConfig();
parser.MapVariables(dbConfig);
Console.WriteLine(dbConfig.DbName); // outputs "my_db"
Secrets in files
If you wish to keep some secrets in files, this is possible by adding
the _FILE
suffix to you environement variable.
Consider the follwing environment variable :
DB_PASSWORD_FILE='/var/secrets/db_password'
and the file /var/secrets/db_password
content :
the_password_is_in_keypass
To get the password value keep using the DB_PASSWORD
variable name in your code :
var password = parser.GetVariable("DB_PASSWORD");
Console.WriteLine(password); // outputs "the_password_is_in_keypass" (never do that !)
The _FILE
suffix can be customized by using the fileEnvSuffix
optional
argument in the EnvironmentParser
constructor.
consider the env var :
DB_PASSWORD_SECRET='/var/secrets/db_password'
var parser = new EnvironmentParser("_SECRET"); // change the suffix to _SECRET
var password = parser.GetVariable("DB_PASSWORD");
Console.WriteLine(password); // outputs "the_password_is_in_keypass" (never do that !)
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 389 | 11/21/2022 |
1.0.0-beta.4 | 111 | 11/21/2022 |
1.0.0-beta.3 | 106 | 11/21/2022 |
1.0.0-beta.2 | 110 | 11/21/2022 |