aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs
blob: 84dd8b35edb1bd53833ee35d3965307c3274c1e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// <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);
        }
    }
}