From e52ca241f55e47e1e707134097ad5c4f8a641b07 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 21 Jan 2021 23:04:02 -0600 Subject: move stuff to common --- src/slskd/Common/YamlConfigurationSource.cs | 209 ++++++++++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 src/slskd/Common/YamlConfigurationSource.cs (limited to 'src/slskd/Common/YamlConfigurationSource.cs') diff --git a/src/slskd/Common/YamlConfigurationSource.cs b/src/slskd/Common/YamlConfigurationSource.cs new file mode 100644 index 00000000..9dde3179 --- /dev/null +++ b/src/slskd/Common/YamlConfigurationSource.cs @@ -0,0 +1,209 @@ +/* + █▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀ ▀ ▀ ▀▀ + █ The MIT License (MIT) + █ + █ Copyright (c) 2021 JP Dillingham (jp@dillingham.ws) + █ + █ Permission is hereby granted, free of charge, to any person obtaining a copy + █ of this software and associated documentation files (the "Software"), to deal + █ in the Software without restriction, including without limitation the rights + █ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + █ copies of the Software, and to permit persons to whom the Software is + █ furnished to do so, subject to the following conditions: + █ + █ The above copyright notice and this permission notice shall be included in all + █ copies or substantial portions of the Software. + █ + █ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + █ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + █ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + █ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + █ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + █ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + █ SOFTWARE. + █ + ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀▀ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀██ + ██ + ▀█▄ ██ ▄█▀ + ▀████▀ + ▀▀ */ + +namespace slskd +{ + using Microsoft.Extensions.Configuration; + using Microsoft.Extensions.FileProviders; + using System; + using System.IO; + using System.Linq; + using YamlDotNet.Core; + using YamlDotNet.RepresentationModel; + + /// + /// Extension methods for adding . + /// + public static class YamlConfigurationExtensions + { + /// + /// Adds the YAML configuration provider at to . + /// + /// The to add to. + /// Path relative to the base path stored in + /// of . + /// The . + public static IConfigurationBuilder AddYamlFile(this IConfigurationBuilder builder, string path) + { + return AddYamlFile(builder, provider: null, path: path, optional: false, reloadOnChange: false); + } + + /// + /// Adds the YAML configuration provider at to . + /// + /// The to add to. + /// Path relative to the base path stored in + /// of . + /// Whether the file is optional. + /// The . + public static IConfigurationBuilder AddYamlFile(this IConfigurationBuilder builder, string path, bool optional) + { + return AddYamlFile(builder, provider: null, path: path, optional: optional, reloadOnChange: false); + } + + /// + /// Adds the YAML configuration provider at to . + /// + /// The to add to. + /// Path relative to the base path stored in + /// of . + /// Whether the file is optional. + /// Whether the configuration should be reloaded if the file changes. + /// The . + public static IConfigurationBuilder AddYamlFile(this IConfigurationBuilder builder, string path, bool optional, bool reloadOnChange) + { + return AddYamlFile(builder, provider: null, path: path, optional: optional, reloadOnChange: reloadOnChange); + } + + /// + /// Adds a YAML configuration source to . + /// + /// The to add to. + /// The to use to access the file. + /// Path relative to the base path stored in + /// of . + /// Whether the file is optional. + /// Whether the configuration should be reloaded if the file changes. + /// The . + public static IConfigurationBuilder AddYamlFile(this IConfigurationBuilder builder, IFileProvider provider, string path, bool optional, bool reloadOnChange) + { + 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.FileProvider = provider; + s.Path = path; + s.Optional = optional; + s.ReloadOnChange = reloadOnChange; + s.ResolveFileProvider(); + }); + } + + /// + /// Adds a YAML configuration source to . + /// + /// The to add to. + /// Configures the source. + /// The . + public static IConfigurationBuilder AddYamlFile(this IConfigurationBuilder builder, Action configureSource) + => builder.Add(configureSource); + } + + /// + /// Represents a YAML file as an . + /// + public class YamlConfigurationSource : FileConfigurationSource + { + /// + /// Builds the for this source. + /// + /// The . + /// A + public override IConfigurationProvider Build(IConfigurationBuilder builder) + { + EnsureDefaults(builder); + return new YamlConfigurationProvider(this); + } + } + + /// + /// A YAML file based . + /// + public class YamlConfigurationProvider : FileConfigurationProvider + { + /// + /// Initializes a new instance with the specified source. + /// + /// The source settings. + public YamlConfigurationProvider(YamlConfigurationSource source) : base(source) { } + + private string[] NullValues { get; } = new[] { "~", "null", "" }; + + /// + /// Loads the YAML data from a stream. + /// + /// The stream to read. + public override void Load(Stream stream) + { + try + { + using var reader = new StreamReader(stream); + + var yaml = new YamlStream(); + yaml.Load(reader); + + var rootNode = (YamlMappingNode)yaml.Documents[0].RootNode; + + Traverse(rootNode); + } + catch (YamlException e) + { + throw new FormatException("Could not parse the YAML file.", e); + } + } + + private void Traverse(YamlNode root, string path = null) + { + static string Clean(string str) => str?.Replace("_", string.Empty).Replace("-", string.Empty); + + if (root is YamlScalarNode scalar) + { + if (Data.ContainsKey(Clean(path))) + { + throw new FormatException($"A duplicate key '{Clean(path)}' was found."); + } + + Data[Clean(path)] = NullValues.Contains(scalar.Value.ToLower()) ? null : scalar.Value; + } + else if (root is YamlMappingNode map) + { + foreach (var node in map.Children) + { + var key = Clean(((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())); + } + } + } + } +} -- cgit v1.2.3