aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-02-10 21:14:33 -0600
committerJP Dillingham <jpdillingham@gmail.com>2021-02-10 21:14:33 -0600
commit62b44e06b3f311970e8835d647c97de5498f394e (patch)
treeb5092784b31eb056c3159899cccf2eab776a1bc4 /src
parentb82b594008e1f8bd331384d50d4a4aa31ef61045 (diff)
downloadslskd-62b44e06b3f311970e8835d647c97de5498f394e.tar.gz
simplify option mapping
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Common/Configuration/CommandLineConfigurationSource.cs21
-rw-r--r--src/slskd/Common/Configuration/ConfigurationExtensions.cs49
-rw-r--r--src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs19
-rw-r--r--src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs21
-rw-r--r--src/slskd/Program.cs10
5 files changed, 25 insertions, 95 deletions
diff --git a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs
index 088d5ea6..417e5729 100644
--- a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs
+++ b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs
@@ -24,11 +24,6 @@ namespace slskd.Configuration
using Utility.CommandLine;
/// <summary>
- /// Defines a command line argument mapping.
- /// </summary>
- public record CommandLineArgument(char ShortName, string LongName, Type Type, string Key, string Description = null);
-
- /// <summary>
/// Extension methods for adding <see cref="CommandLineConfigurationProvider"/>.
/// </summary>
public static class CommandLineConfigurationExtensions
@@ -40,7 +35,7 @@ namespace slskd.Configuration
/// <param name="map">A list of command line argument mappings.</param>
/// <param name="commandLine">The command line string from which to parse arguments.</param>
/// <returns>The updated <see cref="IConfigurationBuilder"/>.</returns>
- public static IConfigurationBuilder AddCommandLine(this IConfigurationBuilder builder, IEnumerable<CommandLineArgument> map, string commandLine = null)
+ public static IConfigurationBuilder AddCommandLine(this IConfigurationBuilder builder, IEnumerable<Option> map, string commandLine = null)
{
if (builder == null)
{
@@ -80,7 +75,7 @@ namespace slskd.Configuration
}
private string CommandLine { get; set; }
- private IEnumerable<CommandLineArgument> Map { get; set; }
+ private IEnumerable<Option> Map { get; set; }
/// <summary>
/// Parses command line arguments from the specified string and maps them to the specified keys.
@@ -89,16 +84,14 @@ namespace slskd.Configuration
{
var dictionary = Arguments.Parse(CommandLine).ArgumentDictionary;
- foreach (CommandLineArgument item in Map)
+ foreach (Option item in Map)
{
- var (shortName, longName, type, key, _) = item;
-
- if (string.IsNullOrEmpty(key))
+ if (string.IsNullOrEmpty(item.Key))
{
continue;
}
- var arguments = new[] { shortName.ToString(), longName }.Where(i => !string.IsNullOrEmpty(i));
+ var arguments = new[] { item.ShortName.ToString(), item.LongName }.Where(i => !string.IsNullOrEmpty(i));
foreach (var argument in arguments)
{
@@ -106,7 +99,7 @@ namespace slskd.Configuration
{
var value = dictionary[argument].ToString();
- if (type == typeof(bool) && string.IsNullOrEmpty(value))
+ if (item.Type == typeof(bool) && string.IsNullOrEmpty(value))
{
value = "true";
}
@@ -131,7 +124,7 @@ namespace slskd.Configuration
/// <summary>
/// Gets or sets a list of command line argument mappings.
/// </summary>
- public IEnumerable<CommandLineArgument> Map { get; set; }
+ public IEnumerable<Option> Map { get; set; }
/// <summary>
/// Builds the <see cref="CommandLineConfigurationProvider"/> for this source.
diff --git a/src/slskd/Common/Configuration/ConfigurationExtensions.cs b/src/slskd/Common/Configuration/ConfigurationExtensions.cs
deleted file mode 100644
index 33385e0a..00000000
--- a/src/slskd/Common/Configuration/ConfigurationExtensions.cs
+++ /dev/null
@@ -1,49 +0,0 @@
-// <copyright file="ConfigurationExtensions.cs" company="slskd Team">
-// Copyright (c) slskd Team. All rights reserved.
-//
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU Affero General Public License as published
-// by the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU Affero General Public License for more details.
-//
-// You should have received a copy of the GNU Affero General Public License
-// along with this program. If not, see https://www.gnu.org/licenses/.
-// </copyright>
-
-namespace slskd.Configuration
-{
- /// <summary>
- /// Extension methods for the Configuration namespace.
- /// </summary>
- public static class ConfigurationExtensions
- {
- /// <summary>
- /// Converts an <see cref="Option"/> mapping to a <see cref="CommandLineArgument"/> mapping.
- /// </summary>
- /// <param name="option">The option mapping to convert.</param>
- /// <returns>The converted mapping.</returns>
- public static CommandLineArgument ToCommandLineArgument(this Option option)
- => new(option.ShortName, option.LongName, option.Type, option.Key, option.Description);
-
- /// <summary>
- /// Converts an <see cref="Option"/> mapping to a <see cref="EnvironmentVariable"/> mapping.
- /// </summary>
- /// <param name="option">The option mapping to convert.</param>
- /// <returns>The converted mapping.</returns>
- public static EnvironmentVariable ToEnvironmentVariable(this Option option)
- => new(option.EnvironmentVariable, option.Type, option.Key, option.Description);
-
- /// <summary>
- /// Converts an <see cref="Option"/> mapping to a <see cref="DefaultValue"/> mapping.
- /// </summary>
- /// <param name="option">The option mapping to convert.</param>
- /// <returns>The converted mapping.</returns>
- public static DefaultValue ToDefaultValue(this Option option)
- => new(option.Key, option.Type, option.Default);
- }
-}
diff --git a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs
index 07e4f1b7..32a87616 100644
--- a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs
+++ b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs
@@ -23,11 +23,6 @@ namespace slskd.Configuration
using Microsoft.Extensions.Configuration;
/// <summary>
- /// Defines a default value mapping.
- /// </summary>
- public record DefaultValue(string Key, Type Type, object Default);
-
- /// <summary>
/// Extension methods for adding <see cref="DefaultValueConfigurationProvider"/>.
/// </summary>
public static class DefaultValueConfigurationExtensions
@@ -38,7 +33,7 @@ namespace slskd.Configuration
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to which to add.</param>
/// <param name="map">A list of default value mappings.</param>
/// <returns>The updated <see cref="IConfigurationBuilder"/>.</returns>
- public static IConfigurationBuilder AddDefaultValues(this IConfigurationBuilder builder, IEnumerable<DefaultValue> map)
+ public static IConfigurationBuilder AddDefaultValues(this IConfigurationBuilder builder, IEnumerable<Option> map)
{
if (builder == null)
{
@@ -47,7 +42,7 @@ namespace slskd.Configuration
return builder.AddDefaultValues(s =>
{
- s.Map = map ?? Enumerable.Empty<DefaultValue>();
+ s.Map = map ?? Enumerable.Empty<Option>();
});
}
@@ -75,7 +70,7 @@ namespace slskd.Configuration
Map = source.Map;
}
- private IEnumerable<DefaultValue> Map { get; set; }
+ private IEnumerable<Option> Map { get; set; }
/// <summary>
/// Sets the keys specified in the map to the specified default values.
@@ -84,14 +79,12 @@ namespace slskd.Configuration
{
foreach (var item in Map)
{
- var (key, _, value) = item;
-
- if (string.IsNullOrEmpty(key) || value == null)
+ if (string.IsNullOrEmpty(item.Key) || item.Default == null)
{
continue;
}
- Data[item.Key] = value.ToString();
+ Data[item.Key] = item.Default.ToString();
}
}
}
@@ -104,7 +97,7 @@ namespace slskd.Configuration
/// <summary>
/// Gets or sets a list of default value mappings.
/// </summary>
- public IEnumerable<DefaultValue> Map { get; set; }
+ public IEnumerable<Option> Map { get; set; }
/// <summary>
/// Builds the <see cref="DefaultValueConfigurationProvider"/> for this source.
diff --git a/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs b/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs
index 21775689..3a77ee9c 100644
--- a/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs
+++ b/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs
@@ -23,11 +23,6 @@ namespace slskd.Configuration
using Microsoft.Extensions.Configuration;
/// <summary>
- /// Defines an environment variable mapping.
- /// </summary>
- public record EnvironmentVariable(string Name, Type Type, string Key, string Description = null);
-
- /// <summary>
/// Extension methods for adding <see cref="EnvironmentVariableConfigurationProvider"/>.
/// </summary>
public static class EnvironmentVariableConfigurationExtensions
@@ -39,7 +34,7 @@ namespace slskd.Configuration
/// <param name="map">A list of environment variable mappings.</param>
/// <param name="prefix">A prefix to prepend to all variable names.</param>
/// <returns>The updated <see cref="IConfigurationBuilder"/>.</returns>
- public static IConfigurationBuilder AddEnvironmentVariables(this IConfigurationBuilder builder, IEnumerable<EnvironmentVariable> map, string prefix = null)
+ public static IConfigurationBuilder AddEnvironmentVariables(this IConfigurationBuilder builder, IEnumerable<Option> map, string prefix = null)
{
if (builder == null)
{
@@ -48,7 +43,7 @@ namespace slskd.Configuration
return builder.AddEnvironmentVariables(s =>
{
- s.Map = map ?? Enumerable.Empty<EnvironmentVariable>();
+ s.Map = map ?? Enumerable.Empty<Option>();
s.Prefix = prefix ?? string.Empty;
});
}
@@ -78,7 +73,7 @@ namespace slskd.Configuration
Prefix = source.Prefix;
}
- private IEnumerable<EnvironmentVariable> Map { get; set; }
+ private IEnumerable<Option> Map { get; set; }
private string Prefix { get; set; }
/// <summary>
@@ -88,21 +83,19 @@ namespace slskd.Configuration
{
foreach (var item in Map)
{
- var (name, type, key, _) = item;
-
- if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(name))
+ if (string.IsNullOrEmpty(item.Key) || string.IsNullOrEmpty(item.EnvironmentVariable))
{
continue;
}
- var value = Environment.GetEnvironmentVariable(Prefix + name);
+ var value = Environment.GetEnvironmentVariable(Prefix + item.EnvironmentVariable);
if (string.IsNullOrEmpty(value))
{
continue;
}
- if (type == typeof(bool))
+ if (item.Type == typeof(bool))
{
value = value.Equals("true", StringComparison.InvariantCultureIgnoreCase) ? value : "false";
}
@@ -120,7 +113,7 @@ namespace slskd.Configuration
/// <summary>
/// Gets or sets a list of enviroment variable mappings.
/// </summary>
- public IEnumerable<EnvironmentVariable> Map { get; set; }
+ public IEnumerable<Option> Map { get; set; }
/// <summary>
/// Gets or sets a value indicating whether configuration keys should be normalized (_, - removed, changed to lowercase).
diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs
index 4621abf5..834c35a9 100644
--- a/src/slskd/Program.cs
+++ b/src/slskd/Program.cs
@@ -161,7 +161,7 @@ namespace slskd
outputTemplate: "[{SourceContext}] [{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
.WriteTo.Async(config =>
config.File(
- "logs/slskd-.log",
+ $"logs/{AppName}-.log",
outputTemplate: "[{SourceContext}] [{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}",
rollingInterval: RollingInterval.Day))
.WriteTo.Conditional(
@@ -186,7 +186,7 @@ namespace slskd
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
.WriteTo.Async(config =>
config.File(
- "logs/slskd-.log",
+ $"logs/{AppName}-.log",
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}",
rollingInterval: RollingInterval.Day))
.WriteTo.Conditional(
@@ -272,10 +272,10 @@ namespace slskd
return builder
.AddDefaultValues(
- map: Options.Map.Select(o => o.ToDefaultValue()))
+ map: Options.Map)
.AddEnvironmentVariables(
prefix: environmentVariablePrefix,
- map: Options.Map.Select(o => o.ToEnvironmentVariable()))
+ map: Options.Map)
.AddYamlFile(
path: Path.GetFileName(configurationFile),
optional: true,
@@ -283,7 +283,7 @@ namespace slskd
provider: new PhysicalFileProvider(Path.GetDirectoryName(configurationFile), ExclusionFilters.None))
.AddCommandLine(
commandLine: Environment.CommandLine,
- map: Options.Map.Select(o => o.ToCommandLineArgument()));
+ map: Options.Map);
}
private static void GenerateX509Certificate(string password, string filename)