refactor: terse enum parsing syntax and renamed HttpOptions to ServerOptions

This commit is contained in:
sillock1 2024-04-21 00:16:54 +01:00
parent d189d84515
commit 697ee85c28
6 changed files with 34 additions and 56 deletions

View File

@ -54,9 +54,9 @@ namespace NzbDrone.Common.Test
.Setup(v => v.Value) .Setup(v => v.Value)
.Returns(new AppOptions()); .Returns(new AppOptions());
Mocker.GetMock<IOptions<HttpOptions>>() Mocker.GetMock<IOptions<ServerOptions>>()
.Setup(v => v.Value) .Setup(v => v.Value)
.Returns(new HttpOptions()); .Returns(new ServerOptions());
Mocker.GetMock<IOptions<LogOptions>>() Mocker.GetMock<IOptions<LogOptions>>()
.Setup(v => v.Value) .Setup(v => v.Value)

View File

@ -36,7 +36,7 @@ namespace NzbDrone.Common.Test
container.RegisterInstance(new Mock<IOptions<PostgresOptions>>().Object); container.RegisterInstance(new Mock<IOptions<PostgresOptions>>().Object);
container.RegisterInstance(new Mock<IOptions<AppOptions>>().Object); container.RegisterInstance(new Mock<IOptions<AppOptions>>().Object);
container.RegisterInstance(new Mock<IOptions<AuthOptions>>().Object); container.RegisterInstance(new Mock<IOptions<AuthOptions>>().Object);
container.RegisterInstance(new Mock<IOptions<HttpOptions>>().Object); container.RegisterInstance(new Mock<IOptions<ServerOptions>>().Object);
container.RegisterInstance(new Mock<IOptions<LogOptions>>().Object); container.RegisterInstance(new Mock<IOptions<LogOptions>>().Object);
container.RegisterInstance(new Mock<IOptions<UpdateOptions>>().Object); container.RegisterInstance(new Mock<IOptions<UpdateOptions>>().Object);

View File

@ -1,6 +1,6 @@
namespace NzbDrone.Common.Options; namespace NzbDrone.Common.Options;
public class HttpOptions public class ServerOptions
{ {
public string UrlBase { get; set; } public string UrlBase { get; set; }
public string BindAddress { get; set; } public string BindAddress { get; set; }

View File

@ -73,7 +73,7 @@ namespace NzbDrone.Core.Configuration
private readonly PostgresOptions _postgresOptions; private readonly PostgresOptions _postgresOptions;
private readonly AuthOptions _authOptions; private readonly AuthOptions _authOptions;
private readonly AppOptions _appOptions; private readonly AppOptions _appOptions;
private readonly HttpOptions _httpOptions; private readonly ServerOptions _serverOptions;
private readonly UpdateOptions _updateOptions; private readonly UpdateOptions _updateOptions;
private readonly LogOptions _logOptions; private readonly LogOptions _logOptions;
@ -89,7 +89,7 @@ namespace NzbDrone.Core.Configuration
IOptions<PostgresOptions> postgresOptions, IOptions<PostgresOptions> postgresOptions,
IOptions<AuthOptions> authOptions, IOptions<AuthOptions> authOptions,
IOptions<AppOptions> appOptions, IOptions<AppOptions> appOptions,
IOptions<HttpOptions> httpOptions, IOptions<ServerOptions> serverOptions,
IOptions<UpdateOptions> updateOptions, IOptions<UpdateOptions> updateOptions,
IOptions<LogOptions> logOptions) IOptions<LogOptions> logOptions)
{ {
@ -100,7 +100,7 @@ namespace NzbDrone.Core.Configuration
_postgresOptions = postgresOptions.Value; _postgresOptions = postgresOptions.Value;
_authOptions = authOptions.Value; _authOptions = authOptions.Value;
_appOptions = appOptions.Value; _appOptions = appOptions.Value;
_httpOptions = httpOptions.Value; _serverOptions = serverOptions.Value;
_updateOptions = updateOptions.Value; _updateOptions = updateOptions.Value;
_logOptions = logOptions.Value; _logOptions = logOptions.Value;
} }
@ -158,7 +158,7 @@ namespace NzbDrone.Core.Configuration
{ {
const string defaultValue = "*"; const string defaultValue = "*";
var bindAddress = _httpOptions.BindAddress ?? GetValue("BindAddress", defaultValue); var bindAddress = _serverOptions.BindAddress ?? GetValue("BindAddress", defaultValue);
if (string.IsNullOrWhiteSpace(bindAddress)) if (string.IsNullOrWhiteSpace(bindAddress))
{ {
return defaultValue; 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); public bool LaunchBrowser => _appOptions.LaunchBrowser ?? GetValueBoolean("LaunchBrowser", true);
@ -204,29 +204,16 @@ namespace NzbDrone.Core.Configuration
return AuthenticationType.Basic; return AuthenticationType.Basic;
} }
var parsed = Enum.TryParse<AuthenticationType>(_authOptions.Method, out var enumValue); return Enum.TryParse<AuthenticationType>(_authOptions.Method, out var enumValue)
if (parsed) ? enumValue
{ : GetValueEnum("AuthenticationMethod", AuthenticationType.None);
return enumValue;
}
return GetValueEnum("AuthenticationMethod", AuthenticationType.None);
} }
} }
public AuthenticationRequiredType AuthenticationRequired public AuthenticationRequiredType AuthenticationRequired =>
{ Enum.TryParse<AuthenticationRequiredType>(_authOptions.Required, out var enumValue)
get ? enumValue
{ : GetValueEnum("AuthenticationRequired", AuthenticationRequiredType.Enabled);
var parsed = Enum.TryParse<AuthenticationRequiredType>(_authOptions.Required, out var enumValue);
if (parsed)
{
return enumValue;
}
return GetValueEnum("AuthenticationRequired", AuthenticationRequiredType.Enabled);
}
}
public bool AnalyticsEnabled => _logOptions.AnalyticsEnabled ?? GetValueBoolean("AnalyticsEnabled", true, persist: false); 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 bool LogSql => _logOptions.Sql ?? GetValueBoolean("LogSql", false, persist: false);
public int LogRotate => _logOptions.Rotate ?? GetValueInt("LogRotate", 50, persist: false); public int LogRotate => _logOptions.Rotate ?? GetValueInt("LogRotate", 50, persist: false);
public bool FilterSentryEvents => _logOptions.FilterSentryEvents ?? GetValueBoolean("FilterSentryEvents", true, persist: false); public bool FilterSentryEvents => _logOptions.FilterSentryEvents ?? GetValueBoolean("FilterSentryEvents", true, persist: false);
public string SslCertPath => _httpOptions.SslCertPath ?? GetValue("SslCertPath", ""); public string SslCertPath => _serverOptions.SslCertPath ?? GetValue("SslCertPath", "");
public string SslCertPassword => _httpOptions.SslCertPassword ?? GetValue("SslCertPassword", ""); public string SslCertPassword => _serverOptions.SslCertPassword ?? GetValue("SslCertPassword", "");
public string UrlBase public string UrlBase
{ {
get get
{ {
var urlBase = _httpOptions.UrlBase ?? GetValue("UrlBase", "").Trim('/'); var urlBase = _serverOptions.UrlBase ?? GetValue("UrlBase", "").Trim('/');
if (urlBase.IsNullOrWhiteSpace()) if (urlBase.IsNullOrWhiteSpace())
{ {
@ -284,19 +271,10 @@ namespace NzbDrone.Core.Configuration
public bool UpdateAutomatically => _updateOptions.Automatically ?? GetValueBoolean("UpdateAutomatically", false, false); public bool UpdateAutomatically => _updateOptions.Automatically ?? GetValueBoolean("UpdateAutomatically", false, false);
public UpdateMechanism UpdateMechanism public UpdateMechanism UpdateMechanism =>
{ Enum.TryParse<UpdateMechanism>(_updateOptions.Mechanism, out var enumValue)
get ? enumValue
{ : GetValueEnum("UpdateMechanism", UpdateMechanism.BuiltIn, false);
var isParsed = Enum.TryParse<UpdateMechanism>(_updateOptions.Mechanism, out var enumValue);
if (isParsed)
{
return enumValue;
}
return GetValueEnum("UpdateMechanism", UpdateMechanism.BuiltIn, false);
}
}
public string UpdateScriptPath => _updateOptions.ScriptPath ?? GetValue("UpdateScriptPath", "", false); public string UpdateScriptPath => _updateOptions.ScriptPath ?? GetValue("UpdateScriptPath", "", false);

View File

@ -49,7 +49,7 @@ namespace NzbDrone.App.Test
container.RegisterInstance<IOptions<PostgresOptions>>(new Mock<IOptions<PostgresOptions>>().Object); container.RegisterInstance<IOptions<PostgresOptions>>(new Mock<IOptions<PostgresOptions>>().Object);
container.RegisterInstance<IOptions<AuthOptions>>(new Mock<IOptions<AuthOptions>>().Object); container.RegisterInstance<IOptions<AuthOptions>>(new Mock<IOptions<AuthOptions>>().Object);
container.RegisterInstance<IOptions<AppOptions>>(new Mock<IOptions<AppOptions>>().Object); container.RegisterInstance<IOptions<AppOptions>>(new Mock<IOptions<AppOptions>>().Object);
container.RegisterInstance<IOptions<HttpOptions>>(new Mock<IOptions<HttpOptions>>().Object); container.RegisterInstance<IOptions<ServerOptions>>(new Mock<IOptions<ServerOptions>>().Object);
container.RegisterInstance<IOptions<UpdateOptions>>(new Mock<IOptions<UpdateOptions>>().Object); container.RegisterInstance<IOptions<UpdateOptions>>(new Mock<IOptions<UpdateOptions>>().Object);
container.RegisterInstance<IOptions<LogOptions>>(new Mock<IOptions<LogOptions>>().Object); container.RegisterInstance<IOptions<LogOptions>>(new Mock<IOptions<LogOptions>>().Object);

View File

@ -101,7 +101,7 @@ namespace NzbDrone.Host
services.Configure<PostgresOptions>(config.GetSection("Sonarr:Postgres")); services.Configure<PostgresOptions>(config.GetSection("Sonarr:Postgres"));
services.Configure<AppOptions>(config.GetSection("Sonarr:App")); services.Configure<AppOptions>(config.GetSection("Sonarr:App"));
services.Configure<AuthOptions>(config.GetSection("Sonarr:Auth")); services.Configure<AuthOptions>(config.GetSection("Sonarr:Auth"));
services.Configure<HttpOptions>(config.GetSection("Sonarr:Http")); services.Configure<ServerOptions>(config.GetSection("Sonarr:Server"));
services.Configure<LogOptions>(config.GetSection("Sonarr:Log")); services.Configure<LogOptions>(config.GetSection("Sonarr:Log"));
services.Configure<UpdateOptions>(config.GetSection("Sonarr:Update")); services.Configure<UpdateOptions>(config.GetSection("Sonarr:Update"));
}).Build(); }).Build();
@ -125,12 +125,12 @@ namespace NzbDrone.Host
{ {
var config = GetConfiguration(context); var config = GetConfiguration(context);
var bindAddress = config.GetValue<string>($"Sonarr:Http:{nameof(HttpOptions.BindAddress)}") ?? config.GetValue(nameof(ConfigFileProvider.BindAddress), "*"); var bindAddress = config.GetValue<string>($"Sonarr:Server:{nameof(ServerOptions.BindAddress)}") ?? config.GetValue(nameof(ConfigFileProvider.BindAddress), "*");
var port = config.GetValue<int?>($"Sonarr:Http:{nameof(HttpOptions.Port)}") ?? config.GetValue(nameof(ConfigFileProvider.Port), 8989); var port = config.GetValue<int?>($"Sonarr:Server:{nameof(ServerOptions.Port)}") ?? config.GetValue(nameof(ConfigFileProvider.Port), 8989);
var sslPort = config.GetValue<int?>($"Sonarr:Http:{nameof(HttpOptions.SslPort)}") ?? config.GetValue(nameof(ConfigFileProvider.SslPort), 9898); var sslPort = config.GetValue<int?>($"Sonarr:Server:{nameof(ServerOptions.SslPort)}") ?? config.GetValue(nameof(ConfigFileProvider.SslPort), 9898);
var enableSsl = config.GetValue<bool?>($"Sonarr:Http:{nameof(HttpOptions.EnableSsl)}") ?? config.GetValue(nameof(ConfigFileProvider.EnableSsl), false); var enableSsl = config.GetValue<bool?>($"Sonarr:Server:{nameof(ServerOptions.EnableSsl)}") ?? config.GetValue(nameof(ConfigFileProvider.EnableSsl), false);
var sslCertPath = config.GetValue<string>($"Sonarr:Http:{nameof(HttpOptions.SslCertPath)}") ?? config.GetValue<string>(nameof(ConfigFileProvider.SslCertPath)); var sslCertPath = config.GetValue<string>($"Sonarr:Server:{nameof(ServerOptions.SslCertPath)}") ?? config.GetValue<string>(nameof(ConfigFileProvider.SslCertPath));
var sslCertPassword = config.GetValue<string>($"Sonarr:Http:{nameof(HttpOptions.SslCertPassword)}") ?? config.GetValue<string>(nameof(ConfigFileProvider.SslCertPassword)); var sslCertPassword = config.GetValue<string>($"Sonarr:Server:{nameof(ServerOptions.SslCertPassword)}") ?? config.GetValue<string>(nameof(ConfigFileProvider.SslCertPassword));
var urls = new List<string> { BuildUrl("http", bindAddress, port) }; var urls = new List<string> { BuildUrl("http", bindAddress, port) };
@ -160,7 +160,7 @@ namespace NzbDrone.Host
services.Configure<PostgresOptions>(config.GetSection("Sonarr:Postgres")); services.Configure<PostgresOptions>(config.GetSection("Sonarr:Postgres"));
services.Configure<AppOptions>(config.GetSection("Sonarr:App")); services.Configure<AppOptions>(config.GetSection("Sonarr:App"));
services.Configure<AuthOptions>(config.GetSection("Sonarr:Auth")); services.Configure<AuthOptions>(config.GetSection("Sonarr:Auth"));
services.Configure<HttpOptions>(config.GetSection("Sonarr:Http")); services.Configure<ServerOptions>(config.GetSection("Sonarr:Server"));
services.Configure<LogOptions>(config.GetSection("Sonarr:Log")); services.Configure<LogOptions>(config.GetSection("Sonarr:Log"));
services.Configure<UpdateOptions>(config.GetSection("Sonarr:Update")); services.Configure<UpdateOptions>(config.GetSection("Sonarr:Update"));
}) })