< Summary

Information
Class: ArturRios.Util.WebApi.Configuration.WebApiParameters
Assembly: ArturRios.Util.WebApi
File(s): D:\Repositories\dotnet-webapi-util\src\Configuration\WebApiParameters.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 45
Coverable lines: 45
Total lines: 92
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 26
Branch coverage: 0%
Method coverage
0%
Covered methods: 0
Fully covered methods: 0
Total methods: 7
Method coverage: 0%
Full method coverage: 0%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)0%506220%
get_EnvironmentName()100%210%
get_UseAppSettings()100%210%
get_UseEnvFile()100%210%
get_EnableSwaggerDocs()100%210%
get_SwaggerEnvironments()100%210%
GetSwaggerEnvironments()0%2040%

File(s)

D:\Repositories\dotnet-webapi-util\src\Configuration\WebApiParameters.cs

#LineLine coverage
 1using ArturRios.Configuration.Enums;
 2using ArturRios.Extensions;
 3
 4namespace ArturRios.Util.WebApi.Configuration;
 5
 6/// <summary>Startup parameters parsed from command-line arguments (e.g. <c>Environment:Production</c>,
 7/// <c>EnableSwaggerDocs:false</c>), controlling how <see cref="WebApiStartup"/> configures the application.</summary>
 8public class WebApiParameters
 9{
 010    private readonly string[] _defaultSwaggerEnvironments =
 011        [nameof(EnvironmentType.Development), nameof(EnvironmentType.Local)];
 12
 13    /// <summary>Parses <paramref name="args"/> into the corresponding properties. Unrecognized or malformed
 14    /// entries are ignored, leaving the default values in place.</summary>
 15    /// <param name="args">The command-line arguments, each in <c>Key:Value</c> form.</param>
 016    public WebApiParameters(string[] args)
 017    {
 018        if (args.IsEmpty())
 019        {
 020            return;
 21        }
 22
 023        foreach (var arg in args)
 024        {
 025            var parts = arg.Split(':', 2, StringSplitOptions.TrimEntries);
 26
 027            if (parts.Length != 2)
 028            {
 029                continue;
 30            }
 31
 032            var key = parts[0].Trim();
 033            var value = parts[1].Trim();
 34
 035            switch (key)
 36            {
 37                case "Environment":
 038                    EnvironmentName = value.IsValidEnumValue<EnvironmentType>() ? value : string.Empty;
 039                    break;
 40                case "EnableSwaggerDocs":
 041                    EnableSwaggerDocs = value.ParseToBoolOrDefault(true)!.Value;
 042                    break;
 43                case "UseAppSetting":
 044                    UseAppSettings = value.ParseToBoolOrDefault(true)!.Value;
 045                    break;
 46                case "UseEnvFile":
 047                    UseEnvFile = value.ParseToBoolOrDefault(true)!.Value;
 048                    break;
 49                case "SwaggerEnvironments":
 050                    if (value.StartsWith('[') && value.EndsWith(']'))
 051                    {
 052                        var envs = value.Trim('[', ']').Split(
 053                            ',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
 054                        SwaggerEnvironments = envs;
 055                    }
 56
 057                    break;
 58            }
 059        }
 060    }
 61
 62    /// <summary>The environment name (e.g. <c>Development</c>, <c>Production</c>), if a valid one was supplied.</summar
 063    public string EnvironmentName { get; set; } = string.Empty;
 64
 65    /// <summary>Whether <c>appsettings.json</c> should be loaded. Defaults to <c>true</c>.</summary>
 066    public bool UseAppSettings { get; set; } = true;
 67
 68    /// <summary>Whether a <c>.env</c> file should be loaded. Defaults to <c>true</c>.</summary>
 069    public bool UseEnvFile { get; set; } = true;
 70
 71    /// <summary>Parsed from the <c>EnableSwaggerDocs</c> argument. Reserved and not currently consulted by
 72    /// <see cref="WebApiStartup"/>; Swagger is gated by environment (see <see cref="SwaggerEnvironments"/> and
 73    /// <see cref="GetSwaggerEnvironments"/>). Defaults to <c>true</c>.</summary>
 074    public bool EnableSwaggerDocs { get; set; } = true;
 75
 76    /// <summary>The environment names in which Swagger should be enabled, as parsed from the <c>SwaggerEnvironments</c>
 077    public string[] SwaggerEnvironments { get; set; } = [];
 78
 79    /// <summary>Returns the configured Swagger environments, falling back to <c>Development</c> and <c>Local</c>
 80    /// when none were supplied or none are valid.</summary>
 81    public string[] GetSwaggerEnvironments()
 082    {
 083        if (SwaggerEnvironments.IsEmpty())
 084        {
 085            return _defaultSwaggerEnvironments;
 86        }
 87
 088        var validEnvs = SwaggerEnvironments.Where(env => env.IsValidEnumValue<EnvironmentType>()).ToArray();
 89
 090        return validEnvs.IsNotEmpty() ? validEnvs : _defaultSwaggerEnvironments;
 091    }
 92}