-
Notifications
You must be signed in to change notification settings - Fork 76
Extract Argu.Extensions.ConfigurationManager #234
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
base: master
Are you sure you want to change the base?
Changes from all commits
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,22 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>netstandard2.0</TargetFramework> | ||
| <GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="Library.fs" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="DotNet.ReproducibleBuilds" PrivateAssets="All" /> | ||
| <PackageReference Include="FSharp.Core" /> | ||
| <PackageReference Include="System.Configuration.ConfigurationManager" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Argu\Argu.fsproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| namespace Argu | ||
|
|
||
| open System.Configuration | ||
| open System.IO | ||
|
|
||
| /// AppSettings XML configuration reader | ||
| type AppSettingsConfigurationReader () = | ||
| interface IConfigurationReader with | ||
| member _.Name = "AppSettings configuration reader" | ||
| member _.GetValue(key : string) = ConfigurationManager.AppSettings[key] | ||
|
|
||
| /// AppSettings XML configuration reader | ||
| type AppSettingsConfigurationFileReader private (xmlPath : string, kv : KeyValueConfigurationCollection) = | ||
| member _.Path = xmlPath | ||
| interface IConfigurationReader with | ||
| member _.Name = $"App.config configuration reader: %s{xmlPath}" | ||
| member _.GetValue(key : string) = | ||
| match kv[key] with | ||
| | null -> null | ||
| | entry -> entry.Value | ||
|
|
||
| /// Create used supplied XML file path | ||
| static member Create(path : string) = | ||
| if not <| File.Exists path then raise <| FileNotFoundException(path) | ||
| let fileMap = ExeConfigurationFileMap(ExeConfigFilename = path) | ||
| let config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None) | ||
| AppSettingsConfigurationFileReader(path, config.AppSettings.Settings) | ||
|
|
||
| [<AutoOpen>] | ||
| module ConfigurationReaderExtensions = | ||
| open System.Reflection | ||
| open System | ||
|
|
||
| /// Configuration reader implementations | ||
| type ConfigurationReader with | ||
|
|
||
| /// Create a configuration reader instance using the application's resident AppSettings configuration | ||
| static member FromAppSettings() : IConfigurationReader = AppSettingsConfigurationReader() | ||
|
|
||
| /// Create a configuration reader instance using a local xml App.Config file | ||
| static member FromAppSettingsFile(path : string) : IConfigurationReader = AppSettingsConfigurationFileReader.Create path | ||
|
|
||
| /// Create a configuration reader instance using the location of an assembly file | ||
| static member FromAppSettings(assembly : Assembly) : IConfigurationReader = | ||
| let path = assembly.Location | ||
| if String.IsNullOrEmpty path then | ||
| invalidArg assembly.FullName $"Assembly location for '{assembly.Location}' is null or empty." | ||
|
|
||
| AppSettingsConfigurationFileReader.Create(path + ".config") :> IConfigurationReader | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -229,7 +229,7 @@ and [<Sealed; NoEquality; NoComparison; AutoSerializable(false)>] | |
| let configurationReader = | ||
| match configurationReader with | ||
| | Some c -> c | ||
| | None -> ConfigurationReader.FromAppSettings() | ||
| | None -> ConfigurationReader.NullReader | ||
|
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 better to simply make reader mandatory otherwise people need to read the docs to discover the semantic change which is a killer for something like this where the people that picked Argu are long gone, the app people don't care, and an LLM or infra specialist is doing dependency updates (and as a followup also remove argv defaulting) |
||
|
|
||
| try | ||
| let appSettingsResults = parseKeyValueConfig configurationReader argInfo | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dont think there's a benefit to this layering - type augmentation in namespace should be good?