Skip to content

Commit

Permalink
Merge pull request #3 from bolorundurowb/develop
Browse files Browse the repository at this point in the history
release: add support for injection
  • Loading branch information
Bolorunduro Winner-Timothy B authored Dec 31, 2017
2 parents 407034d + 07189b1 commit 6cb40ad
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 1 deletion.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@

dotenv.net is a zero-dependency module that loads environment variables from a .env file into `Environment`.

## Contributors

Big ups to those who have contributed to this library. :clap:

[@bolorundurowb](https://github.com/bolorundurowb) [@joliveros](https://github.com/joliveros)

## Usage

### Conventional

First install the library as a dependency in your application from nuget

```
Expand All @@ -24,7 +32,7 @@ or for paket
paket add dotenv.net
```

Create a file with no filename and an extension of `.env`. Add the file to the root of your project. Set the file `Copy To Output Directory` to `Always`.
Create a file with no filename and an extension of `.env`.

A sample `.env` file would look like this:
```text
Expand All @@ -50,6 +58,27 @@ the values saved in your `.env` file would be avaibale in your application and c
Environment.GetEnvironmentVariable("DB_HOST"); // would output 'localhost'
```

## With ASP.NET Core or any other DI system that uses `IServiceCollection`

In the `Startup.cs` file

``` csharp
public void ConfigureServices(IServiceCollection services)
{
...

// configure dotenv
services.AddEnv(builder => {
builder
.AddEnvFile("/custom/path/to/your/env/vars")
.AddThrowOnError(false)
.AddEncoding(Encoding.ASCII);
});
}
```

With this, your application would have the env file variables imported.

### Options

#### ThrowError
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Text;
using dotenv.net.DependencyInjection.Infrastructure;
using Microsoft.Extensions.DependencyInjection;

namespace dotenv.net.DependencyInjection.Extensions
{
/// <summary>
/// Servcie collection extensions
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Add the environment vars using a service
/// </summary>
/// <param name="services">The service collection</param>
/// <returns>The service collection</returns>
public static IServiceCollection AddEnv(this IServiceCollection services)
{
AddEnv(services, builder =>
{
builder
.AddEnvFile(".env")
.AddEncoding(Encoding.Default)
.AddThrowOnError(true);
});
return services;
}

/// <summary>
/// Add the environment vars using a service
/// </summary>
/// <param name="services">Service collection</param>
/// <param name="setupAction">The dot env options buider action</param>
/// <returns>The service collection</returns>
/// <exception cref="ArgumentNullException">If the service passed in is null</exception>
public static IServiceCollection AddEnv(this IServiceCollection services, Action<DotEnvOptionsBuilder> setupAction)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (setupAction == null)
{
throw new ArgumentNullException(nameof(setupAction));
}

DotEnvOptionsBuilder dotEnvOptionsBuilder = new DotEnvOptionsBuilder();
setupAction(dotEnvOptionsBuilder);

var dotEnvOptions = dotEnvOptionsBuilder.Build();
DotEnv.Config(dotEnvOptions);

return services;
}
}
}
22 changes: 22 additions & 0 deletions dotenv.net/DependencyInjection/Infrastructure/DotEnvOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Text;

namespace dotenv.net.DependencyInjection.Infrastructure
{
public class DotEnvOptions
{
/// <summary>
/// A value to state whether to throw an exception if the env file doesn't exist. The default is true. <see cref="T:dotenv.net.DependencyInjection.Infrastructure.DotEnvOptions"/>
/// </summary>
public bool ThrowOnError { get; set; }

/// <summary>
/// The path to the env file. The default is ".env". <see cref="T:dotenv.net.DependencyInjection.Infrastructure.DotEnvOptions"/>
/// </summary>
public string EnvFile { get; set; }

/// <summary>
/// The Encoding that the env file was created with. <see cref="T:dotenv.net.DependencyInjection.Infrastructure.DotEnvOptions"/>
/// </summary>
public Encoding Encoding { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Text;

namespace dotenv.net.DependencyInjection.Infrastructure
{
public class DotEnvOptionsBuilder
{
private readonly DotEnvOptions _dotEnvOptions = new DotEnvOptions();

/// <summary>
/// Sets the environment file to be read <see cref="T:dotenv.net.DependencyInjection.Infrastructure.DotEnvOptionsBuilder"/>
/// </summary>
/// <param name="file">The file path</param>
/// <returns>The current options builder</returns>
public DotEnvOptionsBuilder AddEnvFile(string file)
{
_dotEnvOptions.EnvFile = file;
return this;
}

/// <summary>
/// Sets the option to throw an eception if an error should occur <see cref="T:dotenv.net.DependencyInjection.Infrastructure.DotEnvOptionsBuilder"/>
/// </summary>
/// <param name="throwOnError">The option to use</param>
/// <returns>The current options builder</returns>
public DotEnvOptionsBuilder AddThrowOnError(bool throwOnError)
{
_dotEnvOptions.ThrowOnError = throwOnError;
return this;
}

/// <summary>
/// Set the encoding to read the env file in. <see cref="T:dotenv.net.DependencyInjection.Infrastructure.DotEnvOptionsBuilder"/>
/// </summary>
/// <param name="encoding">The encoding to use</param>
/// <returns>The current options builder</returns>
public DotEnvOptionsBuilder AddEncoding(Encoding encoding)
{
_dotEnvOptions.Encoding = encoding;
return this;
}

/// <summary>
///
/// </summary>
/// <returns>The constructed <see cref="T:dotenv.net.DependencyInjection.Infrastructure.DotEnvOptions"/></returns>
public DotEnvOptions Build()
{
return _dotEnvOptions;
}
}
}
10 changes: 10 additions & 0 deletions dotenv.net/DotEnv.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Text;
using dotenv.net.DependencyInjection.Infrastructure;

namespace dotenv.net
{
Expand Down Expand Up @@ -55,5 +56,14 @@ public static void Config(bool throwOnError = true, string filePath = ".env", En
}
}
}

/// <summary>
/// Configure the environment variables from a .env file
/// </summary>
/// <param name="options">Options on how to load the env file</param>
public static void Config(DotEnvOptions options)
{
Config(options.ThrowOnError, options.EnvFile, options.Encoding);
}
}
}
14 changes: 14 additions & 0 deletions dotenv.net/dotenv.net.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>1.0.1</Version>
<Copyright>Copyright 2018</Copyright>
<PackageReleaseNotes>Make the config varibales injectable</PackageReleaseNotes>
<Authors>Bolorunduro Winner-Timothy</Authors>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageId>dotenv.net</PackageId>
<Description>A library to read .env files and add then values to the environment variables of the application.</Description>
<PackageIconUrl>https://raw.githubusercontent.com/motdotla/dotenv/master/dotenv.png</PackageIconUrl>
<PackageProjectUrl>https://bolorundurowb.github.io/dotenv.net/</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/bolorundurowb/dotenv.net/blob/master/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/bolorundurowb/dotenv.net</RepositoryUrl>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
</ItemGroup>
</Project>

0 comments on commit 6cb40ad

Please sign in to comment.