aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-01-27 19:48:55 -0600
committerJP Dillingham <jp@dillingham.me>2026-01-27 19:48:55 -0600
commite8e3a4aa80fbcca9cf8c841c94ff73396c662352 (patch)
tree4a22c65f99207d10f1f261ae7fc9da624a9bf768 /src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs
parent8859b9a6c82b1490349f7ffb4f6699b502384f8a (diff)
downloadslskd-e8e3a4aa80fbcca9cf8c841c94ff73396c662352.tar.gz
wip
Diffstat (limited to 'src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs')
-rw-r--r--src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs152
1 files changed, 0 insertions, 152 deletions
diff --git a/src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs b/src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs
deleted file mode 100644
index 84dd8b35..00000000
--- a/src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs
+++ /dev/null
@@ -1,152 +0,0 @@
-// <copyright file="RuntimePatchConfigurationSource.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.Collections.Generic;
- using System.Reflection;
- using Microsoft.Extensions.Configuration;
-
- /// <summary>
- /// Extension methods for adding <see cref="RuntimePatchConfigurationProvider"/>.
- /// </summary>
- public static class RuntimePatchConfigurationExtensions
- {
- /// <summary>
- /// Adds a runtime patch 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 AddRuntimePatch(
- this IConfigurationBuilder builder,
- Action<RuntimePatchConfigurationSource> configureSource)
- => builder.Add(configureSource);
- }
-
- /// <summary>
- /// A runtime patch <see cref="ConfigurationProvider"/>.
- /// </summary>
- public class RuntimePatchConfigurationProvider : ConfigurationProvider
- {
- /// <summary>
- /// Initializes a new instance of the <see cref="RuntimePatchConfigurationProvider"/> class.
- /// </summary>
- /// <param name="source">The source settings.</param>
- public RuntimePatchConfigurationProvider(RuntimePatchConfigurationSource source)
- {
- Source = source;
- source.Provider = this;
- }
-
- private RuntimePatchConfigurationSource Source { get; }
-
- /// <summary>
- /// Loads patch values from the current <see cref="OptionsPatch"/> and maps them to the corresponding keys.
- /// </summary>
- public override void Load()
- {
- var data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
-
- if (Source.Patch != null)
- {
- Flatten(Source.Patch, "slskd", data);
- }
-
- Data = data;
- }
-
- /// <summary>
- /// Applies the specified <paramref name="patch"/> and triggers a configuration reload.
- /// </summary>
- /// <param name="patch">The patch to apply.</param>
- public void ApplyPatch(OptionsPatch patch)
- {
- Source.Patch = patch;
- Load();
- OnReload();
- }
-
- private void Flatten(object obj, string path, Dictionary<string, string> data)
- {
- if (obj == null)
- {
- return;
- }
-
- var type = obj.GetType();
- var props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
-
- foreach (var property in props)
- {
- var value = property.GetValue(obj);
-
- if (value == null)
- {
- continue;
- }
-
- var key = ConfigurationPath.Combine(path, property.Name.ToLowerInvariant());
-
- if (property.PropertyType.IsClass && property.PropertyType != typeof(string))
- {
- Flatten(value, key, data);
- }
- else
- {
- data[key] = value.ToString();
- }
- }
- }
- }
-
- /// <summary>
- /// Represents a runtime patch as an <see cref="IConfigurationSource"/>.
- /// </summary>
- public class RuntimePatchConfigurationSource : IConfigurationSource
- {
- /// <summary>
- /// Gets or sets the provider instance.
- /// </summary>
- internal RuntimePatchConfigurationProvider Provider { get; set; }
-
- /// <summary>
- /// Gets or sets the current patch.
- /// </summary>
- internal OptionsPatch Patch { get; set; }
-
- /// <summary>
- /// Builds the <see cref="RuntimePatchConfigurationProvider"/> for this source.
- /// </summary>
- /// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param>
- /// <returns>A <see cref="RuntimePatchConfigurationProvider"/>.</returns>
- public IConfigurationProvider Build(IConfigurationBuilder builder)
- {
- return new RuntimePatchConfigurationProvider(this);
- }
-
- /// <summary>
- /// Applies the specified <paramref name="patch"/> and triggers a configuration reload.
- /// </summary>
- /// <param name="patch">The patch to apply.</param>
- public void ApplyPatch(OptionsPatch patch)
- {
- Provider?.ApplyPatch(patch);
- }
- }
-}