aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-07-27 06:38:39 -0500
committerJP Dillingham <jpdillingham@gmail.com>2021-07-27 06:38:39 -0500
commit742c8c8a77d28eccda886f0e6ca15d4a05a3cf4d (patch)
tree0b7cb4d34aa4a5b9809e503f221062bfa5c95bbb /src
parent48d569da0e86f1ec0144713c25cad3a5e767c4d2 (diff)
parent2e04802345be5d887e53351dafc13830f38a9cd7 (diff)
downloadslskd-742c8c8a77d28eccda886f0e6ca15d4a05a3cf4d.tar.gz
Merge branch 'lists' of https://github.com/slskd/slskd into lists
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Common/Configuration/CommandLineConfigurationSource.cs38
-rw-r--r--src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs11
-rw-r--r--src/slskd/Common/Extensions.cs24
-rw-r--r--src/slskd/Options.cs4
-rw-r--r--src/slskd/Program.cs14
5 files changed, 78 insertions, 13 deletions
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<T> 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<object>)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;
+ }
}
}
}
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();
+ }
}
}
}
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
@@ -45,6 +45,30 @@ namespace slskd
public static T ToObject<T>(this string str) => JsonSerializer.Deserialize<T>(str);
/// <summary>
+ /// Recursively retrieves all properties.
+ /// </summary>
+ /// <param name="type">The type from which to retrieve properties.</param>
+ /// <returns>The list of properties.</returns>
+ public static IEnumerable<PropertyInfo> GetPropertiesRecursively(this Type type)
+ {
+ var props = new List<PropertyInfo>();
+
+ 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;
+ }
+
+ /// <summary>
/// Deeply compares this object with the specified object and returns a list of properties that are different.
/// </summary>
/// <param name="left">The left side of the comparison.</param>
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<string>();
- [Argument(default, "list-of-ints")]
+ [Argument('b', "list-of-ints")]
[EnvironmentVariable("LIST_OF_INTS")]
public int[] ListOfInts { get; private set; } = Array.Empty<int>();
diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs
index a458d0ca..3821078b 100644
--- a/src/slskd/Program.cs
+++ b/src/slskd/Program.cs
@@ -335,7 +335,17 @@ namespace slskd
{
configurationFile = Path.GetFullPath(configurationFile);
- // todo: write some code here to fetch any properties in options marked with `Attribute` and backed by an array. stuff those arguments into a list for multiValuedArgumetns
+ 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(
@@ -351,7 +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: new[] { "t", "list", "list-of-ints" },
+ multiValuedArguments,
commandLine: Environment.CommandLine);
}