From fbc6e54f641f204b7c9668698638ca1729ae8aa0 Mon Sep 17 00:00:00 2001 From: Sven Nierlein Date: Fri, 28 Jul 2023 09:46:56 +0200 Subject: [PATCH] fix testcmd when command contains spaces --- mod_gearman_worker_testcmd.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/mod_gearman_worker_testcmd.go b/mod_gearman_worker_testcmd.go index 92ee495..9ca8f1e 100644 --- a/mod_gearman_worker_testcmd.go +++ b/mod_gearman_worker_testcmd.go @@ -11,7 +11,7 @@ func runTestCmd(conf *configurationStruct, args []string) (rc int, output string typ: "service", hostName: "test check from commandline", serviceDescription: "test", - commandLine: strings.Join(args, " "), + commandLine: buildCommandLine(args), } if len(args) == 0 { return 3, "usage: mod_gearman_worker [--job_timeout=seconds] testcmd " @@ -32,3 +32,14 @@ func runTestCmd(conf *configurationStruct, args []string) (rc int, output string logger.Debugf("test cmd rc: %d\n", rc) return } + +// reconstruct command line from array of args +func buildCommandLine(args []string) string { + cmd := args[0] + for _, a := range args[1:] { + // escape quotes + a = strings.ReplaceAll(a, `"`, `\"`) + cmd = cmd + ` "` + a + `"` + } + return cmd +}