From fb35d3592af25a40634abad4ea8fe31e667c16f2 Mon Sep 17 00:00:00 2001 From: Tobias Schoknecht Date: Wed, 10 May 2023 11:07:49 +0200 Subject: [PATCH 1/3] Include stderr in result message --- command_locker.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/command_locker.go b/command_locker.go index 2e816a5..b9f41c4 100644 --- a/command_locker.go +++ b/command_locker.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "errors" + "fmt" "net/url" "os/exec" "time" @@ -102,8 +103,10 @@ func (ccl *ConsulCommandLocker) LockAndExecute(key, command string) (string, err targetTime := time.Now().Add(ccl.minLockTime) cmd := exec.CommandContext(ctx, "bash", "-c", command) - var out bytes.Buffer - cmd.Stdout = &out + var stdout bytes.Buffer + var stderr bytes.Buffer + cmd.Stdout = &stdout + cmd.Stderr = &stderr err = cmd.Run() // Ensure to wait at least the minimum lock time @@ -111,9 +114,14 @@ func (ccl *ConsulCommandLocker) LockAndExecute(key, command string) (string, err time.Sleep(remainingTime) } + resultOutput := stdout.String() + if stderr.String() != "" { + resultOutput = fmt.Sprintf("%s\nstderr: %s", resultOutput, stderr.String()) + } + if err != nil { - return "", err + return resultOutput, err } - return out.String(), nil + return resultOutput, nil } From ae51ac9ee81bf28614ab18161974d11204fd8cfa Mon Sep 17 00:00:00 2001 From: Tobias Schoknecht Date: Wed, 10 May 2023 11:08:07 +0200 Subject: [PATCH 2/3] Also print output in error case --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 05ce34c..1238271 100644 --- a/main.go +++ b/main.go @@ -64,7 +64,7 @@ func main() { } output, err := ccl.LockAndExecute(*key, command) if err != nil { - log.Fatalf("%v", err) + log.Fatalf("%v\nCommand output: %s", err, output) } fmt.Println("Command output:") From 0c69cb19fa061192f3559902b6c226bfba882545 Mon Sep 17 00:00:00 2001 From: Tobias Schoknecht Date: Wed, 10 May 2023 11:09:34 +0200 Subject: [PATCH 3/3] Bump package version --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ec530f7..8bf0581 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NAME=cronlocker -VERSION=0.8.0 +VERSION=0.9.0 ARCH=amd64 LICENSE="BSD 2-Clause License" MAINTAINER="Tobias Schoknecht "