From 9ab914263a3e5d613f0ecbb0e45c570aad6baae0 Mon Sep 17 00:00:00 2001 From: Anthony Pauriche Date: Mon, 29 Sep 2025 15:15:05 +0200 Subject: [PATCH 1/3] use shell to run commands --- helpers/command.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/helpers/command.go b/helpers/command.go index 35e4bd5..33f0cfd 100644 --- a/helpers/command.go +++ b/helpers/command.go @@ -6,7 +6,6 @@ import ( "errors" "io" "os/exec" - "strings" "time" "github.com/mitchellh/go-linereader" @@ -18,12 +17,7 @@ import ( func RunCommand(ctx context.Context, command string, doneFn func(output string)) error { var cmd *exec.Cmd - parts := strings.Fields(command) - if len(parts) == 1 { - cmd = exec.CommandContext(ctx, parts[0]) // #nosec G204 - } else { - cmd = exec.CommandContext(ctx, parts[0], parts[1:]...) // #nosec G204 - } + cmd = exec.CommandContext(ctx, "sh", "-c", command) // #nosec G204 // Create a pipe to read the output from. pr, pw := io.Pipe() From 52b860a11878b91e4a3c58ed35b0950238b8fc58 Mon Sep 17 00:00:00 2001 From: Anthony Pauriche Date: Mon, 29 Sep 2025 15:32:19 +0200 Subject: [PATCH 2/3] remove added carriage return --- helpers/command.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/helpers/command.go b/helpers/command.go index 33f0cfd..9c9f413 100644 --- a/helpers/command.go +++ b/helpers/command.go @@ -6,6 +6,7 @@ import ( "errors" "io" "os/exec" + "strings" "time" "github.com/mitchellh/go-linereader" @@ -17,6 +18,7 @@ import ( func RunCommand(ctx context.Context, command string, doneFn func(output string)) error { var cmd *exec.Cmd + // Use a shell to run the command cmd = exec.CommandContext(ctx, "sh", "-c", command) // #nosec G204 // Create a pipe to read the output from. @@ -43,7 +45,12 @@ func RunCommand(ctx context.Context, command string, doneFn func(output string)) _ = pw.Close() <-finishedCh - doneFn(stdoutBytes.String()) + + // Trim the carriage return added by the use of a shell. + output := stdoutBytes.String() + output = strings.TrimRight(output, "\r\n") + + doneFn(output) }() // Wait for the command to start From be79213ea9f514068a3008d7708c792ebdbbde49 Mon Sep 17 00:00:00 2001 From: Anthony Pauriche Date: Mon, 29 Sep 2025 16:28:11 +0200 Subject: [PATCH 3/3] check for os --- helpers/command.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/helpers/command.go b/helpers/command.go index 9c9f413..b4708fd 100644 --- a/helpers/command.go +++ b/helpers/command.go @@ -5,6 +5,7 @@ import ( "context" "errors" "io" + "os" "os/exec" "strings" "time" @@ -18,8 +19,13 @@ import ( func RunCommand(ctx context.Context, command string, doneFn func(output string)) error { var cmd *exec.Cmd - // Use a shell to run the command - cmd = exec.CommandContext(ctx, "sh", "-c", command) // #nosec G204 + // Execute the command using a shell to handle pipes and other shell features. + // Use /bin/sh on Linux/macOS and cmd.exe on Windows. + if isWindows() { + cmd = exec.CommandContext(ctx, "cmd.exe", "/C", command) // #nosec G204 + } else { + cmd = exec.CommandContext(ctx, "/bin/sh", "-c", command) // #nosec G204 + } // Create a pipe to read the output from. pr, pw := io.Pipe() @@ -81,3 +87,8 @@ func logOutput(r io.Reader, startedCh, finishedCh chan struct{}) { logger.Debug("Command output", map[string]any{"line": line}) } } + +// isWindows checks if the current OS is Windows. +func isWindows() bool { + return strings.HasPrefix(strings.ToLower(os.Getenv("OS")), "windows") +}