From 2a16835625a3d358e36a52388386eede8fa94089 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 12 May 2021 08:55:54 -0500 Subject: add app/incomplete directories to config --- src/slskd/Options.cs | 30 ++++++++++++++++++++++-------- src/slskd/Properties/slskd.yml | 4 +++- 2 files changed, 25 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs index 527cfc42..4777cb9f 100644 --- a/src/slskd/Options.cs +++ b/src/slskd/Options.cs @@ -127,13 +127,20 @@ namespace slskd public class DirectoriesOptions { /// - /// Gets the path to shared files. + /// Gets the path where application data is saved. /// - [Argument('s', "shared")] - [EnvironmentVariable("SHARED_DIR")] - [Description("path to shared files")] - [Required] - public string Shared { get; private set; } = null; + [Argument(default, "app")] + [EnvironmentVariable("APP_DIR")] + [Description("path where application data is saved")] + public string App { get; private set; } = Program.DefaultAppDirectory; + + /// + /// Gets the path where incomplete downloads are saved. + /// + [Argument(default, "incomplete")] + [EnvironmentVariable("INCOMPLETE_DIR")] + [Description("path where incomplete downloads are saved")] + public string Incomplete { get; private set; } = Program.DefaultIncompleteDirectory; /// /// Gets the path where downloaded files are saved. @@ -141,8 +148,15 @@ namespace slskd [Argument('o', "downloads")] [EnvironmentVariable("DOWNLOADS_DIR")] [Description("path where downloaded files are saved")] - [Required] - public string Downloads { get; private set; } = null; + public string Downloads { get; private set; } = Program.DefaultDownloadsDirectory; + + /// + /// Gets the path to shared files. + /// + [Argument('s', "shared")] + [EnvironmentVariable("SHARED_DIR")] + [Description("path to shared files")] + public string Shared { get; private set; } = Program.DefaultSharedDirectory; } /// diff --git a/src/slskd/Properties/slskd.yml b/src/slskd/Properties/slskd.yml index d5519b65..c049aa7a 100644 --- a/src/slskd/Properties/slskd.yml +++ b/src/slskd/Properties/slskd.yml @@ -20,8 +20,10 @@ # key: ~ # ttl: 604800000 # directories: -# shared: ~ +# app: ~ +# incomplete: ~ # downloads: ~ +# shared: ~ # logger: # loki: ~ # feature: -- cgit v1.2.3 From 37e486797ac99f42dab6ef5231f31c9c8ba9ff6d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 12 May 2021 09:24:03 -0500 Subject: log listen port at startup --- src/slskd/Service.cs | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/slskd/Service.cs b/src/slskd/Service.cs index 95c04595..8cb96412 100644 --- a/src/slskd/Service.cs +++ b/src/slskd/Service.cs @@ -127,6 +127,7 @@ namespace slskd await Client.ConnectAsync(Options.Soulseek.Username, Options.Soulseek.Password).ConfigureAwait(false); Logger.Information("Connected and logged in as {Username}", Options.Soulseek.Username); + Logger.Information("Listening on port {Port}", Options.Soulseek.ListenPort); Logger.Information("Client started"); } -- cgit v1.2.3 From ccc8dd5b45a3e95c232ef790f27047ad98bb3885 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 12 May 2021 09:45:01 -0500 Subject: add DefaultAppDirectory --- src/slskd/Program.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 00d6d5b4..9998cd39 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -52,9 +52,9 @@ namespace slskd public static readonly string AppName = "slskd"; /// - /// The default database filename. + /// The default application data directory. /// - public static readonly string DefaultDatabaseFile = Path.Combine(AppContext.BaseDirectory, "data", $"{AppName}.db"); + public static readonly string DefaultAppDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), AppName); /// /// The default configuration filename. -- cgit v1.2.3 From 3aee2dcd11b80f21d5845f817166cbe184abd337 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 12 May 2021 09:45:37 -0500 Subject: default shared, downloads and incomplete dirs to app subdirectories --- src/slskd/Program.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 9998cd39..bf864d10 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -64,7 +64,21 @@ namespace slskd /// /// The default XML documentation filename. /// - public static readonly string DefaultXmlDocumentaitonFile = Path.Combine(AppContext.BaseDirectory, "etc", $"{AppName}.xml"); + + /// + /// The default incomplete download directory. + /// + public static readonly string DefaultIncompleteDirectory = Path.Combine(DefaultAppDirectory, "incomplete"); + + /// + /// The default downloads directory. + /// + public static readonly string DefaultDownloadsDirectory = Path.Combine(DefaultAppDirectory, "downloads"); + + /// + /// The default shared directory. + /// + public static readonly string DefaultSharedDirectory = Path.Combine(DefaultAppDirectory, "shared"); /// /// The global prefix for environment variables. -- cgit v1.2.3 From c12ab7abf3a7591df11ea589e9cb8c16b65aaa3b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 12 May 2021 09:45:52 -0500 Subject: fix typo --- src/slskd/Program.cs | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index bf864d10..a3b16af1 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -64,6 +64,7 @@ namespace slskd /// /// The default XML documentation filename. /// + public static readonly string DefaultXmlDocumentationFile = Path.Combine(AppContext.BaseDirectory, "etc", $"{AppName}.xml"); /// /// The default incomplete download directory. -- cgit v1.2.3 From 5ddef023a9dbaf1ace7e534d5c45bee7094d8f16 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 12 May 2021 09:46:15 -0500 Subject: ensure app directory exists and is writeable --- src/slskd/Program.cs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'src') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index a3b16af1..e62a3529 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -213,6 +213,37 @@ namespace slskd return; } + // ensure the application directory exists and is writeable. the most comprehensive way to test this is to try + // to write a file to it. + try + { + if (!Directory.Exists(Options.Directories.App)) + { + Directory.CreateDirectory(Options.Directories.App); + } + } + catch (Exception ex) + { + Console.WriteLine($"App directory {Options.Directories.App} does not exist, and could not be created: {ex.Message}"); + } + + try + { + if (!Directory.Exists(Options.Directories.App)) + { + Directory.CreateDirectory(Options.Directories.App); + } + + var probe = Path.Combine(Options.Directories.App, "probe"); + File.WriteAllText(Path.Combine(Options.Directories.App, "probe"), string.Empty); + File.Delete(probe); + } + catch (Exception ex) + { + Console.WriteLine($"App directory {Options.Directories.App} is not writeable: {ex.Message}"); + return; + } + if (!Options.NoLogo) { PrintLogo(Version); -- cgit v1.2.3 From b564075e251b5fc4dbe2d1f801ed24523575437b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 12 May 2021 09:46:45 -0500 Subject: fix typo --- src/slskd/Program.cs | 2 +- src/slskd/Startup.cs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index e62a3529..a9f4ef94 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs index f7787409..c63699cf 100644 --- a/src/slskd/Startup.cs +++ b/src/slskd/Startup.cs @@ -145,13 +145,13 @@ namespace slskd Version = "v0", }); - if (File.Exists(Program.DefaultXmlDocumentaitonFile)) + if (File.Exists(Program.DefaultXmlDocumentationFile)) { - options.IncludeXmlComments(Program.DefaultXmlDocumentaitonFile); + options.IncludeXmlComments(Program.DefaultXmlDocumentationFile); } else { - logger.Warning($"Unable to find XML documentation in {Program.DefaultXmlDocumentaitonFile}, Swagger will not include metadata"); + logger.Warning($"Unable to find XML documentation in {Program.DefaultXmlDocumentationFile}, Swagger will not include metadata"); } }); } -- cgit v1.2.3 From 7e16e265091480ba197c21563b9ebc30029cae1f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 13 May 2021 18:29:28 -0500 Subject: disable some annoying rules --- src/slskd/Properties/analysis.ruleset | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Properties/analysis.ruleset b/src/slskd/Properties/analysis.ruleset index 6605f525..7bec422b 100644 --- a/src/slskd/Properties/analysis.ruleset +++ b/src/slskd/Properties/analysis.ruleset @@ -18,7 +18,7 @@ - + @@ -38,6 +38,7 @@ + -- cgit v1.2.3 From 64fda245a090e78b89935e33294556a4f012388a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 13 May 2021 18:30:54 -0500 Subject: make local filename resolution extension methods --- src/slskd/Common/Extensions.cs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'src') diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs index b7243549..1fb8f761 100644 --- a/src/slskd/Common/Extensions.cs +++ b/src/slskd/Common/Extensions.cs @@ -86,6 +86,41 @@ namespace slskd sizeSuffixes[mag]); } + /// + /// Converts a fully qualified remote filename to a local filename, swapping directory characters for those specific + /// to the local OS, removing any characters that are invalid for the local OS, and making the path relative to the + /// remote store (including the filename and the parent folder). + /// + /// The fully qualified remote filename to convert. + /// The converted filename. + public static string ToLocalRelativeFilename(this string remoteFilename) + { + var localFilename = remoteFilename.ToLocalOSPath(); + var path = $"{Path.GetDirectoryName(localFilename).Replace(Path.GetDirectoryName(Path.GetDirectoryName(localFilename)), string.Empty)}"; + + var sanitizedFilename = Path.GetFileName(localFilename); + + foreach (var c in Path.GetInvalidFileNameChars()) + { + sanitizedFilename = sanitizedFilename.Replace(c, '_'); + } + + return Path.Combine(path, sanitizedFilename).TrimStart('\\'); + } + + /// + /// Converts a fully qualified remote filename to a local filename based in the provided , swapping directory characters for those specific + /// to the local OS, removing any characters that are invalid for the local OS, and making the path relative to the + /// remote store (including the filename and the parent folder). + /// + /// The fully qualified remote filename to convert. + /// The base directory for the local filename. + /// The converted filename. + public static string ToLocalFilename(this string remoteFilename, string baseDirectory) + { + return Path.Combine(baseDirectory, remoteFilename.ToLocalRelativeFilename()); + } + /// /// Converts the given path to the local format (normalizes path separators). /// -- cgit v1.2.3 From fe2630a45dd45a40070653f810f6cfc1d9d41f92 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 13 May 2021 18:31:56 -0500 Subject: move files on completion --- src/slskd/API/Controllers/TransfersController.cs | 57 +++++++++++++++--------- src/slskd/Trackers/TransferTracker.cs | 2 +- 2 files changed, 37 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/slskd/API/Controllers/TransfersController.cs b/src/slskd/API/Controllers/TransfersController.cs index 948bf7ff..8ba1ef72 100644 --- a/src/slskd/API/Controllers/TransfersController.cs +++ b/src/slskd/API/Controllers/TransfersController.cs @@ -2,18 +2,17 @@ namespace slskd.API.Controllers { - using Microsoft.AspNetCore.Authorization; - using Microsoft.AspNetCore.Mvc; - using Soulseek; using System; using System.ComponentModel.DataAnnotations; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; + using Microsoft.AspNetCore.Authorization; + using Microsoft.AspNetCore.Mvc; using slskd.API.DTO; using slskd.Trackers; - using slskd.Configuration; + using Soulseek; /// /// Transfers @@ -33,13 +32,13 @@ namespace slskd.API.Controllers /// public TransfersController(IOptionsSnapshot options, ISoulseekClient client, ITransferTracker tracker) { - OutputDirectory = options.Value.Directories.Downloads; Client = client; Tracker = tracker; + Options = options.Value; } + private Options Options { get; } private ISoulseekClient Client { get; } - private string OutputDirectory { get; } private ITransferTracker Tracker { get; } /// @@ -97,19 +96,24 @@ namespace slskd.API.Controllers try { var waitUntilEnqueue = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - var stream = GetLocalFileStream(request.Filename, OutputDirectory); + var stream = GetLocalFileStream(request.Filename, Options.Directories.Incomplete); var cts = new CancellationTokenSource(); - var downloadTask = Client.DownloadAsync(username, request.Filename, stream, request.Size, 0, request.Token, new TransferOptions(disposeOutputStreamOnCompletion: true, stateChanged: (e) => + var downloadTask = Task.Run(async () => { - Tracker.AddOrUpdate(e, cts); - - if (e.Transfer.State == TransferStates.Queued || e.Transfer.State == TransferStates.Initializing) + await Client.DownloadAsync(username, request.Filename, stream, request.Size, 0, request.Token, new TransferOptions(disposeOutputStreamOnCompletion: true, stateChanged: (e) => { - waitUntilEnqueue.TrySetResult(true); - } - }, progressUpdated: (e) => Tracker.AddOrUpdate(e, cts)), cts.Token); + Tracker.AddOrUpdate(e, cts); + + if (e.Transfer.State == TransferStates.Queued || e.Transfer.State == TransferStates.Initializing) + { + waitUntilEnqueue.TrySetResult(true); + } + }, progressUpdated: (e) => Tracker.AddOrUpdate(e, cts)), cts.Token); + + MoveFile(request.Filename, Options.Directories.Incomplete, Options.Directories.Downloads); + }); // wait until either the waitUntilEnqueue task completes because the download was successfully queued, or the // downloadTask throws due to an error prior to successfully queueing. @@ -246,24 +250,35 @@ namespace slskd.API.Controllers private static FileStream GetLocalFileStream(string remoteFilename, string saveDirectory) { - var localFilename = remoteFilename.ToLocalOSPath(); - var path = $"{saveDirectory}{Path.DirectorySeparatorChar}{Path.GetDirectoryName(localFilename).Replace(Path.GetDirectoryName(Path.GetDirectoryName(localFilename)), "")}"; + var localFilename = remoteFilename.ToLocalFilename(baseDirectory: saveDirectory); + var path = Path.GetDirectoryName(localFilename); if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } - var sanitizedFilename = Path.GetFileName(localFilename); + return new FileStream(localFilename, FileMode.Create); + } + + private static void MoveFile(string filename, string sourceDirectory, string destinationDirectory) + { + var sourceFilename = filename.ToLocalFilename(sourceDirectory); + var destinationFilename = filename.ToLocalFilename(destinationDirectory); - foreach (var c in Path.GetInvalidFileNameChars()) + var destinationPath = Path.GetDirectoryName(destinationFilename); + + if (!System.IO.Directory.Exists(destinationPath)) { - sanitizedFilename = sanitizedFilename.Replace(c, '_'); + System.IO.Directory.CreateDirectory(destinationPath); } - localFilename = Path.Combine(path, sanitizedFilename); + System.IO.File.Move(sourceFilename, destinationFilename, overwrite: true); - return new FileStream(localFilename, FileMode.Create); + if (!System.IO.Directory.EnumerateFileSystemEntries(Path.GetDirectoryName(sourceFilename)).Any()) + { + System.IO.Directory.Delete(Path.GetDirectoryName(sourceFilename)); + } } private IActionResult CancelTransfer(TransferDirection direction, string username, string id, bool remove = false) diff --git a/src/slskd/Trackers/TransferTracker.cs b/src/slskd/Trackers/TransferTracker.cs index 6efa7345..270f25cb 100644 --- a/src/slskd/Trackers/TransferTracker.cs +++ b/src/slskd/Trackers/TransferTracker.cs @@ -71,7 +71,7 @@ namespace slskd.Trackers directedTransfers.TryGetValue(username, out var transfers); return transfers ?? new ConcurrentDictionary(); } - + /// /// Maps a Transfer collection to a serializable object. /// -- cgit v1.2.3 From fc2c40a0f029f7c632d8aa798e858420696d7fd8 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 13 May 2021 18:32:22 -0500 Subject: only report speed on successful uploads --- src/slskd/Service.cs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/slskd/Service.cs b/src/slskd/Service.cs index 8cb96412..7f5c1901 100644 --- a/src/slskd/Service.cs +++ b/src/slskd/Service.cs @@ -250,17 +250,18 @@ namespace slskd private void Client_TransferStateChanged(object sender, TransferStateChangedEventArgs args) { - var direction = args.Transfer.Direction.ToString().ToUpper(); - var user = args.Transfer.Username; - var file = Path.GetFileName(args.Transfer.Filename); + var xfer = args.Transfer; + var direction = xfer.Direction.ToString().ToUpper(); + var user = xfer.Username; + var file = Path.GetFileName(xfer.Filename); var oldState = args.PreviousState; - var state = args.Transfer.State; + var state = xfer.State; - var completed = args.Transfer.State.HasFlag(TransferStates.Completed); + var completed = xfer.State.HasFlag(TransferStates.Completed); - Console.WriteLine($"[{direction}] [{user}/{file}] {oldState} => {state}{(completed ? $" ({args.Transfer.BytesTransferred}/{args.Transfer.Size} = {args.Transfer.PercentComplete}%) @ {args.Transfer.AverageSpeed.SizeSuffix()}/s" : string.Empty)}"); + Console.WriteLine($"[{direction}] [{user}/{file}] {oldState} => {state}{(completed ? $" ({xfer.BytesTransferred}/{xfer.Size} = {xfer.PercentComplete}%) @ {xfer.AverageSpeed.SizeSuffix()}/s" : string.Empty)}"); - if (args.Transfer.State.HasFlag(TransferStates.Succeeded) && args.Transfer.Direction == TransferDirection.Upload) + if (xfer.Direction == TransferDirection.Upload && xfer.State.HasFlag(TransferStates.Completed | TransferStates.Succeeded)) { _ = Client.SendUploadSpeedAsync((int)args.Transfer.AverageSpeed); } -- cgit v1.2.3 From 2c9bed98cdb95ef031930a7a1b369e8de4be604a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 14 May 2021 11:54:02 -0500 Subject: trim forward slashes --- src/slskd/Common/Extensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs index 1fb8f761..4e08ed02 100644 --- a/src/slskd/Common/Extensions.cs +++ b/src/slskd/Common/Extensions.cs @@ -105,7 +105,7 @@ namespace slskd sanitizedFilename = sanitizedFilename.Replace(c, '_'); } - return Path.Combine(path, sanitizedFilename).TrimStart('\\'); + return Path.Combine(path, sanitizedFilename).TrimStart('\\').TrimStart('/'); } /// -- cgit v1.2.3 From 7c406b0a2d0efb4c1472a0a8651575f9875a162f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 14 May 2021 12:58:57 -0500 Subject: disable s3011 --- src/slskd/Properties/analysis.ruleset | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/slskd/Properties/analysis.ruleset b/src/slskd/Properties/analysis.ruleset index 7bec422b..a57b2b42 100644 --- a/src/slskd/Properties/analysis.ruleset +++ b/src/slskd/Properties/analysis.ruleset @@ -32,6 +32,7 @@ + -- cgit v1.2.3 From 691dcadcd97921c244e8c33d4fececcb52dd97e3 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 14 May 2021 12:59:26 -0500 Subject: use StringLength instead of Required for options with defaults --- src/slskd/Options.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs index 4777cb9f..6074d650 100644 --- a/src/slskd/Options.cs +++ b/src/slskd/Options.cs @@ -371,7 +371,7 @@ namespace slskd [Argument(default, "content-path")] [EnvironmentVariable("CONTENT_PATH")] [Description("path to static web content")] - [Required] + [StringLength(255, MinimumLength = 1)] public string ContentPath { get; private set; } = "wwwroot"; /// @@ -399,7 +399,7 @@ namespace slskd [Argument('u', "username")] [EnvironmentVariable("USERNAME")] [Description("username for web UI")] - [Required] + [StringLength(255, MinimumLength = 1)] public string Username { get; private set; } = Program.AppName; /// @@ -408,7 +408,7 @@ namespace slskd [Argument('p', "password")] [EnvironmentVariable("PASSWORD")] [Description("password for web UI")] - [Required] + [StringLength(255, MinimumLength = 1)] public string Password { get; private set; } = Program.AppName; /// @@ -428,7 +428,7 @@ namespace slskd [Argument(default, "jwt-key")] [EnvironmentVariable("JWT_KEY")] [Description("JWT signing key")] - [Required] + [StringLength(255, MinimumLength = 1)] public string Key { get; private set; } = Guid.NewGuid().ToString(); /// -- cgit v1.2.3 From c89ea31876643110296e7cad7c09bd8027ce4685 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 14 May 2021 12:59:50 -0500 Subject: indicate required options in help --- src/slskd/Program.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index a9f4ef94..8035c010 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -20,6 +20,7 @@ namespace slskd using System; using System.Collections.Generic; using System.ComponentModel; + using System.ComponentModel.DataAnnotations; using System.IO; using System.Linq; using System.Net; @@ -420,6 +421,7 @@ namespace slskd { var attribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(ArgumentAttribute)); var descriptionAttribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(DescriptionAttribute)); + var isRequired = property.CustomAttributes.Any(a => a.AttributeType == typeof(RequiredAttribute)); if (attribute != default) { @@ -427,9 +429,9 @@ namespace slskd var longName = (string)attribute.ConstructorArguments[1].Value; var description = descriptionAttribute?.ConstructorArguments[0].Value; - var suffix = property.PropertyType == typeof(bool) ? string.Empty : $" (default: {property.GetValue(defaults) ?? ""})"; - var item = $"{shortName}{(shortName == default ? " " : "|")}--{GetLongName(longName, property.PropertyType)}"; - var desc = $"{description}{suffix}"; + var suffix = isRequired ? " (required)" : $" (default: {property.GetValue(defaults) ?? ""})"; + var item = $"{(shortName == default ? " " : $"{shortName}|")}--{GetLongName(longName, property.PropertyType)}"; + var desc = $"{description}{(property.PropertyType == typeof(bool) ? string.Empty : suffix)}"; lines.Add(new(item, desc)); } else @@ -467,15 +469,16 @@ namespace slskd { var attribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(EnvironmentVariableAttribute)); var descriptionAttribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(DescriptionAttribute)); + var isRequired = property.CustomAttributes.Any(a => a.AttributeType == typeof(RequiredAttribute)); if (attribute != default) { var name = (string)attribute.ConstructorArguments[0].Value; var description = descriptionAttribute?.ConstructorArguments[0].Value; - var suffix = type == typeof(bool) ? string.Empty : $" (default: {property.GetValue(defaults) ?? ""})"; + var suffix = isRequired ? " (required)" : $" (default: {property.GetValue(defaults) ?? ""})"; var item = $"{prefix}{GetName(name, property.PropertyType)}"; - var desc = $"{description}{suffix}"; + var desc = $"{description} {(type == typeof(bool) ? string.Empty : suffix)}"; lines.Add(new(item, desc)); } else -- cgit v1.2.3 From 1dfcbf0975f428446951e6a03f4ac259dc0047d1 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 14 May 2021 13:46:42 -0500 Subject: remove redundant space --- src/slskd/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 8035c010..d70b50c1 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -478,7 +478,7 @@ namespace slskd var suffix = isRequired ? " (required)" : $" (default: {property.GetValue(defaults) ?? ""})"; var item = $"{prefix}{GetName(name, property.PropertyType)}"; - var desc = $"{description} {(type == typeof(bool) ? string.Empty : suffix)}"; + var desc = $"{description}{(type == typeof(bool) ? string.Empty : suffix)}"; lines.Add(new(item, desc)); } else -- cgit v1.2.3