aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Common/Configuration
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-07-11 22:08:50 -0500
committerJP Dillingham <jpdillingham@gmail.com>2021-07-11 22:08:50 -0500
commitd8eb620c39da4d6ac61fd2d55babb2d73558c9e6 (patch)
tree0b788a308909cb3447181628e68f85cb103d6b0a /src/slskd/Common/Configuration
parentb5167888d9dd6bb74ac632e4559716c7ca433f0f (diff)
downloadslskd-d8eb620c39da4d6ac61fd2d55babb2d73558c9e6.tar.gz
rename Configuration namespace to ConfigurationSources
Diffstat (limited to 'src/slskd/Common/Configuration')
-rw-r--r--src/slskd/Common/Configuration/CommandLineConfigurationSource.cs157
-rw-r--r--src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs136
-rw-r--r--src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs151
-rw-r--r--src/slskd/Common/Configuration/YamlConfigurationSource.cs191
4 files changed, 0 insertions, 635 deletions
diff --git a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs
deleted file mode 100644
index 6cf59808..00000000
--- a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs
+++ /dev/null
@@ -1,157 +0,0 @@
-// <copyright file="CommandLineConfigurationSource.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
-{
- using System;
- using System.Linq;
- using System.Reflection;
- using Microsoft.Extensions.Configuration;
- using Utility.CommandLine;
-
- /// <summary>
- /// Extension methods for adding <see cref="CommandLineConfigurationProvider"/>.
- /// </summary>
- public static class CommandLineConfigurationExtensions
- {
- /// <summary>
- /// Adds a command line argument configuration soruce to <paramref name="builder"/>.
- /// </summary>
- /// <param name="builder">The <see cref="IConfigurationBuilder"/> to which to add.</param>
- /// <param name="targetType">The type from which to map properties.</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, Type targetType, string commandLine = null)
- {
- if (builder == null)
- {
- throw new ArgumentNullException(nameof(builder));
- }
-
- if (targetType == null)
- {
- throw new ArgumentNullException(nameof(targetType));
- }
-
- return builder.AddCommandLine(s =>
- {
- s.TargetType = targetType;
- s.CommandLine = commandLine ?? Environment.CommandLine;
- });
- }
-
- /// <summary>
- /// Adds a command line argument configuration source to <paramref name="builder"/>.
- /// </summary>
- /// <param name="builder">The <see cref="IConfigurationBuilder"/> to which to add.</param>
- /// <param name="configureSource">Configures the source.</param>
- /// <returns>The updated <see cref="IConfigurationBuilder"/>.</returns>
- public static IConfigurationBuilder AddCommandLine(this IConfigurationBuilder builder, Action<CommandLineConfigurationSource> configureSource)
- => builder.Add(configureSource);
- }
-
- /// <summary>
- /// A command line argument <see cref="ConfigurationProvider"/>.
- /// </summary>
- public class CommandLineConfigurationProvider : ConfigurationProvider
- {
- /// <summary>
- /// Initializes a new instance of the <see cref="CommandLineConfigurationProvider"/> class.
- /// </summary>
- /// <param name="source">The source settings.</param>
- public CommandLineConfigurationProvider(CommandLineConfigurationSource source)
- {
- TargetType = source.TargetType;
- Namespace = TargetType.Namespace.Split('.').First();
- CommandLine = source.CommandLine;
- }
-
- private string CommandLine { get; set; }
- private string Namespace { get; set; }
- private Type TargetType { get; set; }
-
- /// <summary>
- /// Parses command line arguments from the specified string and maps them to the corresponding keys.
- /// </summary>
- public override void Load()
- {
- var dictionary = Arguments.Parse(CommandLine).ArgumentDictionary;
-
- void Map(Type type, string path)
- {
- var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
-
- foreach (PropertyInfo property in props)
- {
- var attribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(ArgumentAttribute));
- var key = ConfigurationPath.Combine(path, property.Name.ToLowerInvariant());
-
- if (attribute != default)
- {
- var shortName = ((char)attribute.ConstructorArguments[0].Value).ToString();
- var longName = (string)attribute.ConstructorArguments[1].Value;
- var arguments = new[] { shortName, longName }.Where(i => !string.IsNullOrEmpty(i));
-
- foreach (var argument in arguments)
- {
- if (dictionary.ContainsKey(argument))
- {
- var value = dictionary[argument].ToString();
-
- if (property.PropertyType == typeof(bool) && string.IsNullOrEmpty(value))
- {
- value = "true";
- }
-
- Data[key] = value;
- }
- }
- }
- else
- {
- Map(property.PropertyType, key);
- }
- }
- }
-
- Map(TargetType, Namespace);
- }
- }
-
- /// <summary>
- /// Represents command line arguments as an <see cref="IConfigurationSource"/>.
- /// </summary>
- public class CommandLineConfigurationSource : IConfigurationSource
- {
- /// <summary>
- /// Gets or sets the command line string from which to parse arguments.
- /// </summary>
- public string CommandLine { get; set; }
-
- /// <summary>
- /// Gets or sets the type from which to map properties.
- /// </summary>
- public Type TargetType { get; set; }
-
- /// <summary>
- /// Builds the <see cref="CommandLineConfigurationProvider"/> for this source.
- /// </summary>
- /// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param>
- /// <returns>A <see cref="CommandLineConfigurationProvider"/>.</returns>
- public IConfigurationProvider Build(IConfigurationBuilder builder) => new CommandLineConfigurationProvider(this);
- }
-} \ No newline at end of file
diff --git a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs
deleted file mode 100644
index 15cf4070..00000000
--- a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs
+++ /dev/null
@@ -1,136 +0,0 @@
-// <copyright file="DefaultValueConfigurationSource.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
-{
- using System;
- using System.Linq;
- using System.Reflection;
- using Microsoft.Extensions.Configuration;
-
- /// <summary>
- /// Extension methods for adding <see cref="DefaultValueConfigurationProvider"/>.
- /// </summary>
- public static class DefaultValueConfigurationExtensions
- {
- /// <summary>
- /// Adds a default value configuration source to <paramref name="builder"/>.
- /// </summary>
- /// <param name="builder">The <see cref="IConfigurationBuilder"/> to which to add.</param>
- /// <param name="targetType">The type from which to load default values.</param>
- /// <returns>The updated <see cref="IConfigurationBuilder"/>.</returns>
- public static IConfigurationBuilder AddDefaultValues(this IConfigurationBuilder builder, Type targetType)
- {
- if (builder == null)
- {
- throw new ArgumentNullException(nameof(builder));
- }
-
- if (targetType == null)
- {
- throw new ArgumentNullException(nameof(targetType));
- }
-
- return builder.AddDefaultValues(s =>
- {
- s.TargetType = targetType;
- });
- }
-
- /// <summary>
- /// Adds a default value configuration source to <paramref name="builder"/>.
- /// </summary>
- /// <param name="builder">The <see cref="IConfigurationBuilder"/> to which to add.</param>
- /// <param name="configureSource">Configures the source.</param>
- /// <returns>The updated <see cref="IConfigurationBuilder"/>.</returns>
- public static IConfigurationBuilder AddDefaultValues(this IConfigurationBuilder builder, Action<DefaultValueConfigurationSource> configureSource)
- => builder.Add(configureSource);
- }
-
- /// <summary>
- /// A default value <see cref="ConfigurationProvider"/>.
- /// </summary>
- public class DefaultValueConfigurationProvider : ConfigurationProvider
- {
- /// <summary>
- /// Initializes a new instance of the <see cref="DefaultValueConfigurationProvider"/> class.
- /// </summary>
- /// <param name="source">The source settings.</param>
- public DefaultValueConfigurationProvider(DefaultValueConfigurationSource source)
- {
- TargetType = source.TargetType;
- Namespace = TargetType.Namespace.Split('.').First();
- }
-
- private string Namespace { get; set; }
- private Type TargetType { get; set; }
-
- /// <summary>
- /// Loads default values from the specified <see cref="TargetType"/> and maps them to the corresponding keys.
- /// </summary>
- public override void Load()
- {
- void Map(Type type, string path)
- {
- var defaults = Activator.CreateInstance(type);
- var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
-
- foreach (PropertyInfo property in props)
- {
- var key = ConfigurationPath.Combine(path, property.Name.ToLowerInvariant());
-
- if (property.PropertyType.Namespace.StartsWith(Namespace))
- {
- Map(property.PropertyType, key);
- }
- else
- {
- var value = property.GetValue(defaults);
-
- if (value != null)
- {
- Data[key] = value.ToString();
- }
- }
- }
- }
-
- Map(TargetType, Namespace);
- }
- }
-
- /// <summary>
- /// Represents default values as an <see cref="IConfigurationSource"/>.
- /// </summary>
- public class DefaultValueConfigurationSource : IConfigurationSource
- {
- /// <summary>
- /// Gets or sets the type from which to map properties.
- /// </summary>
- public Type TargetType { get; set; }
-
- /// <summary>
- /// Builds the <see cref="DefaultValueConfigurationProvider"/> for this source.
- /// </summary>
- /// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param>
- /// <returns>A <see cref="DefaultValueConfigurationProvider"/>.</returns>
- public IConfigurationProvider Build(IConfigurationBuilder builder)
- {
- return new DefaultValueConfigurationProvider(this);
- }
- }
-} \ No newline at end of file
diff --git a/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs b/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs
deleted file mode 100644
index 9a09d4e1..00000000
--- a/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs
+++ /dev/null
@@ -1,151 +0,0 @@
-// <copyright file="EnvironmentVariableConfigurationSource.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
-{
- using System;
- using System.Linq;
- using System.Reflection;
- using Microsoft.Extensions.Configuration;
- using Utility.EnvironmentVariables;
-
- /// <summary>
- /// Extension methods for adding <see cref="EnvironmentVariableConfigurationProvider"/>.
- /// </summary>
- public static class EnvironmentVariableConfigurationExtensions
- {
- /// <summary>
- /// Adds an environment variable configuration source to <paramref name="builder"/>.
- /// </summary>
- /// <param name="builder">The <see cref="IConfigurationBuilder"/> to which to add.</param>
- /// <param name="targetType">The type from which to map properties.</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, Type targetType, string prefix = null)
- {
- if (builder == null)
- {
- throw new ArgumentNullException(nameof(builder));
- }
-
- if (targetType == null)
- {
- throw new ArgumentNullException(nameof(targetType));
- }
-
- return builder.AddEnvironmentVariables(s =>
- {
- s.TargetType = targetType;
- s.Prefix = prefix ?? string.Empty;
- });
- }
-
- /// <summary>
- /// Adds an enviroment variable configuration source to <paramref name="builder"/>.
- /// </summary>
- /// <param name="builder">The <see cref="IConfigurationBuilder"/> to which to add.</param>
- /// <param name="configureSource">Configures the source.</param>
- /// <returns>The updated <see cref="IConfigurationBuilder"/>.</returns>
- public static IConfigurationBuilder AddEnvironmentVariables(this IConfigurationBuilder builder, Action<EnvironmentVariableConfigurationSource> configureSource)
- => builder.Add(configureSource);
- }
-
- /// <summary>
- /// An enviroment variable <see cref="ConfigurationProvider"/>.
- /// </summary>
- public class EnvironmentVariableConfigurationProvider : ConfigurationProvider
- {
- /// <summary>
- /// Initializes a new instance of the <see cref="EnvironmentVariableConfigurationProvider"/> class.
- /// </summary>
- /// <param name="source">The source settings.</param>
- public EnvironmentVariableConfigurationProvider(EnvironmentVariableConfigurationSource source)
- {
- TargetType = source.TargetType;
- Namespace = TargetType.Namespace.Split('.').First();
- Prefix = source.Prefix;
- }
-
- private Type TargetType { get; set; }
- private string Namespace { get; set; }
- private string Prefix { get; set; }
-
- /// <summary>
- /// Loads environment variables and maps them to the corresponding keys.
- /// </summary>
- public override void Load()
- {
- void Map(Type type, string path)
- {
- var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
-
- foreach (PropertyInfo property in props)
- {
- var attribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(EnvironmentVariableAttribute));
- var key = ConfigurationPath.Combine(path, property.Name.ToLowerInvariant());
-
- if (attribute != default)
- {
- var name = (string)attribute.ConstructorArguments[0].Value;
-
- if (!string.IsNullOrEmpty(name))
- {
- var value = Environment.GetEnvironmentVariable(Prefix + name);
-
- if (value != null)
- {
- Data[key] = value.ToString();
- }
- }
- }
- else
- {
- Map(property.PropertyType, key);
- }
- }
- }
-
- Map(TargetType, Namespace);
- }
- }
-
- /// <summary>
- /// Represents environment variables as an <see cref="IConfigurationSource"/>.
- /// </summary>
- public class EnvironmentVariableConfigurationSource : IConfigurationSource
- {
- /// <summary>
- /// Gets or sets the type from which to map properties.
- /// </summary>
- public Type TargetType { get; set; }
-
- /// <summary>
- /// Gets or sets a prefix to prepend to all variable names.
- /// </summary>
- public string Prefix { get; set; }
-
- /// <summary>
- /// Builds the <see cref="EnvironmentVariableConfigurationProvider"/> for this source.
- /// </summary>
- /// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param>
- /// <returns>A <see cref="EnvironmentVariableConfigurationProvider"/>.</returns>
- public IConfigurationProvider Build(IConfigurationBuilder builder)
- {
- return new EnvironmentVariableConfigurationProvider(this);
- }
- }
-} \ No newline at end of file
diff --git a/src/slskd/Common/Configuration/YamlConfigurationSource.cs b/src/slskd/Common/Configuration/YamlConfigurationSource.cs
deleted file mode 100644
index 82e2227d..00000000
--- a/src/slskd/Common/Configuration/YamlConfigurationSource.cs
+++ /dev/null
@@ -1,191 +0,0 @@
-// <copyright file="YamlConfigurationSource.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
-{
- using System;
- using System.IO;
- using System.Linq;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.FileProviders;
- using YamlDotNet.Core;
- using YamlDotNet.RepresentationModel;
-
- /// <summary>
- /// Extension methods for adding <see cref="YamlConfigurationProvider"/>.
- /// </summary>
- public static class YamlConfigurationExtensions
- {
- /// <summary>
- /// Adds a YAML configuration source to <paramref name="builder"/>.
- /// </summary>
- /// <param name="builder">The <see cref="IConfigurationBuilder"/> to which to add .</param>
- /// <param name="path">
- /// Path relative to the base path stored in <see cref="IConfigurationBuilder.Properties"/> of <paramref name="builder"/>.
- /// </param>
- /// <param name="targetType">The type from which to map properties.</param>
- /// <param name="optional">Whether the file is optional.</param>
- /// <param name="reloadOnChange">Whether the configuration should be reloaded if the file changes.</param>
- /// <param name="normalizeKeys">
- /// A value indicating whether configuration keys should be normalized (_, - removed, changed to lowercase).
- /// </param>
- /// <param name="provider">The updated <see cref="IFileProvider"/> to use to access the file.</param>
- /// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
- public static IConfigurationBuilder AddYamlFile(this IConfigurationBuilder builder, string path, Type targetType, bool optional = true, bool reloadOnChange = false, bool normalizeKeys = true, IFileProvider provider = null)
- {
- if (builder == null)
- {
- throw new ArgumentNullException(nameof(builder));
- }
-
- if (string.IsNullOrEmpty(path))
- {
- throw new ArgumentException("File path must be a non-empty string.", nameof(path));
- }
-
- return builder.AddYamlFile(s =>
- {
- s.Path = path;
- s.TargetType = targetType;
- s.Optional = optional;
- s.ReloadOnChange = reloadOnChange;
- s.NormalizeKeys = normalizeKeys;
- s.FileProvider = provider;
- s.ResolveFileProvider();
- });
- }
-
- /// <summary>
- /// Adds a YAML configuration source to <paramref name="builder"/>.
- /// </summary>
- /// <param name="builder">The <see cref="IConfigurationBuilder"/> to which to add.</param>
- /// <param name="configureSource">Configures the source.</param>
- /// <returns>The updated <see cref="IConfigurationBuilder"/>.</returns>
- public static IConfigurationBuilder AddYamlFile(this IConfigurationBuilder builder, Action<YamlConfigurationSource> configureSource)
- => builder.Add(configureSource);
- }
-
- /// <summary>
- /// A YAML file based <see cref="FileConfigurationProvider"/>.
- /// </summary>
- public class YamlConfigurationProvider : FileConfigurationProvider
- {
- /// <summary>
- /// Initializes a new instance of the <see cref="YamlConfigurationProvider"/> class.
- /// </summary>
- /// <param name="source">The source settings.</param>
- public YamlConfigurationProvider(YamlConfigurationSource source)
- : base(source)
- {
- TargetType = source.TargetType;
- Namespace = TargetType.Namespace.Split('.').First();
- NormalizeKeys = source.NormalizeKeys;
- }
-
- private Type TargetType { get; set; }
- private string Namespace { get; set; }
- private bool NormalizeKeys { get; set; }
- private string[] NullValues { get; } = new[] { "~", "null", string.Empty };
-
- /// <summary>
- /// Loads the YAML data from a stream.
- /// </summary>
- /// <param name="stream">The stream to read.</param>
- public override void Load(Stream stream)
- {
- try
- {
- using var reader = new StreamReader(stream);
-
- var yaml = new YamlStream();
- yaml.Load(reader);
-
- if (yaml.Documents.Count > 0)
- {
- var rootNode = (YamlMappingNode)yaml.Documents[0].RootNode;
- Traverse(rootNode, Namespace);
- }
- }
- catch (YamlException e)
- {
- throw new FormatException("Could not parse the YAML file.", e);
- }
- }
-
- private void Traverse(YamlNode root, string path = null)
- {
- string Normalize(string str) => NormalizeKeys ? str?.Replace("_", string.Empty).Replace("-", string.Empty).ToLowerInvariant() : str;
-
- if (root is YamlScalarNode scalar)
- {
- if (Data.ContainsKey(Normalize(path)))
- {
- throw new FormatException($"A duplicate key '{Normalize(path)}' was found.");
- }
-
- var value = NullValues.Contains(scalar.Value.ToLower()) ? null : scalar.Value;
-
- if (value != null)
- {
- Data[Normalize(path)] = NullValues.Contains(scalar.Value.ToLower()) ? null : scalar.Value;
- }
- }
- else if (root is YamlMappingNode map)
- {
- foreach (var node in map.Children)
- {
- var key = Normalize(((YamlScalarNode)node.Key).Value);
- Traverse(node.Value, path == null ? key : ConfigurationPath.Combine(path, key));
- }
- }
- else if (root is YamlSequenceNode sequence)
- {
- for (int i = 0; i < sequence.Children.Count; i++)
- {
- Traverse(sequence.Children[i], ConfigurationPath.Combine(path, i.ToString()));
- }
- }
- }
- }
-
- /// <summary>
- /// Represents a YAML file as an <see cref="IConfigurationSource"/>.
- /// </summary>
- public class YamlConfigurationSource : FileConfigurationSource
- {
- /// <summary>
- /// Gets or sets the type from which to map properties.
- /// </summary>
- public Type TargetType { get; set; }
-
- /// <summary>
- /// Gets or sets a value indicating whether configuration keys should be normalized (_, - removed, changed to lowercase).
- /// </summary>
- public bool NormalizeKeys { get; set; }
-
- /// <summary>
- /// Builds the <see cref="YamlConfigurationProvider"/> for this source.
- /// </summary>
- /// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param>
- /// <returns>A <see cref="YamlConfigurationProvider"/>.</returns>
- public override IConfigurationProvider Build(IConfigurationBuilder builder)
- {
- EnsureDefaults(builder);
- return new YamlConfigurationProvider(this);
- }
- }
-} \ No newline at end of file