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') 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') 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 2c7a7268a748fac3b676a31e99964f9f0979f0f5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 26 Jul 2021 20:05:01 -0500 Subject: upgrade Utility.CommandLine.Arguments --- src/slskd/slskd.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/slskd.csproj b/src/slskd/slskd.csproj index c2873938..44f4bf9c 100644 --- a/src/slskd/slskd.csproj +++ b/src/slskd/slskd.csproj @@ -75,7 +75,7 @@ - + -- 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') 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 e4a7073ea750c6bf3a82b35b81ba3883f8438fb4 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 26 Jul 2021 22:57:02 -0500 Subject: add GetPropertiesRecursively extension --- src/slskd/Common/Extensions.cs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src') diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs index 3e4f6e68..33faecf7 100644 --- a/src/slskd/Common/Extensions.cs +++ b/src/slskd/Common/Extensions.cs @@ -44,6 +44,30 @@ namespace slskd /// The new object deserialzied from the string. public static T ToObject(this string str) => JsonSerializer.Deserialize(str); + /// + /// Recursively retrieves all properties. + /// + /// The type from which to retrieve properties. + /// The list of properties. + public static IEnumerable GetPropertiesRecursively(this Type type) + { + var props = new List(); + + foreach (var prop in type.GetProperties()) + { + if (prop.PropertyType.IsPrimitive || prop.PropertyType.IsArray || Nullable.GetUnderlyingType(prop.PropertyType) != null || new[] { typeof(string), typeof(decimal) }.Contains(prop.PropertyType)) + { + props.Add(prop); + } + else + { + props.AddRange(prop.PropertyType.GetPropertiesRecursively()); + } + } + + return props; + } + /// /// Deeply compares this object with the specified object and returns a list of properties that are different. /// -- cgit v1.2.3 From c5804abb31bd74f26101ad28acdd5c12273d2a8f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 26 Jul 2021 22:57:27 -0500 Subject: find arguments backed by arrays and pass them as multiValuedArguments --- src/slskd/Program.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 9447bb7f..3821078b 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -335,6 +335,18 @@ namespace slskd { configurationFile = Path.GetFullPath(configurationFile); + var multiValuedArguments = typeof(Options) + .GetPropertiesRecursively() + .Where(p => p.PropertyType.IsArray) + .SelectMany(p => + p.CustomAttributes + .Where(a => a.AttributeType == typeof(ArgumentAttribute)) + .Select(a => new[] { a.ConstructorArguments[0].Value, a.ConstructorArguments[1].Value }) + .SelectMany(v => v)) + .Select(v => v.ToString()) + .Where(v => v != "\u0000") + .ToArray(); + return builder .AddDefaultValues( targetType: typeof(Options)) @@ -349,6 +361,7 @@ namespace slskd provider: new PhysicalFileProvider(Path.GetDirectoryName(configurationFile), ExclusionFilters.None)) // required for locations outside of the app directory .AddCommandLine( targetType: typeof(Options), + multiValuedArguments, commandLine: Environment.CommandLine); } -- 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') 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') 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 2e04802345be5d887e53351dafc13830f38a9cd7 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 26 Jul 2021 23:23:35 -0500 Subject: add short names --- src/slskd/Options.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs index 75391271..92b5dd49 100644 --- a/src/slskd/Options.cs +++ b/src/slskd/Options.cs @@ -159,11 +159,11 @@ namespace slskd [RequiresRestart] public string InstanceName { get; private set; } = "default"; - [Argument(default, "list")] + [Argument('y', "list")] [EnvironmentVariable("LIST")] public string[] List { get; private set; } = Array.Empty(); - [Argument(default, "list-of-ints")] + [Argument('b', "list-of-ints")] [EnvironmentVariable("LIST_OF_INTS")] public int[] ListOfInts { get; private set; } = Array.Empty(); -- cgit v1.2.3 From 281da1abdb3e8496142a37dcff57376e9672cf31 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 27 Jul 2021 07:46:38 -0500 Subject: handle arrays in DiffWith (#129) --- src/slskd/Common/Extensions.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs index 33faecf7..51c0d147 100644 --- a/src/slskd/Common/Extensions.cs +++ b/src/slskd/Common/Extensions.cs @@ -91,7 +91,14 @@ namespace slskd var propType = prop.PropertyType; var fqn = string.IsNullOrEmpty(parentFqn) ? prop.Name : string.Join(".", parentFqn, prop.Name); - if (propType.IsPrimitive || Nullable.GetUnderlyingType(propType) != null || new[] { typeof(string), typeof(decimal) }.Contains(propType)) + if (propType.IsArray) + { + if (leftVal.ToJson() != rightVal.ToJson()) + { + differences.Add((prop, fqn, leftVal, rightVal)); + } + } + else if (propType.IsPrimitive || Nullable.GetUnderlyingType(propType) != null || new[] { typeof(string), typeof(decimal) }.Contains(propType)) { if (!Equals(leftVal, rightVal)) { -- cgit v1.2.3 From 907ea3b672c4144d8bdbd4c38f747ef85ac54579 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 27 Jul 2021 07:46:53 -0500 Subject: log json when logging changes --- src/slskd/Application/Service.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Application/Service.cs b/src/slskd/Application/Service.cs index 62f6eb32..1d2aec25 100644 --- a/src/slskd/Application/Service.cs +++ b/src/slskd/Application/Service.cs @@ -214,7 +214,7 @@ namespace slskd var requiresRestart = HasAttribute(property); var requiresReconnect = HasAttribute(property); - Logger.Debug($"{fqn} changed from '{left ?? ""}' to '{right ?? ""}'{(requiresRestart ? "; Restart required to take effect." : string.Empty)}{(requiresReconnect ? "; Reconnect required to take effect." : string.Empty)}"); + Logger.Debug($"{fqn} changed from '{left.ToJson() ?? ""}' to '{right.ToJson() ?? ""}'{(requiresRestart ? "; Restart required to take effect." : string.Empty)}{(requiresReconnect ? "; Reconnect required to take effect." : string.Empty)}"); pendingRestart |= requiresRestart; pendingReconnect |= requiresReconnect; -- cgit v1.2.3 From e9b14cbcef276cae621d9b2093bf00b9188cfdeb Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 27 Jul 2021 07:47:09 -0500 Subject: remove test options --- src/slskd/Options.cs | 8 -------- 1 file changed, 8 deletions(-) (limited to 'src') diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs index 92b5dd49..4a032c47 100644 --- a/src/slskd/Options.cs +++ b/src/slskd/Options.cs @@ -159,14 +159,6 @@ namespace slskd [RequiresRestart] public string InstanceName { get; private set; } = "default"; - [Argument('y', "list")] - [EnvironmentVariable("LIST")] - public string[] List { get; private set; } = Array.Empty(); - - [Argument('b', "list-of-ints")] - [EnvironmentVariable("LIST_OF_INTS")] - public int[] ListOfInts { get; private set; } = Array.Empty(); - /// /// Gets directory options. /// -- cgit v1.2.3 From b02e2becd04d3d6a73e15cb09e92386ab2e8a7fd Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 27 Jul 2021 07:54:41 -0500 Subject: remove logging --- src/slskd/Program.cs | 3 --- 1 file changed, 3 deletions(-) (limited to 'src') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 3821078b..b5275756 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -212,9 +212,6 @@ namespace slskd return; } - Console.WriteLine(OptionsAtStartup.List.ToJson()); - Console.WriteLine(OptionsAtStartup.ListOfInts.ToJson()); - try { VerifyOrCreateDirectory(OptionsAtStartup.Directories.App); -- 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') 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') 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