From 334627cd06476df897cf3f3ec02ca70b51b7fec1 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 26 Oct 2024 18:27:34 -0500 Subject: allow one-word scripts --- src/slskd/Integrations/Scripts/ScriptService.cs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src/slskd/Integrations/Scripts') diff --git a/src/slskd/Integrations/Scripts/ScriptService.cs b/src/slskd/Integrations/Scripts/ScriptService.cs index 4e086148..48916b2d 100644 --- a/src/slskd/Integrations/Scripts/ScriptService.cs +++ b/src/slskd/Integrations/Scripts/ScriptService.cs @@ -67,10 +67,18 @@ public class ScriptService try { - // todo: update options validation to ensure run strings have at least 2 words var run = script.Value.Run; - var executable = run.Split(" ", 2)[0]; - var args = run.Split(" ", 2)[1].Replace("$DATA", data.ToJson()); + + // split the string into at most 2 parts, leaving part [0] the executable and part [1] the rest of the string, but possibly null + var parts = run.Split(" ", count: 2, options: StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.RemoveEmptyEntries); + + var executable = parts[0]; + string args = default; + + if (parts.Length > 1) + { + args = parts[1].Replace("$DATA", data.ToJson()); + } Log.Debug("Running script '{Script}': {Run}", script.Key, run); var sw = Stopwatch.StartNew(); @@ -87,7 +95,12 @@ public class ScriptService }; process.Start(); - process.StandardInput.WriteLine($"{args}"); + + if (args is not null) + { + process.StandardInput.WriteLine(args); + } + process.StandardInput.WriteLine("exit"); process.WaitForExit(); -- cgit v1.2.3