aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Core
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2025-03-01 19:01:00 -0600
committerGitHub <noreply@github.com>2025-03-01 19:01:00 -0600
commit4de21d94899605accdba680eeff95b8dc38b59e2 (patch)
tree58f49ae44bbf246f686cec7bda3dff562774fa66 /src/slskd/Core
parentbd3f8adf85bc5a1e6235af518de40001a25fba10 (diff)
parent9c50ae8b5d6e9b6de3d2a3fa26ed6317f50c4433 (diff)
downloadslskd-4de21d94899605accdba680eeff95b8dc38b59e2.tar.gz
Merge pull request #1292 from slskd/scripts
Refactor user-defined scripts
Diffstat (limited to 'src/slskd/Core')
-rw-r--r--src/slskd/Core/Options.cs61
1 files changed, 53 insertions, 8 deletions
diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs
index ff9cc407..266d04aa 100644
--- a/src/slskd/Core/Options.cs
+++ b/src/slskd/Core/Options.cs
@@ -2107,14 +2107,10 @@ namespace slskd
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
- var results = new List<ValidationResult>();
-
if (!Url.StartsWith("http://", StringComparison.OrdinalIgnoreCase) && !Url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
{
- results.Add(new ValidationResult($"The {nameof(Url)} field must contain a fully qualified URL, including protocol (e.g. http:// or https://)"));
+ yield return new ValidationResult($"The {nameof(Url)} field must contain a fully qualified URL, including protocol (e.g. http:// or https://)");
}
-
- return results;
}
}
@@ -2161,9 +2157,58 @@ namespace slskd
/// <summary>
/// Gets the shell script to invoke.
/// </summary>
- [StringLength(int.MaxValue, MinimumLength = 1)]
- [NotNullOrWhiteSpace]
- public string Run { get; init; }
+ public ScriptRunOptions Run { get; init; } = new ScriptRunOptions();
+ }
+
+ /// <summary>
+ /// Script run options.
+ /// </summary>
+ public class ScriptRunOptions : IValidatableObject
+ {
+ /// <summary>
+ /// Gets the shell command to run.
+ /// </summary>
+ public string Command { get; init; }
+
+ /// <summary>
+ /// Gets the executable to start.
+ /// </summary>
+ public string Executable { get; init; }
+
+ /// <summary>
+ /// Gets the arguments to pass to the executable.
+ /// </summary>
+ /// <remarks>
+ /// Mutually exclusive with <see cref="Arglist"/>.
+ /// </remarks>
+ public string Args { get; init; }
+
+ /// <summary>
+ /// Gets the list of arguments to pass to the executable.
+ /// </summary>
+ /// <remarks>
+ /// Mutually exclusive with <see cref="Args"/>.
+ /// </remarks>
+ public string[] Arglist { get; init; } = null;
+
+ public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
+ {
+ var cmdIsSet = !string.IsNullOrWhiteSpace(Command);
+ var exeIsSet = !string.IsNullOrWhiteSpace(Executable);
+
+ if ((cmdIsSet && exeIsSet) || (!cmdIsSet && !exeIsSet))
+ {
+ yield return new ValidationResult($"One and only one of the fields {nameof(Command)} or {nameof(Executable)} may be specified for a single script. If you intend to use the system shell, omit 'executable'. If you intend to use an executable other than the system shell, omit 'command' and specify either 'args' or 'args_list'.");
+ }
+
+ var argsIsSet = !string.IsNullOrWhiteSpace(Args);
+ var argsListIsSet = Arglist is not null;
+
+ if (argsIsSet && argsListIsSet)
+ {
+ yield return new ValidationResult($"Only one of the fields {nameof(Args)} or {nameof(Arglist)} may be specified for a single script. Specify 'args' if you intend to construct a single quoted string yourself, and specify 'args_list' if you'd like slskd to handle quoting for you.");
+ }
+ }
}
/// <summary>