From d02d99dfa047bd8a55e8286098e386879683c869 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 25 Jul 2021 21:58:18 -0500 Subject: split environment variable values by semicolon if backing prop is array --- .../EnvironmentVariableConfigurationSource.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'src/slskd/Common/Configuration') diff --git a/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs b/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs index 9a09d4e1..6d2896c2 100644 --- a/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs +++ b/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs @@ -100,15 +100,32 @@ namespace slskd.Configuration if (attribute != default) { + // retrieve the envar name from the attribute var name = (string)attribute.ConstructorArguments[0].Value; if (!string.IsNullOrEmpty(name)) { + // retrieve the corresponding value from environment variables var value = Environment.GetEnvironmentVariable(Prefix + name); if (value != null) { - Data[key] = value.ToString(); + // if the type of the backing property is an array, + // split the retrieved value by semicolon and add the parts to + // config as zero-based children of the prop + if (property.PropertyType.IsArray) + { + var elements = value.Split(';'); + + for (int i = 0; i < elements.Length; i++) + { + Data[ConfigurationPath.Combine(key, i.ToString())] = elements[i]; + } + } + else + { + Data[key] = value.ToString(); + } } } } -- cgit v1.2.3 From 8807c9967600fb01282eb6c1aecaf57d714eb061 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 25 Jul 2021 22:27:14 -0500 Subject: wip --- src/slskd/Common/Configuration/CommandLineConfigurationSource.cs | 4 +++- src/slskd/Options.cs | 8 ++++++++ src/slskd/Program.cs | 3 +++ 3 files changed, 14 insertions(+), 1 deletion(-) (limited to 'src/slskd/Common/Configuration') diff --git a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs index 6cf59808..115a5be3 100644 --- a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs +++ b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs @@ -89,7 +89,9 @@ namespace slskd.Configuration /// public override void Load() { - var dictionary = Arguments.Parse(CommandLine).ArgumentDictionary; + Console.WriteLine(CommandLine); + var dictionary = Arguments.Parse(CommandLine, TargetType).ArgumentDictionary; + Console.WriteLine(dictionary.ToJson()); void Map(Type type, string path) { diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs index 4a032c47..75391271 100644 --- a/src/slskd/Options.cs +++ b/src/slskd/Options.cs @@ -159,6 +159,14 @@ namespace slskd [RequiresRestart] public string InstanceName { get; private set; } = "default"; + [Argument(default, "list")] + [EnvironmentVariable("LIST")] + public string[] List { get; private set; } = Array.Empty(); + + [Argument(default, "list-of-ints")] + [EnvironmentVariable("LIST_OF_INTS")] + public int[] ListOfInts { get; private set; } = Array.Empty(); + /// /// Gets directory options. /// diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index d22ab433..9447bb7f 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -212,6 +212,9 @@ namespace slskd return; } + Console.WriteLine(OptionsAtStartup.List.ToJson()); + Console.WriteLine(OptionsAtStartup.ListOfInts.ToJson()); + try { VerifyOrCreateDirectory(OptionsAtStartup.Directories.App); -- cgit v1.2.3 From 22e4e494b0baeb1983057f0b89dd03f97dfcdfc0 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 26 Jul 2021 20:05:59 -0500 Subject: add support for MultiValuedArguments --- .../Common/Configuration/CommandLineConfigurationSource.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src/slskd/Common/Configuration') diff --git a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs index 115a5be3..b49b2232 100644 --- a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs +++ b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs @@ -33,9 +33,10 @@ namespace slskd.Configuration /// /// The to which to add. /// The type from which to map properties. + /// An array of argument names which can be specified with multiple values. /// The command line string from which to parse arguments. /// The updated . - public static IConfigurationBuilder AddCommandLine(this IConfigurationBuilder builder, Type targetType, string commandLine = null) + public static IConfigurationBuilder AddCommandLine(this IConfigurationBuilder builder, Type targetType, string[] multiValuedArguments = null, string commandLine = null) { if (builder == null) { @@ -51,6 +52,7 @@ namespace slskd.Configuration { s.TargetType = targetType; s.CommandLine = commandLine ?? Environment.CommandLine; + s.MultiValuedArguments = multiValuedArguments; }); } @@ -78,11 +80,13 @@ namespace slskd.Configuration TargetType = source.TargetType; Namespace = TargetType.Namespace.Split('.').First(); CommandLine = source.CommandLine; + MultiValuedArguments = source.MultiValuedArguments; } private string CommandLine { get; set; } private string Namespace { get; set; } private Type TargetType { get; set; } + private string[] MultiValuedArguments { get; set; } /// /// Parses command line arguments from the specified string and maps them to the corresponding keys. @@ -90,7 +94,7 @@ namespace slskd.Configuration public override void Load() { Console.WriteLine(CommandLine); - var dictionary = Arguments.Parse(CommandLine, TargetType).ArgumentDictionary; + var dictionary = Arguments.Parse(CommandLine, options => options.CombinableArguments = MultiValuedArguments).ArgumentDictionary; Console.WriteLine(dictionary.ToJson()); void Map(Type type, string path) @@ -149,6 +153,11 @@ namespace slskd.Configuration /// public Type TargetType { get; set; } + /// + /// Gets or sets an array of argument names which can be specified with multiple values. + /// + public string[] MultiValuedArguments { get; set; } = Array.Empty(); + /// /// Builds the for this source. /// -- cgit v1.2.3 From a0842636429bcecf87a2302ca8ebb685c77f8514 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 26 Jul 2021 22:58:41 -0500 Subject: handle multiple values --- .../CommandLineConfigurationSource.cs | 38 +++++++++++++++++++--- 1 file changed, 33 insertions(+), 5 deletions(-) (limited to 'src/slskd/Common/Configuration') diff --git a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs index b49b2232..d338eef6 100644 --- a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs +++ b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs @@ -18,6 +18,7 @@ namespace slskd.Configuration { using System; + using System.Collections.Generic; using System.Linq; using System.Reflection; using Microsoft.Extensions.Configuration; @@ -116,14 +117,41 @@ namespace slskd.Configuration { if (dictionary.ContainsKey(argument)) { - var value = dictionary[argument].ToString(); - - if (property.PropertyType == typeof(bool) && string.IsNullOrEmpty(value)) + // if the backing type is an array, it supports multiple values. + if (property.PropertyType.IsArray) { - value = "true"; + var value = dictionary[argument]; + + // Parse() will stuff multiple values into a List if the argument name is + // in the list of those supporting multiple values, and more than one value was supplied. + // detect this, and add the values to the target. + if (value.GetType().IsGenericType) + { + var elements = (List)dictionary[argument]; + + for (int i = 0; i < elements.Count; i++) + { + Data[ConfigurationPath.Combine(key, i.ToString())] = elements[i].ToString(); + } + } + else + { + // there may have only been one value supplied, in which case the value is just a + // string. stick it in index 0 of the target. + Data[ConfigurationPath.Combine(key, "0")] = value.ToString(); + } } + else + { + var value = dictionary[argument].ToString(); - Data[key] = value; + if (property.PropertyType == typeof(bool) && string.IsNullOrEmpty(value)) + { + value = "true"; + } + + Data[key] = value; + } } } } -- cgit v1.2.3 From 91fb504c2662eb5b374c19bd84962b557f4b5600 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 26 Jul 2021 23:23:21 -0500 Subject: don't populate defaults for arrays --- .../Common/Configuration/DefaultValueConfigurationSource.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'src/slskd/Common/Configuration') diff --git a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs index 15cf4070..b115d54d 100644 --- a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs +++ b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs @@ -99,11 +99,14 @@ namespace slskd.Configuration } else { - var value = property.GetValue(defaults); - - if (value != null) + if (!property.PropertyType.IsArray) { - Data[key] = value.ToString(); + var value = property.GetValue(defaults); + + if (value != null) + { + Data[key] = value.ToString(); + } } } } -- cgit v1.2.3 From 58e6918f92163c99edf8eedb1a78e23bf2b039f8 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 27 Jul 2021 07:54:53 -0500 Subject: clarify suppression of array handling --- src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/slskd/Common/Configuration') diff --git a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs index b115d54d..eae4b2eb 100644 --- a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs +++ b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs @@ -99,6 +99,8 @@ namespace slskd.Configuration } else { + // don't add array values to the configuration; these are additive across providers + // and the default value from the class is "stuck", so adding them again results in duplicates. if (!property.PropertyType.IsArray) { var value = property.GetValue(defaults); -- cgit v1.2.3 From c5a23135f01697e5fb234988a314be57c9e245d5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 27 Jul 2021 07:57:59 -0500 Subject: check for List<> to prevent regression --- src/slskd/Common/Configuration/CommandLineConfigurationSource.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/slskd/Common/Configuration') diff --git a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs index d338eef6..0436c922 100644 --- a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs +++ b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs @@ -94,9 +94,7 @@ namespace slskd.Configuration /// public override void Load() { - Console.WriteLine(CommandLine); var dictionary = Arguments.Parse(CommandLine, options => options.CombinableArguments = MultiValuedArguments).ArgumentDictionary; - Console.WriteLine(dictionary.ToJson()); void Map(Type type, string path) { @@ -125,7 +123,7 @@ namespace slskd.Configuration // Parse() will stuff multiple values into a List if the argument name is // in the list of those supporting multiple values, and more than one value was supplied. // detect this, and add the values to the target. - if (value.GetType().IsGenericType) + if (value.GetType().IsGenericType && value.GetType().GetGenericTypeDefinition() == typeof(List<>)) { var elements = (List)dictionary[argument]; -- cgit v1.2.3