-
Notifications
You must be signed in to change notification settings - Fork 76
feat(ConfigurationReader): FromEnvironmentVariables(prefix), FromMicrosoftConfiguration #308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
8e2ff31
b611d75
f82c864
1f581fb
9ef9f52
d3d9098
9f26806
0bf399f
53b910b
61bc252
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <TargetFramework>netstandard2.0</TargetFramework> | ||
| <GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
| <GenerateAssemblyInfo>true</GenerateAssemblyInfo> | ||
| <Description>Microsoft.Extensions.Configuration adapter for Argu — exposes any IConfiguration source as an Argu IConfigurationReader.</Description> | ||
| <PackageTags>F#, argument, commandline, parser, configuration</PackageTags> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="ConfigurationReader.fs"/> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="..\Argu\Argu.fsproj"/> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include="FSharp.Core"/> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions"/> | ||
| </ItemGroup> | ||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| namespace Argu.MSConfig | ||
|
|
||
| open Microsoft.Extensions.Configuration | ||
|
|
||
| open Argu | ||
|
|
||
| /// <summary> | ||
| /// Argu <see cref="IConfigurationReader"/> backed by a | ||
| /// <see cref="Microsoft.Extensions.Configuration.IConfiguration"/>. | ||
| /// The reader returns <c>null</c> when a key is missing, matching the | ||
| /// contract of every other Argu configuration reader. | ||
| /// </summary> | ||
| type MicrosoftExtensionsConfigurationReader(configuration : IConfiguration, ?name : string) = | ||
| let name = defaultArg name "Microsoft.Extensions.Configuration reader" | ||
| interface IConfigurationReader with | ||
| member _.Name = name | ||
| member _.GetValue(key : string) = | ||
| // IConfiguration.[key] returns null for missing keys, which matches Argu's | ||
| // expectation. | ||
| configuration[key] | ||
|
|
||
| /// Factory helpers mirroring the shape of <see cref="ConfigurationReader"/>. | ||
| [<AbstractClass; Sealed>] | ||
| type ConfigurationReader = | ||
| /// Create an Argu <see cref="IConfigurationReader"/> over an <see cref="IConfiguration"/>. | ||
| static member FromMicrosoftConfiguration(configuration : IConfiguration, ?name : string) : IConfigurationReader = | ||
| MicrosoftExtensionsConfigurationReader(configuration, ?name = name) :> _ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| namespace Argu.Tests | ||
|
|
||
| open System | ||
| open Xunit | ||
| open Swensen.Unquote | ||
|
bartelink marked this conversation as resolved.
Outdated
|
||
|
|
||
| open Argu | ||
|
|
||
| /// Tests for the env-var prefix configuration reader (PR 20). | ||
| module ``Argu Tests EnvVarPrefix`` = | ||
|
|
||
| type Args = | ||
| | [<CustomAppSettings("HOST")>] HostName of string | ||
| | [<CustomAppSettings("PORT")>] Port of int | ||
| interface IArgParserTemplate with member this.Usage = "x" | ||
|
|
||
| let private withEnv (key : string) (value : string) (body : unit -> 'T) : 'T = | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably worth making this return an
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in d3d9098 — |
||
| let prior = Environment.GetEnvironmentVariable key | ||
| try | ||
| Environment.SetEnvironmentVariable(key, value) | ||
| body () | ||
| finally | ||
| Environment.SetEnvironmentVariable(key, prior) | ||
|
|
||
| [<Fact>] | ||
| let ``FromEnvironmentVariables(prefix) prepends prefix to key`` () = | ||
| let reader = ConfigurationReader.FromEnvironmentVariables(prefix = "MYAPP_") | ||
| withEnv "MYAPP_HOST" "example.com" (fun () -> | ||
| test <@ reader.GetValue("HOST") = "example.com" @>) | ||
|
|
||
| [<Fact>] | ||
| let ``FromEnvironmentVariables(prefix) returns null for missing keys`` () = | ||
| let reader = ConfigurationReader.FromEnvironmentVariables(prefix = "ARGU_TEST_MISSING_") | ||
| // Ensure no stray env var; SetEnvironmentVariable(null) clears it. | ||
| Environment.SetEnvironmentVariable("ARGU_TEST_MISSING_NOPE", null) | ||
| test <@ reader.GetValue("NOPE") = null @> | ||
|
|
||
| [<Fact>] | ||
| let ``FromEnvironmentVariables(prefix) parses round-trip through ArgumentParser`` () = | ||
| withEnv "DEMO_HOST" "host.local" (fun () -> | ||
| withEnv "DEMO_PORT" "443" (fun () -> | ||
| let parser = ArgumentParser.Create<Args>(programName = "demo") | ||
| let reader = ConfigurationReader.FromEnvironmentVariables(prefix = "DEMO_") | ||
| let results = parser.ParseConfiguration(reader, ignoreMissing = true) | ||
| test <@ results.GetResult(HostName) = "host.local" @> | ||
| test <@ results.GetResult(Port) = 443 @>)) | ||
|
|
||
| [<Fact>] | ||
| let ``No-arg FromEnvironmentVariables still works without prefix`` () = | ||
| // Sanity check: the legacy zero-arg overload is preserved. | ||
| withEnv "ARGU_TEST_PLAIN" "value" (fun () -> | ||
| let reader = ConfigurationReader.FromEnvironmentVariables() | ||
| test <@ reader.GetValue("ARGU_TEST_PLAIN") = "value" @>) | ||
Uh oh!
There was an error while loading. Please reload this page.