From 697ee85c282b704755786bff20f17fb2af44a81e Mon Sep 17 00:00:00 2001 From: sillock1 Date: Sun, 21 Apr 2024 00:16:54 +0100 Subject: [PATCH] refactor: terse enum parsing syntax and renamed HttpOptions to ServerOptions --- .../ConfigFileProviderTest.cs | 4 +- .../ServiceFactoryFixture.cs | 2 +- .../{HttpOptions.cs => ServerOptions.cs} | 2 +- .../Configuration/ConfigFileProvider.cs | 64 ++++++------------- src/NzbDrone.Host.Test/ContainerFixture.cs | 2 +- src/NzbDrone.Host/Bootstrap.cs | 16 ++--- 6 files changed, 34 insertions(+), 56 deletions(-) rename src/NzbDrone.Common/Options/{HttpOptions.cs => ServerOptions.cs} (92%) diff --git a/src/NzbDrone.Common.Test/ConfigFileProviderTest.cs b/src/NzbDrone.Common.Test/ConfigFileProviderTest.cs index cc68799f2..b6fe45584 100644 --- a/src/NzbDrone.Common.Test/ConfigFileProviderTest.cs +++ b/src/NzbDrone.Common.Test/ConfigFileProviderTest.cs @@ -54,9 +54,9 @@ namespace NzbDrone.Common.Test .Setup(v => v.Value) .Returns(new AppOptions()); - Mocker.GetMock>() + Mocker.GetMock>() .Setup(v => v.Value) - .Returns(new HttpOptions()); + .Returns(new ServerOptions()); Mocker.GetMock>() .Setup(v => v.Value) diff --git a/src/NzbDrone.Common.Test/ServiceFactoryFixture.cs b/src/NzbDrone.Common.Test/ServiceFactoryFixture.cs index b9520d725..6749006e5 100644 --- a/src/NzbDrone.Common.Test/ServiceFactoryFixture.cs +++ b/src/NzbDrone.Common.Test/ServiceFactoryFixture.cs @@ -36,7 +36,7 @@ namespace NzbDrone.Common.Test container.RegisterInstance(new Mock>().Object); container.RegisterInstance(new Mock>().Object); container.RegisterInstance(new Mock>().Object); - container.RegisterInstance(new Mock>().Object); + container.RegisterInstance(new Mock>().Object); container.RegisterInstance(new Mock>().Object); container.RegisterInstance(new Mock>().Object); diff --git a/src/NzbDrone.Common/Options/HttpOptions.cs b/src/NzbDrone.Common/Options/ServerOptions.cs similarity index 92% rename from src/NzbDrone.Common/Options/HttpOptions.cs rename to src/NzbDrone.Common/Options/ServerOptions.cs index 4f0dfd280..d21e12b2a 100644 --- a/src/NzbDrone.Common/Options/HttpOptions.cs +++ b/src/NzbDrone.Common/Options/ServerOptions.cs @@ -1,6 +1,6 @@ namespace NzbDrone.Common.Options; -public class HttpOptions +public class ServerOptions { public string UrlBase { get; set; } public string BindAddress { get; set; } diff --git a/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs b/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs index 29070047e..e1168c2fb 100644 --- a/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs +++ b/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs @@ -73,7 +73,7 @@ namespace NzbDrone.Core.Configuration private readonly PostgresOptions _postgresOptions; private readonly AuthOptions _authOptions; private readonly AppOptions _appOptions; - private readonly HttpOptions _httpOptions; + private readonly ServerOptions _serverOptions; private readonly UpdateOptions _updateOptions; private readonly LogOptions _logOptions; @@ -89,7 +89,7 @@ namespace NzbDrone.Core.Configuration IOptions postgresOptions, IOptions authOptions, IOptions appOptions, - IOptions httpOptions, + IOptions serverOptions, IOptions updateOptions, IOptions logOptions) { @@ -100,7 +100,7 @@ namespace NzbDrone.Core.Configuration _postgresOptions = postgresOptions.Value; _authOptions = authOptions.Value; _appOptions = appOptions.Value; - _httpOptions = httpOptions.Value; + _serverOptions = serverOptions.Value; _updateOptions = updateOptions.Value; _logOptions = logOptions.Value; } @@ -158,7 +158,7 @@ namespace NzbDrone.Core.Configuration { const string defaultValue = "*"; - var bindAddress = _httpOptions.BindAddress ?? GetValue("BindAddress", defaultValue); + var bindAddress = _serverOptions.BindAddress ?? GetValue("BindAddress", defaultValue); if (string.IsNullOrWhiteSpace(bindAddress)) { return defaultValue; @@ -168,11 +168,11 @@ namespace NzbDrone.Core.Configuration } } - public int Port => _httpOptions.Port ?? GetValueInt("Port", 8989); + public int Port => _serverOptions.Port ?? GetValueInt("Port", 8989); - public int SslPort => _httpOptions.SslPort ?? GetValueInt("SslPort", 9898); + public int SslPort => _serverOptions.SslPort ?? GetValueInt("SslPort", 9898); - public bool EnableSsl => _httpOptions.EnableSsl ?? GetValueBoolean("EnableSsl", false); + public bool EnableSsl => _serverOptions.EnableSsl ?? GetValueBoolean("EnableSsl", false); public bool LaunchBrowser => _appOptions.LaunchBrowser ?? GetValueBoolean("LaunchBrowser", true); @@ -204,29 +204,16 @@ namespace NzbDrone.Core.Configuration return AuthenticationType.Basic; } - var parsed = Enum.TryParse(_authOptions.Method, out var enumValue); - if (parsed) - { - return enumValue; - } - - return GetValueEnum("AuthenticationMethod", AuthenticationType.None); + return Enum.TryParse(_authOptions.Method, out var enumValue) + ? enumValue + : GetValueEnum("AuthenticationMethod", AuthenticationType.None); } } - public AuthenticationRequiredType AuthenticationRequired - { - get - { - var parsed = Enum.TryParse(_authOptions.Required, out var enumValue); - if (parsed) - { - return enumValue; - } - - return GetValueEnum("AuthenticationRequired", AuthenticationRequiredType.Enabled); - } - } + public AuthenticationRequiredType AuthenticationRequired => + Enum.TryParse(_authOptions.Required, out var enumValue) + ? enumValue + : GetValueEnum("AuthenticationRequired", AuthenticationRequiredType.Enabled); public bool AnalyticsEnabled => _logOptions.AnalyticsEnabled ?? GetValueBoolean("AnalyticsEnabled", true, persist: false); @@ -247,14 +234,14 @@ namespace NzbDrone.Core.Configuration public bool LogSql => _logOptions.Sql ?? GetValueBoolean("LogSql", false, persist: false); public int LogRotate => _logOptions.Rotate ?? GetValueInt("LogRotate", 50, persist: false); public bool FilterSentryEvents => _logOptions.FilterSentryEvents ?? GetValueBoolean("FilterSentryEvents", true, persist: false); - public string SslCertPath => _httpOptions.SslCertPath ?? GetValue("SslCertPath", ""); - public string SslCertPassword => _httpOptions.SslCertPassword ?? GetValue("SslCertPassword", ""); + public string SslCertPath => _serverOptions.SslCertPath ?? GetValue("SslCertPath", ""); + public string SslCertPassword => _serverOptions.SslCertPassword ?? GetValue("SslCertPassword", ""); public string UrlBase { get { - var urlBase = _httpOptions.UrlBase ?? GetValue("UrlBase", "").Trim('/'); + var urlBase = _serverOptions.UrlBase ?? GetValue("UrlBase", "").Trim('/'); if (urlBase.IsNullOrWhiteSpace()) { @@ -284,19 +271,10 @@ namespace NzbDrone.Core.Configuration public bool UpdateAutomatically => _updateOptions.Automatically ?? GetValueBoolean("UpdateAutomatically", false, false); - public UpdateMechanism UpdateMechanism - { - get - { - var isParsed = Enum.TryParse(_updateOptions.Mechanism, out var enumValue); - if (isParsed) - { - return enumValue; - } - - return GetValueEnum("UpdateMechanism", UpdateMechanism.BuiltIn, false); - } - } + public UpdateMechanism UpdateMechanism => + Enum.TryParse(_updateOptions.Mechanism, out var enumValue) + ? enumValue + : GetValueEnum("UpdateMechanism", UpdateMechanism.BuiltIn, false); public string UpdateScriptPath => _updateOptions.ScriptPath ?? GetValue("UpdateScriptPath", "", false); diff --git a/src/NzbDrone.Host.Test/ContainerFixture.cs b/src/NzbDrone.Host.Test/ContainerFixture.cs index d95a2427a..8279a18f0 100644 --- a/src/NzbDrone.Host.Test/ContainerFixture.cs +++ b/src/NzbDrone.Host.Test/ContainerFixture.cs @@ -49,7 +49,7 @@ namespace NzbDrone.App.Test container.RegisterInstance>(new Mock>().Object); container.RegisterInstance>(new Mock>().Object); container.RegisterInstance>(new Mock>().Object); - container.RegisterInstance>(new Mock>().Object); + container.RegisterInstance>(new Mock>().Object); container.RegisterInstance>(new Mock>().Object); container.RegisterInstance>(new Mock>().Object); diff --git a/src/NzbDrone.Host/Bootstrap.cs b/src/NzbDrone.Host/Bootstrap.cs index c5c7ee5be..2073eb622 100644 --- a/src/NzbDrone.Host/Bootstrap.cs +++ b/src/NzbDrone.Host/Bootstrap.cs @@ -101,7 +101,7 @@ namespace NzbDrone.Host services.Configure(config.GetSection("Sonarr:Postgres")); services.Configure(config.GetSection("Sonarr:App")); services.Configure(config.GetSection("Sonarr:Auth")); - services.Configure(config.GetSection("Sonarr:Http")); + services.Configure(config.GetSection("Sonarr:Server")); services.Configure(config.GetSection("Sonarr:Log")); services.Configure(config.GetSection("Sonarr:Update")); }).Build(); @@ -125,12 +125,12 @@ namespace NzbDrone.Host { var config = GetConfiguration(context); - var bindAddress = config.GetValue($"Sonarr:Http:{nameof(HttpOptions.BindAddress)}") ?? config.GetValue(nameof(ConfigFileProvider.BindAddress), "*"); - var port = config.GetValue($"Sonarr:Http:{nameof(HttpOptions.Port)}") ?? config.GetValue(nameof(ConfigFileProvider.Port), 8989); - var sslPort = config.GetValue($"Sonarr:Http:{nameof(HttpOptions.SslPort)}") ?? config.GetValue(nameof(ConfigFileProvider.SslPort), 9898); - var enableSsl = config.GetValue($"Sonarr:Http:{nameof(HttpOptions.EnableSsl)}") ?? config.GetValue(nameof(ConfigFileProvider.EnableSsl), false); - var sslCertPath = config.GetValue($"Sonarr:Http:{nameof(HttpOptions.SslCertPath)}") ?? config.GetValue(nameof(ConfigFileProvider.SslCertPath)); - var sslCertPassword = config.GetValue($"Sonarr:Http:{nameof(HttpOptions.SslCertPassword)}") ?? config.GetValue(nameof(ConfigFileProvider.SslCertPassword)); + var bindAddress = config.GetValue($"Sonarr:Server:{nameof(ServerOptions.BindAddress)}") ?? config.GetValue(nameof(ConfigFileProvider.BindAddress), "*"); + var port = config.GetValue($"Sonarr:Server:{nameof(ServerOptions.Port)}") ?? config.GetValue(nameof(ConfigFileProvider.Port), 8989); + var sslPort = config.GetValue($"Sonarr:Server:{nameof(ServerOptions.SslPort)}") ?? config.GetValue(nameof(ConfigFileProvider.SslPort), 9898); + var enableSsl = config.GetValue($"Sonarr:Server:{nameof(ServerOptions.EnableSsl)}") ?? config.GetValue(nameof(ConfigFileProvider.EnableSsl), false); + var sslCertPath = config.GetValue($"Sonarr:Server:{nameof(ServerOptions.SslCertPath)}") ?? config.GetValue(nameof(ConfigFileProvider.SslCertPath)); + var sslCertPassword = config.GetValue($"Sonarr:Server:{nameof(ServerOptions.SslCertPassword)}") ?? config.GetValue(nameof(ConfigFileProvider.SslCertPassword)); var urls = new List { BuildUrl("http", bindAddress, port) }; @@ -160,7 +160,7 @@ namespace NzbDrone.Host services.Configure(config.GetSection("Sonarr:Postgres")); services.Configure(config.GetSection("Sonarr:App")); services.Configure(config.GetSection("Sonarr:Auth")); - services.Configure(config.GetSection("Sonarr:Http")); + services.Configure(config.GetSection("Sonarr:Server")); services.Configure(config.GetSection("Sonarr:Log")); services.Configure(config.GetSection("Sonarr:Update")); })