From fb228b5d1b94e143d3e36c5df56a3d8311ecec2b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 9 Feb 2021 21:41:08 -0600 Subject: update docs --- .../EnvironmentVariableConfigurationSource.cs | 103 +++++++++++++-------- 1 file changed, 62 insertions(+), 41 deletions(-) (limited to 'src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs') diff --git a/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs b/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs index a18faebc..51ddf535 100644 --- a/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs +++ b/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs @@ -1,9 +1,27 @@ -namespace slskd.Configuration +// +// 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/. +// + +#pragma warning disable SA1649 // File name should match first type name +namespace slskd.Configuration { - using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.Linq; + using Microsoft.Extensions.Configuration; /// /// Defines an environment variable mapping. @@ -18,11 +36,13 @@ /// /// Adds an environment variable configuration source to . /// - /// The to add to. + /// The to which to add. /// A list of environment variable mappings. /// A prefix to prepend to all variable names. - /// A value indicating whether configuration keys should be normalized (_, - removed, changed to lowercase). - /// The . + /// + /// A value indicating whether configuration keys should be normalized (_, - removed, changed to lowercase). + /// + /// The updated . public static IConfigurationBuilder AddEnvironmentVariables(this IConfigurationBuilder builder, IEnumerable map, string prefix = null, bool normalizeKey = true) { if (builder == null) @@ -41,54 +61,23 @@ /// /// Adds an enviroment variable configuration source to . /// - /// The to add to. + /// The to which to add. /// Configures the source. - /// The . + /// The updated . public static IConfigurationBuilder AddEnvironmentVariables(this IConfigurationBuilder builder, Action configureSource) => builder.Add(configureSource); } - /// - /// Represents environment variables as an . - /// - public class EnvironmentVariableConfigurationSource : IConfigurationSource - { - /// - /// Builds the for this source. - /// - /// The . - /// A . - public IConfigurationProvider Build(IConfigurationBuilder builder) - { - return new EnvironmentVariableConfigurationProvider(this); - } - - /// - /// Gets or sets a list of enviroment variable mappings. - /// - public IEnumerable Map { get; set; } - - /// - /// Gets or sets a prefix to prepend to all variable names. - /// - public string Prefix { get; set; } - - /// - /// Gets or sets a value indicating whether configuration keys should be normalized (_, - removed, changed to lowercase). - /// - public bool NormalizeKey { get; set; } - } - /// /// An enviroment variable . /// public class EnvironmentVariableConfigurationProvider : ConfigurationProvider { /// - /// Initializes a new instance with the specified source. + /// Initializes a new instance of the class. /// /// The source settings. - public EnvironmentVariableConfigurationProvider(EnvironmentVariableConfigurationSource source) + public EnvironmentVariableConfigurationProvider(EnvironmentVariableConfigurationSource source) { Map = source.Map; Prefix = source.Prefix; @@ -126,7 +115,7 @@ } if (type == typeof(bool)) - { + { value = value.Equals("true", StringComparison.InvariantCultureIgnoreCase) ? value : "false"; } @@ -134,4 +123,36 @@ } } } + + /// + /// Represents environment variables as an . + /// + public class EnvironmentVariableConfigurationSource : IConfigurationSource + { + /// + /// Gets or sets a list of enviroment variable mappings. + /// + public IEnumerable Map { get; set; } + + /// + /// Gets or sets a value indicating whether configuration keys should be normalized (_, - removed, changed to lowercase). + /// + public bool NormalizeKey { get; set; } + + /// + /// Gets or sets a prefix to prepend to all variable names. + /// + public string Prefix { get; set; } + + /// + /// Builds the for this source. + /// + /// The . + /// A . + public IConfigurationProvider Build(IConfigurationBuilder builder) + { + return new EnvironmentVariableConfigurationProvider(this); + } + } } +#pragma warning restore SA1649 // File name should match first type name \ No newline at end of file -- cgit v1.2.3 From 39c66558cfedfbb28b11c37cd43ef39adcbe1d26 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 10 Feb 2021 08:27:36 -0600 Subject: more docs --- .../CommandLineConfigurationSource.cs | 24 ++------------ .../Configuration/ConfigurationExtensions.cs | 37 +++++++++++++++++++++- .../DefaultValueConfigurationSource.cs | 31 +++--------------- .../EnvironmentVariableConfigurationSource.cs | 17 ++-------- .../Configuration/YamlConfigurationSource.cs | 12 +++---- src/slskd/Options.cs | 34 ++++++++++++++++---- 6 files changed, 77 insertions(+), 78 deletions(-) (limited to 'src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs') diff --git a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs index 75ecadc4..088d5ea6 100644 --- a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs +++ b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs @@ -15,7 +15,6 @@ // along with this program. If not, see https://www.gnu.org/licenses/. // -#pragma warning disable SA1649 // File name should match first type name namespace slskd.Configuration { using System; @@ -32,7 +31,7 @@ namespace slskd.Configuration /// /// Extension methods for adding . /// - public static class CommandLineExtensions + public static class CommandLineConfigurationExtensions { /// /// Adds a command line argument configuration soruce to . @@ -40,11 +39,8 @@ namespace slskd.Configuration /// The to which to add. /// A list of command line argument mappings. /// The command line string from which to parse arguments. - /// - /// A value indicating whether configuration keys should be normalized (_, - removed, changed to lowercase). - /// /// The updated . - public static IConfigurationBuilder AddCommandLine(this IConfigurationBuilder builder, IEnumerable map, string commandLine = null, bool normalizeKey = true) + public static IConfigurationBuilder AddCommandLine(this IConfigurationBuilder builder, IEnumerable map, string commandLine = null) { if (builder == null) { @@ -55,7 +51,6 @@ namespace slskd.Configuration { s.Map = map; s.CommandLine = commandLine ?? Environment.CommandLine; - s.NormalizeKey = normalizeKey; }); } @@ -82,12 +77,10 @@ namespace slskd.Configuration { Map = source.Map; CommandLine = source.CommandLine; - NormalizeKey = source.NormalizeKey; } private string CommandLine { get; set; } private IEnumerable Map { get; set; } - private bool NormalizeKey { get; set; } /// /// Parses command line arguments from the specified string and maps them to the specified keys. @@ -100,11 +93,6 @@ namespace slskd.Configuration { var (shortName, longName, type, key, _) = item; - if (NormalizeKey) - { - key = key?.Replace("-", string.Empty).Replace("_", string.Empty).ToLowerInvariant(); - } - if (string.IsNullOrEmpty(key)) { continue; @@ -145,11 +133,6 @@ namespace slskd.Configuration /// public IEnumerable Map { get; set; } - /// - /// Gets or sets a value indicating whether configuration keys should be normalized (_, - removed, changed to lowercase). - /// - public bool NormalizeKey { get; set; } - /// /// Builds the for this source. /// @@ -157,5 +140,4 @@ namespace slskd.Configuration /// A . public IConfigurationProvider Build(IConfigurationBuilder builder) => new CommandLineConfigurationProvider(this); } -} -#pragma warning restore SA1649 // File name should match first type name \ No newline at end of file +} \ No newline at end of file diff --git a/src/slskd/Common/Configuration/ConfigurationExtensions.cs b/src/slskd/Common/Configuration/ConfigurationExtensions.cs index 77b59700..33385e0a 100644 --- a/src/slskd/Common/Configuration/ConfigurationExtensions.cs +++ b/src/slskd/Common/Configuration/ConfigurationExtensions.cs @@ -1,13 +1,48 @@ -namespace slskd.Configuration +// +// 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/. +// + +namespace slskd.Configuration { + /// + /// Extension methods for the Configuration namespace. + /// public static class ConfigurationExtensions { + /// + /// Converts an mapping to a mapping. + /// + /// The option mapping to convert. + /// The converted mapping. public static CommandLineArgument ToCommandLineArgument(this Option option) => new(option.ShortName, option.LongName, option.Type, option.Key, option.Description); + /// + /// Converts an mapping to a mapping. + /// + /// The option mapping to convert. + /// The converted mapping. public static EnvironmentVariable ToEnvironmentVariable(this Option option) => new(option.EnvironmentVariable, option.Type, option.Key, option.Description); + /// + /// Converts an mapping to a mapping. + /// + /// The option mapping to convert. + /// The converted mapping. 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 5bbba10e..07e4f1b7 100644 --- a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs +++ b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs @@ -15,7 +15,6 @@ // along with this program. If not, see https://www.gnu.org/licenses/. // -#pragma warning disable SA1649 // File name should match first type name namespace slskd.Configuration { using System; @@ -24,7 +23,7 @@ namespace slskd.Configuration using Microsoft.Extensions.Configuration; /// - /// Defines an default value mapping. + /// Defines a default value mapping. /// public record DefaultValue(string Key, Type Type, object Default); @@ -38,11 +37,8 @@ namespace slskd.Configuration /// /// The to which to add. /// A list of default value mappings. - /// - /// A value indicating whether configuration keys should be normalized (_, - removed, changed to lowercase). - /// /// The updated . - public static IConfigurationBuilder AddDefaultValues(this IConfigurationBuilder builder, IEnumerable map, bool normalizeKey = true) + public static IConfigurationBuilder AddDefaultValues(this IConfigurationBuilder builder, IEnumerable map) { if (builder == null) { @@ -52,7 +48,6 @@ namespace slskd.Configuration return builder.AddDefaultValues(s => { s.Map = map ?? Enumerable.Empty(); - s.NormalizeKey = normalizeKey; }); } @@ -78,11 +73,9 @@ namespace slskd.Configuration public DefaultValueConfigurationProvider(DefaultValueConfigurationSource source) { Map = source.Map; - NormalizeKey = source.NormalizeKey; } private IEnumerable Map { get; set; } - private bool NormalizeKey { get; set; } /// /// Sets the keys specified in the map to the specified default values. @@ -93,17 +86,7 @@ namespace slskd.Configuration { var (key, _, value) = item; - if (NormalizeKey) - { - key = key?.Replace("_", string.Empty).Replace("-", string.Empty).ToLowerInvariant(); - } - - if (string.IsNullOrEmpty(key)) - { - continue; - } - - if (value == null) + if (string.IsNullOrEmpty(key) || value == null) { continue; } @@ -123,11 +106,6 @@ namespace slskd.Configuration /// public IEnumerable Map { get; set; } - /// - /// Gets or sets a value indicating whether configuration keys should be normalized (_, - removed, changed to lowercase). - /// - public bool NormalizeKey { get; set; } - /// /// Builds the for this source. /// @@ -138,5 +116,4 @@ namespace slskd.Configuration return new DefaultValueConfigurationProvider(this); } } -} -#pragma warning restore SA1649 // File name should match first type name \ No newline at end of file +} \ No newline at end of file diff --git a/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs b/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs index 51ddf535..21775689 100644 --- a/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs +++ b/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs @@ -15,7 +15,6 @@ // along with this program. If not, see https://www.gnu.org/licenses/. // -#pragma warning disable SA1649 // File name should match first type name namespace slskd.Configuration { using System; @@ -39,11 +38,8 @@ namespace slskd.Configuration /// The to which to add. /// A list of environment variable mappings. /// A prefix to prepend to all variable names. - /// - /// A value indicating whether configuration keys should be normalized (_, - removed, changed to lowercase). - /// /// The updated . - public static IConfigurationBuilder AddEnvironmentVariables(this IConfigurationBuilder builder, IEnumerable map, string prefix = null, bool normalizeKey = true) + public static IConfigurationBuilder AddEnvironmentVariables(this IConfigurationBuilder builder, IEnumerable map, string prefix = null) { if (builder == null) { @@ -54,7 +50,6 @@ namespace slskd.Configuration { s.Map = map ?? Enumerable.Empty(); s.Prefix = prefix ?? string.Empty; - s.NormalizeKey = normalizeKey; }); } @@ -81,11 +76,9 @@ namespace slskd.Configuration { Map = source.Map; Prefix = source.Prefix; - NormalizeKey = source.NormalizeKey; } private IEnumerable Map { get; set; } - private bool NormalizeKey { get; set; } private string Prefix { get; set; } /// @@ -97,11 +90,6 @@ namespace slskd.Configuration { var (name, type, key, _) = item; - if (NormalizeKey) - { - key = key?.Replace("_", string.Empty).Replace("-", string.Empty).ToLowerInvariant(); - } - if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(name)) { continue; @@ -154,5 +142,4 @@ namespace slskd.Configuration return new EnvironmentVariableConfigurationProvider(this); } } -} -#pragma warning restore SA1649 // File name should match first type name \ No newline at end of file +} \ No newline at end of file diff --git a/src/slskd/Common/Configuration/YamlConfigurationSource.cs b/src/slskd/Common/Configuration/YamlConfigurationSource.cs index 7766e14c..332df647 100644 --- a/src/slskd/Common/Configuration/YamlConfigurationSource.cs +++ b/src/slskd/Common/Configuration/YamlConfigurationSource.cs @@ -15,7 +15,6 @@ // along with this program. If not, see https://www.gnu.org/licenses/. // -#pragma warning disable SA1649 // File name should match first type name namespace slskd.Configuration { using System; @@ -34,7 +33,7 @@ namespace slskd.Configuration /// /// Adds a YAML configuration source to . /// - /// The to add to. + /// The to which to add . /// /// Path relative to the base path stored in of . /// @@ -43,7 +42,7 @@ namespace slskd.Configuration /// /// A value indicating whether configuration keys should be normalized (_, - removed, changed to lowercase). /// - /// The to use to access the file. + /// The updated to use to access the file. /// The . public static IConfigurationBuilder AddYamlFile(this IConfigurationBuilder builder, string path, bool optional = true, bool reloadOnChange = false, bool normalizeKeys = true, IFileProvider provider = null) { @@ -71,9 +70,9 @@ namespace slskd.Configuration /// /// Adds a YAML configuration source to . /// - /// The to add to. + /// The to which to add. /// Configures the source. - /// The . + /// The updated . public static IConfigurationBuilder AddYamlFile(this IConfigurationBuilder builder, Action configureSource) => builder.Add(configureSource); } @@ -178,5 +177,4 @@ namespace slskd.Configuration return new YamlConfigurationProvider(this); } } -} -#pragma warning restore SA1649 // File name should match first type name \ No newline at end of file +} \ No newline at end of file diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs index b48853ef..af78e68d 100644 --- a/src/slskd/Options.cs +++ b/src/slskd/Options.cs @@ -1,12 +1,32 @@ -namespace slskd +// +// 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/. +// + +namespace slskd { - using slskd.Validation; - using Soulseek.Diagnostics; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Diagnostics; + using slskd.Validation; + using Soulseek.Diagnostics; + /// + /// Defines an option mapping. + /// public record Option(char ShortName, string LongName, string EnvironmentVariable, string Key, Type Type, object Default = null, string Description = null); public class Options @@ -17,11 +37,11 @@ public bool NoLogo { get; private set; } = false; public string InstanceName { get; private set; } = "default"; [Validate] - public DirectoriesOptions Directories { get; private set; } = new DirectoriesOptions(); + public DirectoriesOptions Directories { get; private set; } = new DirectoriesOptions(); [Validate] - public WebOptions Web { get; private set; } = new WebOptions(); + public WebOptions Web { get; private set; } = new WebOptions(); [Validate] - public LoggerOptions Logger { get; private set; } = new LoggerOptions(); + public LoggerOptions Logger { get; private set; } = new LoggerOptions(); [Validate] public FeatureOptions Feature { get; private set; } = new FeatureOptions(); [Validate] @@ -336,7 +356,7 @@ ShortName: default, LongName: "prometheus", EnvironmentVariable: "PROMETHEUS", - Key: "slskd:feature:prometheus", + Key: "slskd:feature:prometheus", Type: typeof(bool), Default: Defaults.Feature.Prometheus, Description: "enable collection and publishing of prometheus metrics"), -- cgit v1.2.3 From 62b44e06b3f311970e8835d647c97de5498f394e Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 10 Feb 2021 21:14:33 -0600 Subject: simplify option mapping --- .../CommandLineConfigurationSource.cs | 21 ++++------ .../Configuration/ConfigurationExtensions.cs | 49 ---------------------- .../DefaultValueConfigurationSource.cs | 19 +++------ .../EnvironmentVariableConfigurationSource.cs | 21 ++++------ src/slskd/Program.cs | 10 ++--- 5 files changed, 25 insertions(+), 95 deletions(-) delete mode 100644 src/slskd/Common/Configuration/ConfigurationExtensions.cs (limited to 'src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs') 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 @@ -23,11 +23,6 @@ namespace slskd.Configuration using Microsoft.Extensions.Configuration; using Utility.CommandLine; - /// - /// Defines a command line argument mapping. - /// - public record CommandLineArgument(char ShortName, string LongName, Type Type, string Key, string Description = null); - /// /// Extension methods for adding . /// @@ -40,7 +35,7 @@ namespace slskd.Configuration /// A list of command line argument mappings. /// The command line string from which to parse arguments. /// The updated . - public static IConfigurationBuilder AddCommandLine(this IConfigurationBuilder builder, IEnumerable map, string commandLine = null) + public static IConfigurationBuilder AddCommandLine(this IConfigurationBuilder builder, IEnumerable